在 WSL 2 裏搭建 minikube 環境

面試到的職位使用了golang和kubernetes,因此提早自學下,先搭建個簡單的開發環境。html

安裝 WSL 2

官網文檔node

不作贅述。linux

下載minikube

官網文檔nginx

這裏使用binary download的方式。git

curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
 sudo install minikube-linux-amd64 /usr/local/bin/minikube

以後使用 minikube命令能夠看到輸出。github

$ minikube
minikube provisions and manages local Kubernetes clusters optimized for development workflows.

基本命令:
  start          Starts a local Kubernetes cluster
  status         Gets the status of a local Kubernetes cluster
  stop           Stops a running local Kubernetes cluster
  delete         Deletes a local Kubernetes cluster
  dashboard      Access the Kubernetes dashboard running within the minikube cluster
  pause          pause Kubernetes
  unpause        恢復 Kubernetes

Images Commands:
  docker-env     Configure environment to use minikube's Docker daemon
  podman-env     Configure environment to use minikube's Podman service
  cache          Add, delete, or push a local image into minikube

配置和管理命令:
  addons         Enable or disable a minikube addon
  config         Modify persistent configuration values
  profile        Get or list the the current profiles (clusters)
  update-context Update kubeconfig in case of an IP or port change

網絡和鏈接命令:
  service        Returns a URL to connect to a service
  tunnel         Connect to LoadBalancer services

高級命令:
  mount          將指定的目錄掛載到 minikube
  ssh            Log into the minikube environment (for debugging)
  kubectl        Run a kubectl binary matching the cluster version
  node           Add, remove, or list additional nodes

故障排除命令ƒ
  ssh-key        檢索指定集羣的 ssh 密鑰路徑
  ip             檢索正在運行的羣集的 IP 地址
  logs           Returns logs to debug a local Kubernetes cluster
  update-check   打印當前和最新版本版本
  version        打印 minikube 版本

Other Commands:
  completion     Generate command completion for a shell

Use "minikube <command> --help" for more information about a given command.

這就算安裝完成了。golang

啓動集羣

由於衆所周知的緣由gcr.io這些谷歌的服務全都被牆攔掉了。故所有轉用國內的鏡像。這裏鏡像推薦用阿里雲的鏡像或者ustc。面試

阿里雲容器鏡像服務 USTC Docker Hub 源使用幫助docker

這裏使用我阿里雲的容器鏡像shell

minikube start \
    --driver docker \
    --registry-mirror https://********.mirror.aliyuncs.com \
    --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers

若是要使用 USTC的鏡像或者你的阿里雲鏡像,把 registry-mirror 這個參數換成你的鏡像地址就行了。

輸出結果

😄  Debian 10.4 上的 minikube v1.11.0
✨  根據現有的配置文件使用 docker 驅動程序
👍  Starting control plane node minikube in cluster minikube
🏃  Updating the running docker "minikube" container ...
🐳  正在 Docker 19.03.2 中準備 Kubernetes v1.18.3…
    ▪ kubeadm.pod-network-cidr=10.244.0.0/16
🔎  Verifying Kubernetes components...
🌟  Enabled addons: default-storageclass, storage-provisioner
🏄  完成!kubectl 已經配置至 "minikube"

至此就安裝好了,能夠測試下。

weakptr@DESKTOP-DO4OJHD:~/sources/scripts/kubernetes$ kubectl run nginx --image nginx
pod/nginx created
weakptr@DESKTOP-DO4OJHD:~/sources/scripts/kubernetes$ kubectl get pod
NAME    READY   STATUS              RESTARTS   AGE
nginx   0/1     ContainerCreating   0          6s
weakptr@DESKTOP-DO4OJHD:~/sources/scripts/kubernetes$ kubectl logs nginx    
Error from server (BadRequest): container "nginx" in pod "nginx" is waiting to start: image can't be pulled

鏡像不能pull下來,參考這篇博客提到的思路,認定是vm裏(也就是kicbase這個容器裏)的docker沒法pull下鏡像。

驗證思路:

weakptr@DESKTOP-DO4OJHD:~$ docker exec -t d2c8bc89c023 docker pull nginx
Using default tag: latest
Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)

以前指定了 registry-mirror,因此理論上來講不該該出現這種狀況,除非這個registry-mirror沒有生效。用docker info檢查也證明了個人想法,發現確實沒有生效。minikube ssh docker info的輸出裏沒有registry-mirror的字段。

而這個問題我在github上找到了相關的issue和解決辦法。

其不生效的理由是以前已經用minikube start建立過vm,因此在此使用minikube去指定mirror-registry並不會更新已有vm的鏡像選項。

最簡單的處理方式是用minikube delete把現有的vm刪了,從新執行一遍minikube start,帶上mirror-registry參數。或者也能夠像是上面的issue 2638裏的方法,minikube ssh遠程進vm,而後手動修改好daemon.json,重啓docker守護進程。

相關文章
相關標籤/搜索