面试到的职位使用了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和解决办法。
- issue 2638: how to set up mirror-registry
- issue 6848: how to update the registry-mirror of a minikube instance
其不生效的理由是以前已经用minikube start
建立过vm,因此在此使用minikube
去指定mirror-registry
并不会更新已有vm的镜像选项。
最简单的处理方式是用minikube delete
把现有的vm删了,从新执行一遍minikube start
,带上mirror-registry
参数。或者也能够像是上面的issue 2638里的方法,minikube ssh
远程进vm,而后手动修改好daemon.json,重启docker守护进程。