使用minikube安装单机测试Kubernetes集群

Posted by lili on August 28, 2020

本文介绍使用minikube安装单机测试Kubernetes集群,主要是解决因为墙带来的各种问题。为了让广大局域网用户用上k8s,minikube start还特地增加了image-mirror-country参数,咱终于享受到特供的待遇了。这里的Host系统是Ubuntu 16.04,其它系统请参考官方文档,需要翻墙或者设置mirror的地方可供参考。

目录

安装

检查虚拟化的支持

$ grep -E --color 'vmx|svm' /proc/cpuinfo

如果上面的命令返回的不是空就ok。

安装kubectl

kubectl的安装可以参考这里,难点是科学上网下载。 这是我安装的kubectl版本:

$ kubectl version
Client Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.3", GitCommit:"2e7996e3e2712684bc73f0dec0200d64eec7fe40", GitTreeState:"clean", BuildDate:"2020-05-20T12:52:00Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.3", GitCommit:"2e7996e3e2712684bc73f0dec0200d64eec7fe40", GitTreeState:"clean", BuildDate:"2020-05-20T12:43:34Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}

如果翻不了墙的同学也可以试试apt安装,不过版本可能会旧一点:

$ curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
$ sudo touch /etc/apt/sources.list.d/kubernetes.list 
$ echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list.d/kubernetes.list
$ sudo apt-get update
$ sudo apt-get install -y kubectl

安装一种Hypervisor

我们这里安装virtualbox,首先安装依赖:

$ sudo apt-get update
$ sudo apt-get install -y apt-transport-https

然后安装virtualbox:

$ sudo apt-get install -y virtualbox virtualbox-ext-pack

安装minicube

下载:

$ curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 \
  && chmod +x minikube

复制到/usr/local/bin:

sudo mkdir -p /usr/local/bin/
sudo cp minikube /usr/local/bin/

启动

这是最关键的地方,我之前尝试翻墙,但是因为要去gcr.io下载很大的image,还是很难成功。还好有image-mirror-country:

$ minikube start --image-mirror-country='cn' --image-repository='registry.cn-hangzhou.aliyuncs.com/google_containers'

第一次运行时需要拉很多镜像,中间可能会重试几次,会比较慢,经过一段时间之后就会成功启动:

$ minikube start --image-mirror-country='cn' --image-repository='registry.cn-hangzhou.aliyuncs.com/google_containers' 
😄  Ubuntu 16.04 上的 minikube v1.12.3
✨  Automatically selected the docker driver. Other choices: kvm2, virtualbox
✅  正在使用镜像存储库 registry.cn-hangzhou.aliyuncs.com/google_containers
👍  Starting control plane node minikube in cluster minikube
🚜  Pulling base image ...
E0828 16:50:34.140667 1129053 cache.go:63] save image to file "registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.18.3" -> "/home/lili/.minikube/cache/images/registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager_v1.18.3" failed: write: unexpected EOF
E0828 16:51:21.087078 1129053 cache.go:63] save image to file "registry.cn-hangzhou.aliyuncs.com/google_containers/storage-provisioner:v2" -> "/home/lili/.minikube/cache/images/registry.cn-hangzhou.aliyuncs.com/google_containers/storage-provisioner_v2" failed: write: unexpected EOF
E0828 16:51:23.083770 1129053 cache.go:63] save image to file "registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.7" -> "/home/lili/.minikube/cache/images/registry.cn-hangzhou.aliyuncs.com/google_containers/coredns_1.6.7" failed: write: unexpected EOF

🔥  Creating docker container (CPUs=2, Memory=7900MB) ...
🤦  StartHost failed, but will try again: creating host: create: creating: setting up container node: preparing volume for minikube container: docker run --rm --entrypoint /usr/bin/test -v minikube:/var gcr.io/k8s-minikube/kicbase:v0.0.11@sha256:6fee59db7d67ed8ae6835e4bcb02f32056dc95f11cb369c51e352b62dd198aa0 -d /var/lib: exit status 125
stdout:

stderr:
Unable to find image 'gcr.io/k8s-minikube/kicbase:v0.0.11@sha256:6fee59db7d67ed8ae6835e4bcb02f32056dc95f11cb369c51e352b62dd198aa0' locally
docker: Error response from daemon: Get https://gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers).
See 'docker run --help'.

🤷  docker "minikube" container is missing, will recreate.
🔥  Creating docker container (CPUs=2, Memory=7900MB) ...


😿  Failed to start docker container. "minikube start" may fix it: recreate: creating host: create: creating: setting up container node: preparing volume for minikube container: docker run --rm --entrypoint /usr/bin/test -v minikube:/var gcr.io/k8s-minikube/kicbase:v0.0.11@sha256:6fee59db7d67ed8ae6835e4bcb02f32056dc95f11cb369c51e352b62dd198aa0 -d /var/lib: exit status 125
stdout:

