Akemi

使用kind快速部署多节点k8s

2025/02/06

kind可以用以部署多个集群,适用于本地多集群测试与CI/CD测试
支持多集群共享镜像缓存
缺点:只适用于内核版本比较高的系统,像是almalinux等
像是centos7.9就会因为内核版本落后,无法使用cgroup进行容器的资源隔离,启动时会报错

初始化内容(略过)

包括安装docker,kubectl等

安装kind

1
2
3
curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.22.0/kind-linux-amd64
chmod +x ./kind
sudo mv ./kind /usr/local/bin/kind

创建单节点集群

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
kind create cluster --name my-cluster
kind create cluster --name my-cluster-2

Creating cluster "my-cluster-2" ...
✓ Ensuring node image (kindest/node:v1.29.2) 🖼
✓ Preparing nodes 📦
✓ Writing configuration 📜
✓ Starting control-plane 🕹️
✓ Installing CNI 🔌
✓ Installing StorageClass 💾
Set kubectl context to "kind-my-cluster-2"
You can now use your cluster with:

kubectl cluster-info --context kind-my-cluster-2

Thanks for using kind! 😊

此时就已经创建了两个集群了,通过查看上下文可以
kubectl config get-contexts
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
kind-my-cluster kind-my-cluster kind-my-cluster
* kind-my-cluster-2 kind-my-cluster-2 kind-my-cluster-2

测试进行镜像拉取,能不能跑业务
cat >k8s-test.yaml <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:alpine
ports:
- containerPort: 80
EOF

kubectl apply -f k8s-test.yaml

删除这两个集群
kind delete cluster --name my-cluster
kind delete cluster --name my-cluster-2

创建多节点k8s集群

利用yaml配置文件来创建集群,可以指定k8s的镜像版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
cat >kind-config.yaml<<EOF
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
image: kindest/node:v1.23.0
extraMounts:
- hostPath: /var/run/docker.sock
containerPath: /var/run/docker.sock
- role: worker
image: kindest/node:v1.23.0
extraMounts:
- hostPath: /var/run/docker.sock
containerPath: /var/run/docker.sock
- role: worker
image: kindest/node:v1.23.0
extraMounts:
- hostPath: /var/run/docker.sock
containerPath: /var/run/docker.sock
EOF

kind create cluster --config kind-config.yaml --name ws-cluster

kubectl get nodes
NAME STATUS ROLES AGE VERSION
ws-cluster-control-plane Ready control-plane,master 11m v1.23.0
ws-cluster-worker Ready <none> 11m v1.23.0
ws-cluster-worker2 Ready <none> 11m v1.23.0

其他常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
创建单节点集群
kind create cluster --name <cluster-name>

使用配置文件创建集群
kind create cluster --config <config-name> --name <cluster-name>

删除集群
kind delete cluster --name <cluster-name>

列出集群
kind get clusters

导出集群配置到一个新的kubeconfig中
kind export kubeconfig --name <cluster-name> --kubeconfig <kubeconfig-name>

kind export kubeconfig --name ws-cluster --kubeconfig config_ws
#Set kubectl context to "kind-ws-cluster"
当前目录下生成了一个config_ws

加载镜像到集群内
kind load docker-image <image-name> --name <cluster-name>

停止与启动集群
docker stop $(docker ps -q -f "label=io.x-k8s.kind.cluster=<cluster-name>")
docker start $(docker ps -aq -f "label=io.x-k8s.kind.cluster=<cluster-name>")
CATALOG
  1. 1. 初始化内容(略过)
  2. 2. 安装kind
  3. 3. 创建单节点集群
  4. 4. 创建多节点k8s集群
  5. 5. 其他常用命令