介紹
Minikube 是 K8S 官方為了開發者能在個人電腦上運行 K8S 而提供的一套工具。實現上是通過 Go 語言編寫,通過調用虛擬化管理程序,創建出一個運行在虛擬機內的單節點集群。
注:從這里也可以看出,對於 K8S 集群的基本功能而言,節點數並沒有什么限制。只有一個節點同樣可以創建集群。
大家在實際開發和體驗 Kubernetes時,可能會遇到網絡訪問的原因或者其他的坑導致很多朋友無法使用minikube進行實驗。因此我通過撰寫這篇博客,讓大家少走一點彎路。阿里雲提供了一個修改版的Minikube,可以從阿里雲的鏡像地址來獲取Docker鏡像和配置。
坑位盤點
-
unable to cache ISO: connection failed because connected host has failed to respond.
終端報錯信息:
minikube start o minikube v0.35.0 on windows (amd64) > Creating virtualbox VM (CPUs=2, Memory=2048MB, Disk=20000MB) ... @ Downloading Minikube ISO ... ! Unable to start VM: unable to cache ISO: https://storage.googleapis.com/minikube/iso/minikube-v0.35.0.iso: failed to download: failed to download to temp file: download failed: 5 error(s) occurred : * Temporary download error: Get https://storage.googleapis.com/minikube/iso/minikube-v0.35.0.iso: dial tcp 216.58.207.144:443: connectex: A connection attempt failed because the connected party did no t properly respond after a period of time, or established connection failed because connected host has failed to respond. * Temporary download error: Get https://storage.googleapis.com/minikube/iso/minikube-v0.35.0.iso: dial tcp 216.58.207.144:443: connectex: A connection attempt failed because the connected party did no t properly respond after a period of time, or established connection failed because connected host has failed to respond. * Temporary download error: Get https://storage.googleapis.com/minikube/iso/minikube-v0.35.0.iso: dial tcp 216.58.207.144:443: connectex: A connection attempt failed because the connected party did no t properly respond after a period of time, or established connection failed because connected host has failed to respond. * Temporary download error: Get https://storage.googleapis.com/minikube/iso/minikube-v0.35.0.iso: dial tcp 216.58.207.144:443: connectex: A connection attempt failed because the connected party did no t properly respond after a period of time, or established connection failed because connected host has failed to respond. * Temporary download error: Get https://storage.googleapis.com/minikube/iso/minikube-v0.35.0.iso: dial tcp 216.58.207.144:443: connectex: A connection attempt failed because the connected party did no t properly respond after a period of time, or established connection failed because connected host has failed to respond.
報錯原因: 由於無法從外網下載Minikube ISO出現如上報錯。
解決辦法: 修改鏡像地址為國內資源,解決網絡錯誤。
-
Minikube doesn't run on VirtualBox
終端報錯信息:
minikube start 😄 minikube v1.0.1 on linux (amd64) 🤹 Downloading Kubernetes v1.14.1 images in the background ... 🔥 Creating virtualbox VM (CPUs=2, Memory=2048MB, Disk=20000MB) ... 💣 Unable to start VM: create: creating: Unable to start the VM: /usr/bin/VBoxManage startvm minikube --type headless failed: VBoxManage: error: The virtual machine 'minikube' has terminated unexpectedly during startup with exit code 1 (0x1) VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component MachineWrap, interface IMachine 😿 Sorry that minikube crashed. If this was unexpected, we would love to hear from you: 👉 https://github.com/kubernetes/minikube/issues/new
報錯原因: VT-x/AMD-v虛擬化在所有CPU中被禁用。
解決方法: VT-x/AMD-v虛擬化必須在BIOS中開啟。
參考文檔: https://jingyan.baidu.com/article/fc07f98976710e12ffe519de.html
-
[MACHINE_DOES_NOT_EXST] Error getting state for host: machine does not exist
終端報錯信息
[sudo] james 的密碼: 😄 minikube v1.2.0 on linux (amd64) ⚠️ Please don't run minikube as root or with 'sudo' privileges. It isn't necessary. ✅ using image repository registry.cn-hangzhou.aliyuncs.com/google_containers 💡 Tip: Use 'minikube start -p <name>' to create a new cluster, or 'minikube delete' to delete this one. E0722 14:57:51.637843 5215 start.go:559] StartHost: Error getting state for host: machine does not exist 💣 Unable to start VM ❌ Error: [MACHINE_DOES_NOT_EXST] Error getting state for host: machine does not exist 💡 Advice: Run 'minikube delete' to delete the stale VM ⁉️ Related issues: ▪ https://github.com/kubernetes/minikube/issues/3864 😿 If the above advice does not help, please let us know: 👉 https://github.com/kubernetes/minikube/issues/new
報錯原因: 已經被使用過VM
解決辦法:
minikute delete
注:
-
本文已更新到 Minikube v1.2.0/Kubernetes v1.15+
-
如需更新minikube,需要更新 minikube 安裝包
minikube delete
刪除現有虛機,刪除~/.minikube
目錄緩存的文件- 重新創建 minikube 環境
-
Docker社區版也為Mac/Windows用戶提供了Kubernetes開發環境的支持 https://yq.aliyun.com/articles/508460,大家也可以試用
配置
先決條件
- 安裝 kubectl
Minikube在不同操作系統上支持不同的驅動
- macOS
- Linux
- VirtualBox 或 KVM
- NOTE: Minikube 也支持
--vm-driver=none
選項來在本機運行 Kubernetes 組件,這時候需要本機安裝了 Docker。在使用 0.27版本之前的 none 驅動時,在執行minikube delete
命令時,會移除 /data 目錄,請注意,問題說明;另外 none 驅動會運行一個不安全的API Server,會導致安全隱患,不建議在個人工作環境安裝。
- Windows
- VirtualBox 或 Hyper-V - 請參考下文
注:
- 由於minikube復用了docker-machine,在其軟件包中已經支持了相應的VirtualBox, VMware Fusion驅動
- VT-x/AMD-v 虛擬化必須在 BIOS 中開啟
- 在Windows環境下,如果開啟了Hyper-V,不支持VirtualBox方式
Kubernetes 1.14+ release
我們提供了最新的Minikube修改版的文件,可以直接下載使用
Mac OSX
curl -Lo minikube http://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/releases/v1.2.0/minikube-darwin-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/
Linux
curl -Lo minikube http://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/releases/v1.2.0/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/
Windows
下載 minikube-windows-amd64.exe 文件,並重命名為 minikube.exe
自己構建
也可以從Github上獲取相應的項目自行構建。
注:需要本地已經安裝配置好 Golang 開發環境和Docker引擎
git clone https://github.com/AliyunContainerService/minikube
cd minikube
git checkout aliyun-v1.2.0
make
sudo cp out/minikube /usr/local/bin/
啟動
缺省Minikube使用VirtualBox驅動來創建Kubernetes本地環境
minikube start --registry-mirror=https://registry.docker-cn.com
😄 minikube v1.2.0 on linux (amd64)
⚠️ Please don't run minikube as root or with 'sudo' privileges. It isn't necessary.
✅ using image repository registry.cn-hangzhou.aliyuncs.com/google_containers
🔥 Creating virtualbox VM (CPUs=2, Memory=2048MB, Disk=20000MB) ...
🐳 Configuring environment for Kubernetes v1.15.0 on Docker 18.09.6
🚜 Pulling images ...
🚀 Launching Kubernetes ...
⌛ Verifying: apiserver proxy etcd scheduler controller dns
🏄 Done! kubectl is now configured to use "minikube"
支持不同的Kubernetes版本
# 安裝Kubernetes v1.12.1
minikube start --registry-mirror=https://registry.docker-cn.com --kubernetes-version v1.12.1
打開Kubernetes控制台
minikube dashboard
對於使用Hyper-V環境的用戶,首先應該打開Hyper-V管理器創建一個外部虛擬交換機,
之后,我們可以用如下命令來創建基於Hyper-V的Kubernetes測試環境
.\minikube.exe start --registry-mirror=https://registry.docker-cn.com --vm-driver="hyperv" --memory=4096 --hyperv-virtual-switch="MinikubeSwitch"
注:需要管理員權限來創建Hyper-V虛擬機
Minikube基本操作
檢測集群狀態,運行:
kubectl cluster-info
Kubernetes master is running at https://192.168.99.100:8443
KubeDNS is running at https://192.168.99.100:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
Ubuntu下,Minikube的配置文件在如下路徑
~/.minikube/machines/minikube/config.json
查看配置文件內容:
kubectl config view
apiVersion: v1
clusters:
- cluster:
certificate-authority: /home/james/.minikube/ca.crt
server: https://192.168.99.100:8443
name: minikube
contexts:
- context:
cluster: minikube
user: minikube
name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: minikube
user:
client-certificate: /home/james/.minikube/client.crt
client-key: /home/james/.minikube/client.key
檢驗Node狀態:
kubectl get nodes
NAME STATUS ROLES AGE VERSION
minikube Ready master 11m v1.15.0
使用ssh進入Minikube虛機:
sudo minikube ssh
_ _
_ _ ( ) ( )
___ ___ (_) ___ (_)| |/') _ _ | |_ __
/' _ ` _ `\| |/' _ `\| || , < ( ) ( )| '_`\ /'__`\
| ( ) ( ) || || ( ) || || |\`\ | (_) || |_) )( ___/
(_) (_) (_)(_)(_) (_)(_)(_) (_)`\___/'(_,__/'`\____)
$
停止運行中的kubernetes集群:
$ minikube stop
刪除本地的kubernetes集群:
$ minikube delete
打開Kubernetes控制台
Kubernete附帶一個web,允許您在不與命令行交互的情況下管理集群。在minikube上默認安裝並啟用儀表板插件
$ minikube addons list
- addon-manager: enabled
- coredns: disabled
- dashboard: enabled
- default-storageclass: enabled
- efk: disabled
- freshpod: disabled
- heapster: disabled
- ingress: disabled
- kube-dns: enabled
- metrics-server: disabled
- registry: disabled
- registry-creds: disabled
- storage-provisioner: enabled
要直接在默認瀏覽器上打開,請使用:
$ minikube dashboard
獲取儀表板的URL
$ minikube dashboard --url
http://192.168.39.117:30000
通過打開您最喜歡的瀏覽器上的URL訪問Kubernetes Dashboard。進一步閱讀,請查看:
-
你好Minikube系列: https://kubernetes.io/docs/tutorials/stateless-application/hello-minikube/
-
minkube新手指南: https://kubernetes.io/docs/getting-started-guides/minikube/
使用Minikube
Minikube利用本地虛擬機環境部署Kubernetes,其基本架構如下圖所示。
用戶使用Minikube CLI管理虛擬機上的Kubernetes環境,比如:啟動,停止,刪除,獲取狀態等。一旦Minikube虛擬機啟動,用戶就可以使用熟悉的Kubectl CLI在Kubernetes集群上執行操作。
好了,開始探索Kubernetes的世界吧!😃
參考連接:
- https://computingforgeeks.com/how-to-install-minikube-on-ubuntu-18-04/
- https://www.virtualbox.org/wiki/Linux_Downloads
- https://github.com/kubernetes/minikube/issues/3922
- https://stackoverflow.com/questions/55988282/minikube-doesnt-run-on-virtualbox
- https://github.com/AliyunContainerService/minikube/releases
- https://yq.aliyun.com/articles/221687
- https://jingyan.baidu.com/article/fc07f98976710e12ffe519de.html
- https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors
- https://github.com/kubernetes/minikube/issues/3864