Minikube 搭建Kubernetes学习环境

1. 介绍

Minikube是Kubernetes社区维护的单机版的Kubernetes集群工具,帮助Kubernetes新手和开发者快速建立本地的Kubernetes集群,降低Kubernetes学习成本。

在Kubernetes文档中也首推开发者使用Minikube进行学习使用,见:https://kubernetes.io/zh/docs/tutorials/hello-minikube/

Minikube官网链接: https://minikube.sigs.k8s.io

Minikube 搭建Kubernetes学习环境

以下是在本地Linux虚拟机中搭建成功的结果示例:

[landscape@MiWiFi-R4CM-srv ~]$ minikube start
* Centos 7.8.2003 上的 minikube v1.15.1
* 根据现有的配置文件使用 docker 驱动程序
* Starting control plane node minikube in cluster minikube
* Updating the running docker "minikube" container ...
* 正在 Docker 20.10.0 中准备 Kubernetes v1.19.4…
* Verifying Kubernetes components...
* Enabled addons: storage-provisioner, default-storageclass, dashboard
* Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default

[landscape@MiWiFi-R4CM-srv ~]$ minikube status
minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured

[landscape@MiWiFi-R4CM-srv ~]$ docker container ls
CONTAINER ID        IMAGE                              COMMAND                  CREATED             STATUS              PORTS                                                                                                      NAMES
e4acce8ab8c3        kicbase/stable:v0.0.15-snapshot4   "/usr/local/bin/entr…"   5 weeks ago         Up 10 hours         127.0.0.1:32771->22/tcp, 127.0.0.1:32770->2376/tcp, 127.0.0.1:32769->5000/tcp, 127.0.0.1:32768->8443/tcp   minikube

[landscape@MiWiFi-R4CM-srv ~]$ minikube ssh
docker@minikube:~$ docker container ls
CONTAINER ID   IMAGE                                                           COMMAND                  CREATED        STATUS        PORTS     NAMES
c38e76753066   bad58561c4be                                                    "/storage-provisioner"   5 hours ago    Up 5 hours              k8s_storage-provisioner_storage-provisioner_kube-system_41b54bd4-c655-4f3d-8e65-0945bf564971_15
a697f54873d6   503bc4b7440b                                                    "/dashboard --insecu…"   10 hours ago   Up 10 hours             k8s_kubernetes-dashboard_kubernetes-dashboard-6f9cb85d88-6s4v9_kubernetes-dashboard_0c142bb5-a1ed-4f78-a56d-e408a1ce4120_8
150b34be990b   86262685d9ab                                                    "/metrics-sidecar"       10 hours ago   Up 10 hours             k8s_dashboard-metrics-scraper_dashboard-metrics-scraper-c85578d8-k7vdm_kubernetes-dashboard_1dd96b8e-c9ca-4e25-b632-0b187b85ca51_4
f9cf094c99f0   bfe3a36ebd25                                                    "/coredns -conf /etc…"   10 hours ago   Up 10 hours             k8s_coredns_coredns-6c76c8bb89-gb8rp_kube-system_6f72f157-73da-4618-b882-b9a2cddc6f46_8
aeb847fb4718   635b36f4d89f                                                    "/usr/local/bin/kube…"   10 hours ago   Up 10 hours             k8s_kube-proxy_kube-proxy-vm84w_kube-system_4e157482-6fee-4d5c-9e8c-7848666bfea2_7
006b63a03302   4830ab618586                                                    "kube-controller-man…"   10 hours ago   Up 10 hours             k8s_kube-controller-manager_kube-controller-manager-minikube_kube-system_be338aa45050ec7206c766bab91becdb_8
5f8a8157b70a   14cd22f7abe7                                                    "kube-scheduler --au…"   10 hours ago   Up 10 hours             k8s_kube-scheduler_kube-scheduler-minikube_kube-system_bfdfba5e160e5615ef897a84299c8df5_7
6c17b84fe3b0   b15c6247777d                                                    "kube-apiserver --ad…"   10 hours ago   Up 10 hours             k8s_kube-apiserver_kube-apiserver-minikube_kube-system_84fbf273d9d4bc25831a46f437b7344c_8
62509564babb   0369cf4303ff                                                    "etcd --advertise-cl…"   10 hours ago   Up 10 hours             k8s_etcd_etcd-minikube_kube-system_44e0bd5176f5542a3e8d77b000b60c76_8

虽然Minikube官方文档很完善,但是还是会让第一次使用的人摸不着头脑。希望这篇简单的文章可以帮助你绕过弯路初窥Kubernetes的真容。

如果文中出现错误或者有更好的方案,欢迎评论交流。

2. 安装

