面试到的职位使用了golang和kubernetes,所以提前自学下,先搭建个简单的开发环境。
安装 WSL 2
不做赘述。
下载minikube
这里使用binary download的方式。
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
之后使用 minikube命令可以看到输出。
$ 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.
这就算安装完成了。
启动集群
因为众所周知的原因gcr.io这些谷歌的服务全都被墙拦掉了。故全部转用国内的镜像。这里镜像推荐用阿里云的镜像或者ustc。
阿里云容器镜像服务 USTC Docker Hub 源使用帮助
这里使用我阿里云的容器镜像
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守护进程。