大家好,我是小碗汤,今天演示一个项目,可以在一分钟内用容器将k8s集群运行起来,真的很方便。

您可能已经知道,将 Kubernetes 集群安装在 VM 上。但在 Docker 容器中安装一个 Kubernetes 集群,还没有太多的实践。

本文我将简要演示如何在 Docker 容器中创建 Kubernetes 集群。如果你想看细节,可以查看 GitHub仓库

创建集群

下载脚本

git clone --depth=1 [https://github.com/ManasPecenek/clinco.git](https://github.com/ManasPecenek/clinco.git "https://github.com/ManasPecenek/clinco.git") && cd clinco && chmod +x initial-script.sh add-worker.sh

查看目录结构

# ls 
add-worker.sh  initial-script.sh  LICENSE  master image  README.md  worker image

现在您可以设置工作节点数,然后运行脚本。例如./initial-script.sh 1将产生一个 worker-node 集群:

./initial-script.sh <worker-node-count>

稍等片刻,即可看到1 master 1 worker集群创建好了:

# docker ps -a
CONTAINER ID   IMAGE                              COMMAND        CREATED          STATUS          PORTS                                                                          NAMES
7521c916e92e   petschenek/ubuntu-systemd:worker   "/sbin/init"   11 minutes ago   Up 11 minutes                                                                                  worker-1
a2e5bcf92c92   petschenek/ubuntu-systemd:master   "/sbin/init"   15 minutes ago   Up 15 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:6443->6443/tcp, :::6443->6443/tcp   master

master会将80、6443端口映射到宿主机上。

我们将容器中的kubectl命令复制到宿主机上,方便操作:

# docker cp master:/usr/local/bin/kubectl /usr/local/bin/

在clinco目录中默认存在了admin.kubeconfig文件,我们查看集群:

# kubectl get nodes -o wide --kubeconfig admin.kubeconfig
NAME       STATUS   ROLES    AGE   VERSION   INTERNAL-IP   EXTERNAL-IP   OS-IMAGE       KERNEL-VERSION                CONTAINER-RUNTIME
worker-1   Ready    <none>   29m   v1.21.0   172.172.1.1   <none>        Ubuntu 21.10   3.10.0-1160.31.1.el7.x86_64   containerd://1.5.8

# kubectl cluster-info --kubeconfig admin.kubeconfig
Kubernetes control plane is running at https://127.0.0.1:6443

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

添加工作节点

运行下面的脚本,例如./add-worker.sh 1 将向现有集群添加1个工作节点:

./add-worker.sh <worker-node-count>

查看添加的节点:

# docker ps -a
CONTAINER ID   IMAGE                              COMMAND        CREATED          STATUS          PORTS                                                                          NAMES
bfc68af79191   petschenek/ubuntu-systemd:worker   "/sbin/init"   14 seconds ago   Up 13 seconds                                                                                  worker-2
7521c916e92e   petschenek/ubuntu-systemd:worker   "/sbin/init"   33 minutes ago   Up 33 minutes                                                                                  worker-1
a2e5bcf92c92   petschenek/ubuntu-systemd:master   "/sbin/init"   36 minutes ago   Up 36 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:6443->6443/tcp, :::6443->6443/tcp   master

稍等片刻,节点会变成Ready:

# kubectl get nodes -o wide --kubeconfig admin.kubeconfig
NAME       STATUS   ROLES    AGE   VERSION   INTERNAL-IP   EXTERNAL-IP   OS-IMAGE       KERNEL-VERSION                CONTAINER-RUNTIME
worker-1   Ready    <none>   33m   v1.21.0   172.172.1.1   <none>        Ubuntu 21.10   3.10.0-1160.31.1.el7.x86_64   containerd://1.5.8
worker-2   Ready    <none>   38s   v1.21.0   172.172.1.2   <none>        Ubuntu 21.10   3.10.0-1160.31.1.el7.x86_64   containerd://1.5.8

为了方便,可以设置环境变量,或者将admin.kubeconfig复制到~/.kube/config文件中,这里设置个KUBECONFIG环境变量:

export KUBECONFIG=admin.kubeconfig

部署服务

部署2个实例的nginx deployment做测试:

kubectl create deploy nginx --image nginx --replicas 4

稍等片刻即可启动成功:

# kubectl  get pod  -o wide
NAME                     READY   STATUS              RESTARTS   AGE     IP       NODE       NOMINATED NODE   READINESS GATES
nginx-6799fc88d8-b5xhx   1/1     Running   0          2m10s   <none>   worker-2   <none>           <none>
nginx-6799fc88d8-ch2x9   1/1     Running   0          2m10s   <none>   worker-2   <none>           <none>

修复集群

如果使用delete node命令删除节点后,例如:

kubectl  delete node worker-2

则直接执行以下命令重启容器即可:

docker restart worker-2

如果容器被删除,删除的是主节点,想恢复集群,可以运行以下命令:

# docker stop master ; docker rm master
# docker run -dt --network clinco --hostname master --name master -v master:/root -v etcd:/lib/etcd -v /sys/fs/cgroup:/sys/fs/cgroup:ro --ip=172.172.0.1 -p 6443:6443 -p 80:80 --privileged --user root petschenek/ubuntu-systemd:master && docker exec -it --privileged --user root master bash -c "./master.sh"

如果容器被删除,删除的是工作节点之一(例如worker-2),可以运行以下命令来重新创建:

# docker stop worker-2 ; docker rm worker-2
# j=2 && docker run -dt --network clinco --hostname worker-$j --name worker-$j -v /lib/modules:/lib/modules:ro -v worker-$j:/root -v /sys/fs/cgroup:/sys/fs/cgroup:ro --ip=172.172.0.$j --privileged --user root petschenek/ubuntu-systemd:worker && docker exec -it --privileged --user root worker-$j bash -c "./worker.sh"

原理

其实在master node容器中运行了系统组件:

# docker exec -ti master ps -ef  | grep kube
root        1452       1  3 13:10 ?        00:01:33 /usr/local/bin/kube-apiserve
root        1453       1  0 13:10 ?        00:00:21 /usr/local/bin/kube-controll
root        1454       1  0 13:10 ?        00:00:05 /usr/local/bin/kube-schedule
# docker exec -ti master ps -ef  | grep etcd
root        1367       1  1 13:10 ?        00:00:29 /usr/local/bin/etcd --name m

在worker node容器中运行了kubelet:

# docker exec -ti worker-1 ps -ef  | grep kubelet
root         980       1  0 13:11 ?        00:00:20 /usr/local/bin/kubelet --con

# docker exec -ti worker-2 ps -ef  | grep kubelet
root         981       1  0 13:44 ?        00:00:05 /usr/local/bin/kubelet --con

因为在master node容器中没有运行kubelet,所以查看node时看不到master。

容器网络都为clinco模式:

# docker network  ls -f name=clinco
NETWORK ID     NAME      DRIVER    SCOPE
5d018b1d9b06   clinco    bridge    local

# docker inspect master | grep NetworkMode
            "NetworkMode": "clinco",
# docker inspect worker-1 | grep NetworkMode
            "NetworkMode": "clinco",
# docker inspect worker-2 | grep NetworkMode
            "NetworkMode": "clinco",

数据也是挂载到宿主机上的:

# docker volume ls -f name=etcd
DRIVER    VOLUME NAME
local     etcd

总结

这里用到两个镜像(master、worker各一个),我们可以在10秒内轻松装好一个 Kubernetes 集群,平时测试用起来将变得方便快捷。

原文

本文首发于微信公众号【我的小碗汤】,扫左侧码关注,了解更多咨询,更有免费资源供您学习。

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