本文默认安装环境为:

  • Cent OS 7.X+
  • Docker 19.0+

其他版本或环境也可以稍作参考,没有安装Docker可以按照Docker官方文档的步骤安装Docker:https://docs.docker.com/engine/install/

2.1 Minikube

首先从官网选择合适的版本下载Minikube,地址:https://minikube.sigs.k8s.io/docs/start/,这里建议用讯雷或者其他工具下载
Minikube 搭建Kubernetes学习环境

上传到服务器,我这里选择了二进制文件进行安装,所以按照文档执行命令:sudo install minikube-linux-amd64 /usr/local/bin/minikube

[landscape@localhost ~]$ ls
minikube-linux-amd64
[landscape@localhost ~]$ sudo install minikube-linux-amd64 /usr/local/bin/minikube
[landscape@localhost ~]$ minikube start
* Centos 7.8.2003 上的 minikube v1.17.0
* minikube 1.17.1 is available! Download it: https://github.com/kubernetes/minikube/releases/tag/v1.17.1
* To disable this notice, run: 'minikube config set WantUpdateNotification false'

* 根据现有的配置文件使用 docker 驱动程序
* Starting control plane node minikube in cluster minikube
* Pulling base image ...

到这里继续按照官方文档的步骤直接执行minikube start大概率是没用的,minikube会从谷歌镜像仓库下载所需镜像,可能是用法网络等原因,按照阿里云社区这篇文档的步骤进行依旧无法使用:https://developer.aliyun.com/article/221687。
然后一直卡住,最终下载以失败告终,如下:

[landscape@localhost ~]$ ./minikube-linux-amd64-ali start --image-mirror-country cn --registry-mirror=https://registry.docker-cn.com
* Centos 7.8.2003 上的 minikube v1.16.0
* 根据现有的配置文件使用 docker 驱动程序
* Starting control plane node minikube in cluster minikube
* Pulling base image ...
E0130 20:58:57.870751    3153 cache.go:180] Error downloading kic artifacts:  failed to download kic base image or any fallback image
* Creating docker container (CPUs=2, Memory=2200MB) ...
! 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 kicbase/stable:v0.0.17 -d /var/lib: exit status 125
stdout:

stderr:
Unable to find image 'kicbase/stable:v0.0.17' locally
docker: Error response from daemon: Head https://registry-1.docker.io/v2/kicbase/stable/manifests/v0.0.17: Get https://auth.docker.io/token?scope=repository%3Akicbase%2Fstable%3Apull&service=registry.docker.io: net/http: TLS handshake timeout.
See 'docker run --help'.

* docker "minikube" container is missing, will recreate.
* Creating docker container (CPUs=2, Memory=2200MB) ...

我们从错误信息中得知minikube拉取的基础镜像是 kicbase/stable:v0.0.17,这里可以在DockerHub中找到镜像 ,如下:

Minikube 搭建Kubernetes学习环境

尝试手动拉取镜像:

[landscape@localhost ~]$ docker pull kicbase/stable:v0.0.17
v0.0.17: Pulling from kicbase/stable
da7391352a9b: Pull complete 
14428a6d4bcd: Pull complete 
2c2d948710f2: Pull complete 
a068cd99c676: Pull complete 
086636b22e9d: Pull complete 
5221d3fd465c: Pull complete 
8a2738df9ade: Pull complete 
74fc054d5085: Downloading [============================>                      ]  10.84MB/19.17MB
20228527694f: Download complete 
a7823b5983e9: Downloading [==>                                                ]  5.901MB/130.4MB
a036a37c5c11: Downloading [==========================================>        ]  36.82MB/43.48MB
bb15b50b0a94: Waiting 
a596876de093: Waiting 
0e301f5a0ff7: Waiting 
9e29a8a5c096: Waiting 
445dc96e3be9: Waiting 
f41b1d751d1d: Waiting 
cdabbc900ca4: Waiting 

网络不好的可以尝试更换版本、开启魔法上网工具,实在不行可以尝试一下其他的备用镜像:

Minikube 搭建Kubernetes学习环境

然后使用minikube参数指定你下载的镜像即可启动minikube

[landscape@localhost ~]$ minikube start --base-image="kicbase/stable:v0.0.15-snapshot4" --image-repository='registry.cn-hangzhou.aliyuncs.com/google_containers'
* Centos 7.8.2003 上的 minikube v1.17.0
* 自动选择 docker 驱动
* 正在使用镜像存储库 registry.cn-hangzhou.aliyuncs.com/google_containers
* Starting control plane node minikube in cluster minikube
* Creating docker container (CPUs=2, Memory=2200MB) ...
    > kubectl.sha256: 64 B / 64 B [--------------------------] 100.00% ? p/s 0s
    > kubeadm.sha256: 64 B / 64 B [--------------------------] 100.00% ? p/s 0s
  - Generating certificates and keys ...
  - Booting up control plane ...
  - Configuring RBAC rules ...
