黑河网站seoseo整站优化吧
在某些时候可能需要快速的部署一个k8s集群用于测试,不想部署复杂的k8s集群环境,这个时候我们就可以使用kind来部署一个k8s集群了,下面是使用kind部署的过程
 一、安装单节点集群
 1、下载kind二进制文件
[root@localhost knid]# curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.17.0/kind-linux-amd64% Total    % Received % Xferd  Average Speed   Time    Time     Time  CurrentDload  Upload   Total   Spent    Left  Speed
100    97  100    97    0     0    115      0 --:--:-- --:--:-- --:--:--   1160     0    0     0    0     0      0      0 --:--:--  0:00:02 --:--:--     0
100 6766k  100 6766k    0     0   793k      0  0:00:08  0:00:08 --:--:-- 1259k
 
2、给下载好的kind添加可执行权限
[root@localhost knid]# chmod +x kind
 
3、将kind命令放在/usr/local/bin/kind
[root@localhost knid]# cp kind /usr/local/bin/kind
 
4、查看kind的版本
[root@localhost knid]# kind --version
kind version 0.17.0
 
5、使用kind创建集群
[root@localhost knid]# kind create cluster     # 使用此命令创建
Creating cluster "kind" ...✓ Ensuring node image (kindest/node:v1.25.3) 🖼 ✓ Preparing nodes 📦  ✓ Writing configuration 📜 ✓ Starting control-plane 🕹️ ✓ Installing CNI 🔌 ✓ Installing StorageClass 💾 
Set kubectl context to "kind-kind"
You can now use your cluster with:kubectl cluster-info --context kind-kindThanks for using kind! 😊
 
说明:上面我们已经安装好了 kind,接下来就可以使用 kind 搭建 k8s 集群了
 5.1、指定集群名称
# 使用 --name 指定名称
[root@localhost ~]# kind create cluster --name huhu
Creating cluster "huhu" ...✓ Ensuring node image (kindest/node:v1.25.3) 🖼 ✓ Preparing nodes 📦  ✓ Writing configuration 📜 ✓ Starting control-plane 🕹️ ✓ Installing CNI 🔌 ✓ Installing StorageClass 💾 
Set kubectl context to "kind-huhu"
You can now use your cluster with:kubectl cluster-info --context kind-huhuThanks for using kind! 😊
 
6、默认情况下,如果未设置 $KUBECONFIG 环境变量,则集群访问配置存储在 ${HOME}/.kube/config 中
[root@localhost knid]# ll ${HOME}/.kube/config
-rw-------. 1 root root 5582 9月  25 21:55 /root/.kube/config
 
7、查看kind是否启动完成
[root@localhost knid]# docker ps -a
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS              PORTS                       NAMES
d9fa3a93f629        kindest/node:v1.25.3   "/usr/local/bin/entr…"   4 minutes ago       Up 3 minutes        127.0.0.1:42737->6443/tcp   kind-control-plane
 
8、查看集群
[root@localhost ~]# kind get clusters
kind
huhu
 
9、查看集群信息
 9.1、查看集群我们需要进入到 docker 容器里面
# 查看 容器 id
[root@localhost ~]# docker ps |grep kind
d9fa3a93f629        kindest/node:v1.25.3   "/usr/local/bin/entr…"   12 hours ago        Up 12 hours         127.0.0.1:42737->6443/tcp   kind-control-plane# 进入到 docker 容器里面
[root@localhost ~]# docker exec -it d9fa3a93f629 /bin/bash
# 查看节点,可以看到,是单节点的
root@kind-control-plane:/# kubectl get nodes
NAME                 STATUS   ROLES           AGE   VERSION
kind-control-plane   Ready    control-plane   12h   v1.25.3
# 查看 pod
root@kind-control-plane:/# kubectl get pod -n kube-system
NAME                                         READY   STATUS    RESTARTS   AGE
coredns-565d847f94-7z8fc                     1/1     Running   0          12h
coredns-565d847f94-vtlbl                     1/1     Running   0          12h
etcd-kind-control-plane                      1/1     Running   0          12h
kindnet-69kkt                                1/1     Running   0          12h
kube-apiserver-kind-control-plane            1/1     Running   0          12h
kube-controller-manager-kind-control-plane   1/1     Running   0          12h
kube-proxy-stbwq                             1/1     Running   0          12h
kube-scheduler-kind-control-plane            1/1     Running   0          12h
 