stderr:
Unable to find image 'gcr.io/k8s-minikube/kicbase:v0.0.11@sha256:6fee59db7d67ed8ae6835e4bcb02f32056dc95f11cb369c51e352b62dd198aa0' locally
docker: Error response from daemon: Get https://gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers).
See 'docker run --help'.

❗  Startup with docker driver failed, trying with alternate driver kvm2: Failed to start host: recreate: creating host: create: creating: setting up container node: preparing volume for minikube container: docker run --rm --entrypoint /usr/bin/test -v minikube:/var gcr.io/k8s-minikube/kicbase:v0.0.11@sha256:6fee59db7d67ed8ae6835e4bcb02f32056dc95f11cb369c51e352b62dd198aa0 -d /var/lib: exit status 125
stdout:

stderr:
Unable to find image 'gcr.io/k8s-minikube/kicbase:v0.0.11@sha256:6fee59db7d67ed8ae6835e4bcb02f32056dc95f11cb369c51e352b62dd198aa0' locally
docker: Error response from daemon: Get https://gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers).
See 'docker run --help'.

🔥  正在删除 docker 中的“minikube”…
🔥  正在移除 /home/lili/.minikube/machines/minikube…
💀  Removed all traces of the "minikube" cluster.
💾  正在下载驱动 docker-machine-driver-kvm2:
    > docker-machine-driver-kvm2.sha256: 65 B / 65 B [-------] 100.00% ? p/s 0s
    > docker-machine-driver-kvm2: 13.81 MiB / 13.81 MiB  100.00% 109.22 KiB p/s
✅  正在使用镜像存储库 registry.cn-hangzhou.aliyuncs.com/google_containers
💿  正在下载 VM boot image...
    > minikube-v1.12.2.iso.sha256: 65 B / 65 B [-------------] 100.00% ? p/s 0s
    > minikube-v1.12.2.iso: 173.73 MiB / 173.73 MiB  100.00% 582.39 KiB p/s 5m6
👍  Starting control plane node minikube in cluster minikube
🔥  Creating kvm2 VM (CPUs=2, Memory=6000MB, Disk=20000MB) ...
❗  This VM is having trouble accessing https://registry.cn-hangzhou.aliyuncs.com/google_containers
💡  To pull new external images, you may need to configure a proxy: https://minikube.sigs.k8s.io/docs/reference/networking/proxy/
E0828 17:14:18.228053 1129053 cache.go:190] Error caching images:  Caching images for kubeadm: caching images: caching image "/home/lili/.minikube/cache/images/registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager_v1.18.3": write: unexpected EOF
🐳  正在 Docker 19.03.12 中准备 Kubernetes v1.18.3…
    > kubelet.sha256: 65 B / 65 B [--------------------------] 100.00% ? p/s 0s
    > kubeadm.sha256: 65 B / 65 B [--------------------------] 100.00% ? p/s 0s
    > kubectl.sha256: 65 B / 65 B [--------------------------] 100.00% ? p/s 0s
    > kubeadm: 37.97 MiB / 37.97 MiB [-----------] 100.00% 173.12 KiB p/s 3m45s
    > kubectl: 41.99 MiB / 41.99 MiB [-----------] 100.00% 149.93 KiB p/s 4m47s
    > kubelet: 108.04 MiB / 108.04 MiB [---------] 100.00% 355.32 KiB p/s 5m11s
🔎  Verifying Kubernetes components...
🌟  Enabled addons: default-storageclass, storage-provisioner
🏄  完成!kubectl 已经配置至 "minikube"

不容易!终于成功了,我们验证一下:

$ minikube status
minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured

接着可以启动dashboard,图形界面看起来会比较好:

minikube dashboard
🤔  正在验证 dashboard 运行情况 ...
🚀  Launching proxy ...
🤔  正在验证 proxy 运行状况 ...
🎉  Opening http://127.0.0.1:42191/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/ in your default browser...

如果一切顺利的话会弹出浏览器。

创建Deployment

我们使用echo-server创建Deployment,注意不要使用墙外的:

$ kubectl create deployment hello-minikube --image=registry.aliyuncs.com/google_containers/echoserver:1.10

导出Service

kubectl expose deployment hello-minikube --type=NodePort --port=8080

查看状态

kubectl get pod

一开始可能是ContainerCreating状态,过一阵应该变成Running状态。如果有问题可以用kubectl describe pods看问题。

得到Service的url

$ minikube service hello-minikube --url
http://192.168.39.82:30740

然后用浏览器打开: