Kind快速部署Kubernetes多集群

2023年 11月 22日 80.5k 0

Kind架构及原理

Kind 使用一个 container 来模拟一个 node,在 container 里面跑 systemd ,并用 systemd 托管 kubelet 以及 containerd,然后通过容器内部的 kubelet 把其他 K8s 组件,比如 kube-apiserver、etcd、CNI 等跑起来。

它可以通过配置文件的方式创建多个 container 来模拟创建多个 node,并以这些 node 构建一个多节点的 Kubernetes 集群。

Kind 内部使用的集群部署工具是 kubeadm,借助 kubeadm 提供的 Alpha 特性,它可以部署包括 HA master 的高可用集群。同时,在 HA master 下, 它还额外部署了一个 Nginx,用来提供负载均衡 vip。

Docker 安装

宿主机安装Docker

  1. for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done
  1. sudo apt-get update
  2. sudo apt-get install ca-certificates curl gnupg
  3. sudo install -m 0755 -d /etc/apt/keyrings
  4. curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
  5. sudo chmod a+r /etc/apt/keyrings/docker.gpg
  6. # Add the repository to Apt sources:
  7. echo
  8. "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu
  9. "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" |
  10. sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  11. sudo apt-get update

安装Docker

  1. sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Kind安装

首先我们下载Kind二进制包

  1. wget https://github.com/kubernetes-sigs/kind/releases/download/v0.20.0/kind-linux-amd64
  2. mv kind-linux-amd64 /usr/bin/kind
  3. chmod +x /usr/bin/kind

指定版本号并创建集群

  1. cat >kind-demo.yaml<<EOF
  2. kind: Cluster
  3. name: abcdocker-kind-demo
  4. apiVersion: kind.x-k8s.io/v1alpha4
  5. networking:
  6. disableDefaultCNI: false
  7. nodes:
  8. - role: control-plane
  9. #- role: control-plane
  10. #- role: control-plane
  11. - role: worker
  12. - role: worker
  13. - role: worker
  14. EOF

参数详解

  • name 设置集群名称
  • networking.disableDefaultCNI 创建的集群默认自带一个轻量级的 CNI 插件 kindnetd ,我们也可以禁用默认设置来安装其他 CNI,比如 Calico。
  • nodes control-plance master节点数量
  • nodes worker node节点数量

快速创建集群

本次版本采用v1.24.15

  1. kind create cluster --config=kind-demo.yaml --image=kindest/node:v1.24.15 --name=abcdocker-kind-demo
  2. 日志输出以下
  3. Creating cluster "abcdocker-kind-demo" ...
  4. Ensuring node image (kindest/node:v1.24.15) 🖼
  5. Preparing nodes 📦 📦
  6. Writing configuration 📜
  7. Starting control-plane 🕹️
  8. Installing CNI 🔌
  9. Installing StorageClass 💾
  10. Joining worker nodes 🚜
  11. Set kubectl context to "kind-abcdocker-kind-demo"
  12. You can now use your cluster with:
  13. kubectl cluster-info --context kind-abcdocker-kind-demo
  14. Thanks for using kind! 😊

安装kubectl

  1. curl -LO https://dl.k8s.io/release/v1.24.15/bin/linux/amd64/kubectl
  2. chmod +x kubectl
  3. mv kubectl /usr/bin/

可以查看集群状态

  1. root@abcdocker:~# kubectl cluster-info --context kind-abcdocker-kind-demo
  2. Kubernetes control plane is running at https://127.0.0.1:42687
  3. CoreDNS is running at https://127.0.0.1:42687/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
  4. To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'