10、删除集群
 10.1、创建了一个集群使用kind create cluster那么删除同样简单,使用
# 删除集群,--name如果未指定标志,则 kind 将使用默认集群上下文名称kind并删除该集群
[root@localhost ~]# kind delete cluster
 
11、删除指定名称的集群
# 删除名称为 huhu 的集群
[root@localhost ~]# kind delete cluster --name=huhu
Deleting cluster "huhu" ...
 
12、使用如下命令创建nginx应用
[root@localhost ~]# kubectl run nginx-test --image=nginx:latest
 
13、查看nginx应用是否部署成功
root@kind-control-plane:/# kubectl get pod 
NAME                    READY   STATUS    RESTARTS   AGE
nginx-test              1/1     Running   0          11h
 
二、安装多节点集群
 上面我们创建的是单节点的集群,默认安装的集群只部署了一个控制节点,如果需要部署多节点集群,我们可以通过配置文件的方式来创建多个容器。这样就可以达到模拟多个节点的目的,并以这些节点来构建一个多节点的 kubernetes 集群
创建多节点 kubernetes 集群配置文件
kind 在创建集群的时候,支持通过 --config 参数传递配置文件给 kind,配置文件可修改的内容主要有 role 和 节点使用的镜像
 1、创建一个 multi-node.yaml 的文件,内容如下
[root@localhost kind]# cat multi-node.yaml
kind: Cluster
# 一共两个节点,一个主节点,一个从节点
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane  # 主节点
- role: worker  # 从节点
 
2、配置文件创建完成后,就可以使用下面的命令来完成多节点 Kubernetes 集群搭建
# --config 指定 yaml 文件的路径
[root@localhost kind]# kind create cluster --config=multi-node.yaml --name=my-cluster1
Creating cluster "my-cluster1" ...✓ Ensuring node image (kindest/node:v1.25.3) 🖼 
⢄⡱ Preparing nodes 📦 📦  ✓ Preparing nodes 📦 📦  ✓ Writing configuration 📜 ✓ Starting control-plane 🕹️ ✓ Installing CNI 🔌 ✓ Installing StorageClass 💾 ✓ Joining worker nodes 🚜 
Set kubectl context to "kind-my-cluster1"
You can now use your cluster with:kubectl cluster-info --context kind-my-cluster1Thanks for using kind! 😊
 
3、使用 docker ps 就可以看到有两个容器在运行
[root@localhost kind]# docker ps -a
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS              PORTS                       NAMES
ece0ed3760bb        kindest/node:v1.25.3   "/usr/local/bin/entr…"   14 minutes ago      Up 13 minutes       127.0.0.1:38929->6443/tcp   my-cluster1-control-plane
01a17747a401        kindest/node:v1.25.3   "/usr/local/bin/entr…"   14 minutes ago      Up 13 minutes                                   my-cluster1-worker
 
4、进入到容器中,查看节点信息
[root@localhost kind]# docker exec -it ece0ed3760bb /bin/bash
root@my-cluster1-control-plane:/# kubectl get nodes
NAME                        STATUS   ROLES           AGE   VERSION
my-cluster1-control-plane   Ready    control-plane   14m   v1.25.3
my-cluster1-worker          Ready    <none>          13m   v1.25.3
# 成功创建了一个master节点和一个node节点
 
5、删除集群
# 执行后,将会删除两个 docker 容器 
[root@localhost kind]# kind delete cluster --name=my-cluster1[root@localhost kind]# docker ps | grep kind
