kind 在本地快速部署一个 k8s集群

镜像

kindest-node.tar

docker load -i kindest-node.tar

启动程序

#kind
chmod +x kind
cp kind /usr/bin

配置文件

kind-ha.yml

启动命令

kind create cluster –image kindest/node:v1.25.3 –name ha –config kind-ha.yml

1
2
3
4
5
6
[root@19]# docker ps 
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
04f2872acfce kindest/node:v1.25.3 "/usr/local/bin/entr…" 6 minutes ago Up 6 minutes 127.0.0.1:42432->6443/tcp ha-control-plane
acfcbef16924 kindest/node:v1.25.3 "/usr/local/bin/entr…" 6 minutes ago Up 6 minutes ha-worker2
0c8a93ffe53e kindest/node:v1.25.3 "/usr/local/bin/entr…" 6 minutes ago Up 6 minutes ha-worker

进入管理节点

docker exec -it ha-control-plane bash

在管理节点内执行K8S命令

kubectl get pod -A
kubectl get node

删除集群

kind delete cluster –name ha

如下在宿主机器执行

获取K8S集群的访问控制文件

mkdir /etc/kubernetes
docker cp ha-control-plane:/etc/kubernetes/admin.conf /etc/kubernetes/

增加环境变量

export KUBECONFIG=/etc/kubernetes/admin.conf

#添加集群hosts主机名信息
#主要是把容器名对应的ip解析出来
docker network inspect kind | awk -F”:” ‘{if( $1 ~ /Containers/ ){p_f=1} ;if($1 ~ /Name/ && p_f == 1 ){print $2};if($1 ~ /IPv4Address/ ){ print $2 }}’ |xargs |awk -F “[/|,| ]” ‘{print $3 , $1 ; print $8 , $6 ;print $13 , $11}’ >> /etc/hosts

在宿主机执行客户端命令

kubectl get node
kubectl get pod -A

操作结果如下

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
54
55
56
57
58
59
60
61
62
[root@19]# kind create cluster --image kindest/node:v1.25.3 --name ha --config kind-ha.yml
Creating cluster "ha" ...
✓ Ensuring node image (kindest/node:v1.25.3) 🖼
✓ Preparing nodes 📦 📦 📦
✓ Writing configuration 📜
✓ Starting control-plane 🕹️
✓ Installing CNI 🔌
✓ Installing StorageClass 💾
✓ Joining worker nodes 🚜
Set kubectl context to "kind-ha"
You can now use your cluster with:

kubectl cluster-info --context kind-ha

Not sure what to do next? 😅 Check out https://kind.sigs.k8s.io/docs/user/quick-start/
[root@19]# docker ps | grep kind
27e3711bee1e kindest/node:v1.25.3 "/usr/local/bin/entr…" 2 minutes ago Up 2 minutes ha-worker
b111ce3503c7 kindest/node:v1.25.3 "/usr/local/bin/entr…" 2 minutes ago Up 2 minutes 127.0.0.1:46233->6443/tcp ha-control-plane
f20a7408480a kindest/node:v1.25.3 "/usr/local/bin/entr…" 2 minutes ago Up 2 minutes ha-worker2
[root@19]# mkdir /etc/kubernetes
[root@19]#
[root@19]# docker cp ha-control-plane:/etc/kubernetes/admin.conf /etc/kubernetes/
Preparing to copy...
Successfully copied 7.168kB to /etc/kubernetes/
[root@19]#
[root@19]# export KUBECONFIG=/etc/kubernetes/admin.conf
[root@19]#
[root@19]#
[root@19]# docker network inspect kind | awk -F":" '{if( $1 ~ /Containers/ ){p_f=1} ;if($1 ~ /Name/ && p_f == 1 ){print $2};if($1 ~ /IPv4Address/ ){ print $2 }}' |xargs |awk -F "[/|,| ]" '{print $3 , $1 ; print $8 , $6 ;print $13 , $11}'
172.18.0.4 ha-worker
172.18.0.3 ha-control-plane
172.18.0.2 ha-worker2
[root@19]#
[root@19]# docker network inspect kind | awk -F":" '{if( $1 ~ /Containers/ ){p_f=1} ;if($1 ~ /Name/ && p_f == 1 ){print $2};if($1 ~ /IPv4Address/ ){ print $2 }}' |xargs |awk -F "[/|,| ]" '{print $3 , $1 ; print $8 , $6 ;print $13 , $11}' >> /etc/hosts
[root@19]#
[root@19]# chmod +x kubectl
[root@19]#
[root@19]# kubectl get node
-bash: kubectl: 未找到命令
[root@19]# ./kubectl get node
NAME STATUS ROLES AGE VERSION
ha-control-plane Ready control-plane 3m17s v1.25.3
ha-worker Ready <none> 2m44s v1.25.3
ha-worker2 Ready <none> 2m44s v1.25.3
[root@19]#
[root@19]# ./kubectl get pod -A
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-565d847f94-knp4c 1/1 Running 0 3m5s
kube-system coredns-565d847f94-zbd2l 1/1 Running 0 3m5s
kube-system etcd-ha-control-plane 1/1 Running 0 3m18s
kube-system kindnet-5fqvm 1/1 Running 0 2m49s
kube-system kindnet-pt9t5 1/1 Running 0 3m5s
kube-system kindnet-v8fmm 1/1 Running 0 2m49s
kube-system kube-apiserver-ha-control-plane 1/1 Running 0 3m18s
kube-system kube-controller-manager-ha-control-plane 1/1 Running 0 3m18s
kube-system kube-proxy-9qvhb 1/1 Running 0 2m49s
kube-system kube-proxy-g5mlh 1/1 Running 0 3m5s
kube-system kube-proxy-tdhdj 1/1 Running 0 2m49s
kube-system kube-scheduler-ha-control-plane 1/1 Running 0 3m18s
local-path-storage local-path-provisioner-684f458cdd-jmkl2 1/1 Running 0 3m5s
[root@19]#