接下来我们就可以get node查看到节点信息

  1. root@abcdocker:~# kubectl get node
  2. NAME STATUS ROLES AGE VERSION
  3. abcdocker-kind-demo-control-plane Ready control-plane 2m2s v1.24.15
  4. abcdocker-kind-demo-worker Ready <none> 90s v1.24.15
  5. root@abcdocker:~# kubectl get pod -n kube-system
  6. NAME READY STATUS RESTARTS AGE
  7. coredns-57575c5f89-9ltk6 1/1 Running 0 108s
  8. coredns-57575c5f89-glgvh 1/1 Running 0 107s
  9. etcd-abcdocker-kind-demo-control-plane 1/1 Running 0 2m7s
  10. kindnet-ljptz 1/1 Running 0 99s
  11. kindnet-qzglw 1/1 Running 0 109s
  12. kube-apiserver-abcdocker-kind-demo-control-plane 1/1 Running 0 2m5s
  13. kube-controller-manager-abcdocker-kind-demo-control-plane 1/1 Running 0 2m1s
  14. kube-proxy-kvzx9 1/1 Running 0 109s
  15. kube-proxy-pnzmz 1/1 Running 0 99s
  16. kube-scheduler-abcdocker-kind-demo-control-plane 1/1 Running 0 2m9s

Kind 常用命令

  1. 查看集群
  2. kind get clusters
  3. 删除集群
  4. kind delete cluster --name 集群名称

Kind创建多套集群

实际上只是需要给yaml文件修改即可

  1. cat >kind-demo1.yaml<<EOF
  2. kind: Cluster
  3. name: abcdocker-kind-demo1
  4. apiVersion: kind.x-k8s.io/v1alpha4
  5. networking:
  6. disableDefaultCNI: true #这里开启个cni
  7. nodes:
  8. - role: control-plane
  9. #- role: control-plane
  10. #- role: control-plane
  11. - role: worker
  12. EOF

disableDefaultCNI禁用了默认的kindnetd插件,CNI就需要我们自己安装flannel或者其它calico组件

使用下面命令创建集群

版本我们这边换成1.28.0

  1. kind create cluster --config=kind-demo1.yaml --image=kindest/node:v1.28.0 --name=abcdocker-kind-demo1
  2. 日志 输出如下
  3. Creating cluster "abcdocker-kind-demo1" ...
  4. Ensuring node image (kindest/node:v1.28.0) 🖼
  5. Preparing nodes 📦 📦
  6. Writing configuration 📜
  7. Starting control-plane 🕹️
  8. Installing StorageClass 💾
  9. Joining worker nodes 🚜
  10. Set kubectl context to "kind-abcdocker-kind-demo1"
  11. You can now use your cluster with:
  12. kubectl cluster-info --context kind-abcdocker-kind-demo1
  13. Thanks for using kind! 😊

查看集群状态

  1. root@abcdocker:~# kubectl get node
  2. NAME STATUS ROLES AGE VERSION
  3. abcdocker-kind-demo1-control-plane NotReady control-plane 3m46s v1.28.0
  4. abcdocker-kind-demo1-worker NotReady <none> 3m17s v1.28.0
  5. pod状态
  6. root@abcdocker:~# kubectl get pod -n kube-system
  7. NAME READY STATUS RESTARTS AGE
  8. coredns-5dd5756b68-l94jm 0/1 Pending 0 3m58s
  9. coredns-5dd5756b68-psm7p 0/1 Pending 0 3m58s
  10. etcd-abcdocker-kind-demo1-control-plane 1/1 Running 0 4m12s
  11. kube-apiserver-abcdocker-kind-demo1-control-plane 1/1 Running 0 4m11s
  12. kube-controller-manager-abcdocker-kind-demo1-control-plane 1/1 Running 0 4m12s
  13. kube-proxy-bln4n 1/1 Running 0 3m50s
  14. kube-proxy-l4k49 1/1 Running 0 3m59s
  15. kube-scheduler-abcdocker-kind-demo1-control-plane 1/1 Running 0 4m16s

切换集群

  • 1.24集群版本默认cni插件
  • 1.28集群版本不使用默认插件

接下来可以通过 kubectl config use-context xx_name快速切换集群