* Verifying Kubernetes components...
* Enabled addons: storage-provisioner, default-storageclass
* Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default

2.2 kubectl(可选)

接下来安装kubectl管理Kubernetes集群,可以直接在Kubernetes官网找到安装命令执行

curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"

sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl

也将版本号替换到链接中用X雷或者其他工具下载,例如我这里下载的版本是v1.20.2,所以直接下载 https://dl.k8s.io/release/v1.20.2/bin/linux/amd64/kubectl 即可。

3. 启动示例

写入一个简单的Deployment示例:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: mynginx
        image: nginx:1.19.6
        ports:
        - containerPort: 80

创建Deployment:

[landscape@localhost minikube]$ kubectl create -f nginx-deploy.yaml 
deployment.apps/nginx-deployment created
[landscape@localhost minikube]$ kubectl get rs
NAME                         DESIRED   CURRENT   READY   AGE
nginx-deployment-bd5567f5b   3         3         0       11s
[landscape@localhost minikube]$ kubectl get pods
NAME                               READY   STATUS              RESTARTS   AGE
nginx-deployment-bd5567f5b-ctqjb   0/1     ContainerCreating   0          15s
nginx-deployment-bd5567f5b-pzntl   0/1     ContainerCreating   0          15s
nginx-deployment-bd5567f5b-tp8vc   0/1     ContainerCreating   0          15s

这里是一个小坑,Minikube的镜像运行在Docker容器里没有任何配置,即会从官方registry拉取镜像,所以如果像下面一样创建失败了(ImagePullBackOff)也不奇怪

[landscape@localhost minikube]$ kubectl get pods
NAME                               READY   STATUS              RESTARTS   AGE
nginx-deployment-bd5567f5b-ctqjb   0/1     ContainerCreating   0          11m
nginx-deployment-bd5567f5b-pzntl   0/1     ImagePullBackOff    0          11m
nginx-deployment-bd5567f5b-tp8vc   0/1     ContainerCreating   0          11m

所以进入容器,像配置外部Docker一样配置容器内的镜像加速即可,使用minikube ssh进入容器,然后创建或修改/etc/docker/daemon.json文件,如果不了解的同学按照 阿里云 官方镜像加速这篇文章做就行。

minikube ssh

sudo chmod 777 /etc/docker/ && sudo echo '{"registry-mirrors": ["https://XXXXXXXX.mirror.aliyuncs.com"]}' > /etc/docker/daemon.json

重启minikube,stop + start ,直接start虽然显示update但是没什么用

[landscape@localhost minikube]$ minikube stop
* Stopping node "minikube"  ...
* 正在通过 SSH 关闭“minikube”…
* 1 nodes stopped.

[landscape@localhost minikube]$ minikube start
* Centos 7.8.2003 上的 minikube v1.17.0
* 根据现有的配置文件使用 docker 驱动程序
* Starting control plane node minikube in cluster minikube
* Restarting existing docker container for "minikube" ...
! This container 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/
* 正在 Docker 20.10.0 中准备 Kubernetes v1.20.2…
* Verifying Kubernetes components...
* Enabled addons: default-storageclass, storage-provisioner
* Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default

[landscape@localhost minikube]$ kubectl delete deploy nginx-deployment
deployment.apps "nginx-deployment" deleted

[landscape@localhost minikube]$ kubectl create -f nginx-deploy.yaml 
deployment.apps/nginx-deployment created

[landscape@localhost minikube]$ kubectl get rs
NAME                          DESIRED   CURRENT   READY   AGE
nginx-deployment-5599fc6777   3         3         0       4s

[landscape@localhost minikube]$ kubectl get pods
NAME                                READY   STATUS        RESTARTS   AGE
nginx-deployment-5599fc6777-7fmvp   1/1     Running       0          34s
nginx-deployment-5599fc6777-9t5k5   1/1     Running       0          34s
nginx-deployment-5599fc6777-tr49g   1/1     Running       0          34s
nginx-deployment-bd5567f5b-7rczv    0/1     Terminating   0          2m22s
nginx-deployment-bd5567f5b-pjcsl    0/1     Terminating   0          2m22s
nginx-deployment-bd5567f5b-xpsjb    0/1     Terminating   0          2m22s

pod成功运行,开始有趣的Kubernetes学习之旅吧

上一篇:9.1 Ansible 图形界面-AWX


下一篇:1003 我要通过!