介紹
Minikube 用於快速在本地搭建 Kubernetes 單節點叢集環境,它對硬體資源沒有太高的要求,方便開發人員學習試用,或者進行日常的開發。
其支援大部分kubernetes的功能,列表如下
- DNS
- NodePorts
- ConfigMaps and Secrets
- Dashboards
- Container Runtime: Docker, and rkt
- Enabling CNI (Container Network Interface)
- Ingress
- ...
Minikube 支援 Windows、macOS、Linux 三種 OS,會根據平臺不同,下載對應的虛擬機器映象,並在映象內安裝 k8s。
目前的虛擬機器技術都是基於Hypervisor 來實現的,Hypervisor 規定了統一的虛擬層介面,由此 Minikube 就可以無縫切換不同的虛擬機器實現,如 macOS 可以切換hyperkit 或 VirtualBox, Windows 下可以切換 Hyper-V 或 VirtualBox 等。
虛擬機器的切換可以通過 --vm-driver 實現,如minikube start --vm-driver hyperkit/ minikube start --vm-driver hyperv
如果 Minikube 安裝在核心原生就支援 LXC 的 OS 內,如 Ubuntu 等,再安裝一次虛擬機器顯然就是對資源的浪費了,Minikube 提供了直接對接 OS 底層的方式
-
driver!=none mode
In this case minikube provisions a new docker-machine (Docker daemon/Docker host) using any supported providers. For instance: a) local provider = your Windows/Mac local host: it frequently uses VirtualBox as a hypervisor, and creates inside it a VM based on boot2docker image (configurable). In this case k8s bootstraper (kubeadm) creates all Kubernetes components inside this isolated VM. In this setup you have usually two docker daemons, your local one for development (if you installed it prior), and one running inside minikube VM. b) cloud hosts - not supported by minikube
-
driver=none mode
In this mode, your local docker host is re-used. In case no.1 there will be a performance penalty, because each VM generates some overhead, by running several system processes required by VM itself, in addition to those required by k8s components running inside VM. I think driver-mode=none is similar to " HYPERLINK "https://blog.alexellis.io/be-kind-to-yourself/"kind" version of k8s boostraper, meant for doing CI/integration tests.
Minikube 安裝
下載Minikube
curl -Lo minikube https://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/releases/v1.13.0/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/
下載Kubectl
curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl && chmod +x kubectl && sudo mv kubectl /usr/local/bin/
啟動Minikube
啟動引數
啟動命令:minikube start "引數"
- --image-mirror-country cn 將預設利用 registry.cn-hangzhou.aliyuncs.com/google_containers 作為安裝Kubernetes的容器映象倉庫,
- --iso-url=*** 利用阿里雲的映象地址下載相應的 .iso 檔案
- --cpus=2: 為minikube虛擬機器分配CPU核數
- --memory=2000mb: 為minikube虛擬機器分配記憶體數
- --kubernetes-version=***: minikube 虛擬機器將使用的 kubernetes 版本 ,e.g. --kubernetes-version v 1.17.3
- --docker-env http_proxy 傳遞代理地址
預設啟動使用的是 VirtualBox 驅動,使用 --vm-driver 引數可以指定其它驅動
# https://minikube.sigs.k8s.io/docs/drivers/
- --vm-driver=none 表示用容器;
- --vm-driver=virtualbox 表示用虛擬機器;
注意: To use kubectl or minikube commands as your own user, you may need to relocate them. For example, to overwrite your own settings, run:
sudo mv /root/.kube /root/.minikube $HOME
sudo chown -R $USER $HOME/.kube $HOME/.minikube
示例
--vm-driver=kvm2
參考: https://minikube.sigs.k8s.io/docs/drivers/kvm2/
minikube start --image-mirror-country cn --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers --registry-mirror=https://ovfftd6p.mirror.aliyuncs.com --driver=kvm2
--vm-driver=hyperv
# 建立基於Hyper-V的Kubernetes測試環境
minikube.exe start --image-mirror-country cn \
--iso-url=https://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/iso/minikube-v1.5.0.iso \
--registry-mirror=https://xxxxxx.mirror.aliyuncs.com \
--vm-driver="hyperv" \
--hyperv-virtual-switch="MinikubeSwitch" \
--memory=4096
--vm-driver=none
sudo minikube start --image-mirror-country cn --vm-driver=none
sudo minikube start --vm-driver=none --docker-env http_proxy=http://$host_IP:8118 --docker-env https_proxy=https:// $host_IP:8118
其中$host_IP指的是host的IP,可以通過ifconfig檢視;比如在我這臺機器是10.0.2.15,用virtualbox部署,則用下列命令啟動minikube
sudo minikube start --vm-driver=none --docker-env http_proxy=http://10.0.2.15:8118 --docker-env https_proxy=https://10.0.2.15:8118
Minikube 狀態檢視
啟動完畢,將會執行一個單節點的Kubernetes叢集。Minikube也已經把kubectl配置好,因此無需做額外的工作就可以管理容器。
Minikube 建立一個Host-Only(僅主機模式)網路介面,通過這個介面可以路由到節點。如果要與執行的pods或services進行互動,你應該通過這個地址傳送流量。使用 minikube ip
命令可以檢視這個地址:
Minikube 使用
使用者使用Minikube CLI管理虛擬機器上的Kubernetes環境,比如:啟動,停止,刪除,獲取狀態等。一旦Minikube虛擬機器啟動,使用者就可以使用熟悉的Kubectl CLI在Kubernetes叢集上執行操作
# 檢視叢集的所有資源
kubectl get all
# 進入節點伺服器
minikube ssh
# 執行節點伺服器命令,例如檢視節點 docker info
minikube ssh -- docker info
# 刪除叢集, 刪除 ~/.minikube 目錄快取的檔案
minikube delete
# 關閉叢集
minikube stop
## 銷燬叢集
minikube stop && minikube delete
Minikube 外掛
sudo minikube addons list
Minikube 預設整合了 Kubernetes Dashboard。執行 minikube dashboard
命令後,預設會開啟瀏覽器
安裝遇到的問題
問題-1
Failed to save config: failed to acquire lock for /root/.minikube/profiles/minikube/config.json: unable to open /tmp/juju-mk270d1b5db5965f2dc9e9e25770a63417031943: permission denied
解決辦法:
sudo rm -rf /tmp/juju-mk*
sudo rm -rf /tmp/minikube.*
問題-2
unable to read client-cert /root/.minikube/client.crt for minikube due to open /root/.minikube/client.crt: permission denied
unable to read client-key /root/.minikube/client.key for minikube due to open /root/.minikube/client.key: permission denied
unable to read certificate-authority /root/.minikube/ca.crt for minikube due to open /root/.minikube/ca.crt: permission denied
解決辦法:
minikube stop
minikube delete
rm -rf ~/.kube
rm -rf ~/.minikube
sudo rm -rf /var/lib/minikube
sudo rm /var/lib/kubeadm.yaml
sudo rm -rf /etc/kubernetes
參考:https://stackoverflow.com/questions/58541104/minikube-wont-work-after-ubuntu-upgrade-to-19-10
問題-3
Error restarting cluster: restarting kube-proxy: waiting for kube-proxy to be up for configmap update: timed out waiting for the condition
通過 minikube delete,minikube start 可以解決
部署應用
$ kubectl run hello-minikube --image=k8s.gcr.io/echoserver:1.10 --port=8080
deployment.apps/hello-minikube created
#訪問應用
$ kubectl expose deployment hello-minikube --type=NodePort
service/hello-minikube exposed
#獲取服務地址
$ minikube service hello-minikube --url
http://192.168.99.105:30555