首先获取Cluster名称,我现在在k8s 1.28版本,现在我切换到1.24.15版本

  1. root@abcdocker:~# kind get clusters
  2. abcdocker-kind-demo
  3. abcdocker-kind-demo1

切换到abcdocker-kind-demo集群

  1. root@abcdocker:~# kubectl config use-context kind-abcdocker-kind-demo
  2. Switched to context "kind-abcdocker-kind-demo".
  3. root@abcdocker:~# kubectl get node
  4. NAME STATUS ROLES AGE VERSION
  5. abcdocker-kind-demo-control-plane Ready control-plane 43m v1.24.15
  6. abcdocker-kind-demo-worker Ready <none> 43m v1.24.15

切换到abcdocker-kind-demo1集群

  1. root@abcdocker:~# kubectl config use-context kind-abcdocker-kind-demo1
  2. Switched to context "kind-abcdocker-kind-demo1".
  3. root@abcdocker:~# kubectl get node
  4. NAME STATUS ROLES AGE VERSION
  5. abcdocker-kind-demo1-control-plane NotReady control-plane 36m v1.28.0
  6. abcdocker-kind-demo1-worker NotReady <none> 36m v1.28.0

测试集群

  1. cat<<EOF | kubectl apply -f -
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: nginx
  6. spec:
  7. selector:
  8. matchLabels:
  9. app: nginx
  10. template:
  11. metadata:
  12. labels:
  13. app: nginx
  14. spec:
  15. containers:
  16. - image: nginx:alpine
  17. name: nginx
  18. ports:
  19. - containerPort: 80
  20. ---
  21. apiVersion: v1
  22. kind: Service
  23. metadata:
  24. name: nginx
  25. spec:
  26. selector:
  27. app: nginx
  28. type: NodePort
  29. ports:
  30. - protocol: TCP
  31. port: 80
  32. targetPort: 80
  33. nodePort: 30001
  34. ---
  35. apiVersion: v1
  36. kind: Pod
  37. metadata:
  38. name: busybox
  39. namespace: default
  40. spec:
  41. containers:
  42. - name: busybox
  43. image: abcdocker9/centos:v1
  44. command:
  45. - sleep
  46. - "3600"
  47. imagePullPolicy: IfNotPresent
  48. restartPolicy: Always
  49. EOF

查看服务状态

  1. root@abcdocker:~# kubectl get pod,svc
  2. NAME READY STATUS RESTARTS AGE
  3. pod/busybox 1/1 Running 0 2m27s
  4. pod/nginx-6fb79bc456-st6qk 1/1 Running 0 2m27s
  5. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  6. service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 48m
  7. service/nginx NodePort 10.96.93.229 <none> 80:30001/TCP 2m28s

测试dns

  1. root@abcdocker:~# kubectl exec -ti busybox -- nslookup kubernetes
  2. Server: 10.96.0.10
  3. Address: 10.96.0.10#53
  4. Name: kubernetes.default.svc.cluster.local
  5. Address: 10.96.0.1

测试nginx nodeport

nginx nodeport>nginx需要进入到node容器

  1. root@abcdocker:~# kubectl get node
  2. NAME STATUS ROLES AGE VERSION
  3. abcdocker-kind-demo-control-plane Ready control-plane 53m v1.24.15
  4. abcdocker-kind-demo-worker Ready <none> 52m v1.24.15
  5. root@abcdocker:~# docker exec -it abcdocker-kind-demo-control-plane bash
  6. root@abcdocker-kind-demo-control-plane:/#

相关文章

KubeSphere 部署向量数据库 Milvus 实战指南
探索 Kubernetes 持久化存储之 Longhorn 初窥门径
征服 Docker 镜像访问限制!KubeSphere v3.4.1 成功部署全攻略
那些年在 Terraform 上吃到的糖和踩过的坑
无需 Kubernetes 测试 Kubernetes 网络实现
Kubernetes v1.31 中的移除和主要变更

发布评论