声明:本实验是在Linux系统环境下进行演示。

文章目录

    • 1.安装kubectl
    • 2.安装kind
    • 3.安装Docker
    • 4.部署多个Kind集群
    • 5.查看Cluster
    • 6.切换kind集群
    • 7.指定镜像版本安装集群
    • 8.删除Kind集群
    • 9.查看kind 帮助命令
    • 10.查看kind create帮助命令
    • 11.使用配置文件安装集群
    • 12.参考链接

1.安装kubectl

$ yum install -y kubectl$ kubectl version --clientClient Version: v1.28.2Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3

2.安装kind

# For AMD64 / x86_64[ $(uname -m) = x86_64 ] && curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.20.0/kind-$(uname)-amd64注:需要科学上网chmod +x ./kindsudo mv ./kind /usr/local/bin/kind$ kind versionkind v0.20.0 go1.20.4 linux/amd64

3.安装Docker

要使用kind,需要安装docker。

# 安装yum-utils包(它提供yum-config-manager实用程序)$ sudo yum install -y yum-utils# 设置存储库$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo# 安装Docker Engine、containerd和Docker Compose$ sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin# 启动docker服务$ systemctl start docker && systemctl enable docker$ systemctl status docker# 查看docker信息$ docker versionClient: Docker Engine - Community Version: 24.0.7 API version: 1.43 Go version:go1.20.10 Git commit:afdd53b Built: Thu Oct 26 09:11:35 2023 OS/Arch: linux/amd64 Context: default

4.部署多个Kind集群

1️⃣# 部署kind[root@Kind ~]# kind create clusterCreating cluster "kind" ... ✓ Ensuring node image (kindest/node:v1.27.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-kindNot sure what to do next? Check out https://kind.sigs.k8s.io/docs/user/quick-start/2️⃣# 部署kind-2[root@Kind ~]# kind create cluster --name kind-02Creating cluster "kind-02" ... ✓ Ensuring node image (kindest/node:v1.27.3)✓ Preparing nodes✓ Writing configuration✓ Starting control-plane ️ ✓ Installing CNI✓ Installing StorageClass Set kubectl context to "kind-kind-02"You can now use your cluster with:kubectl cluster-info --context kind-kind-02Thanks for using kind! 

5.查看Cluster

[root@Kind ~]# kubectl get nodesNAME STATUS ROLES AGE VERSIONkind-control-plane Readycontrol-plane 4h36m v1.27.3[root@Kind ~]# kind get clusterskindkind-02

查看集群Pod组件。

[root@Kind ~]# kubectl get pods -ANAMESPACENAMEREADY STATUSRESTARTS AGEkube-systemcoredns-5d78c9869d-667881/1 Running 011skube-systemcoredns-5d78c9869d-snf8b1/1 Running 011skube-systemetcd-kind-01-control-plane1/1 Running 024skube-systemkindnet-ljmm9 1/1 Running 011skube-systemkube-apiserver-kind-01-control-plane1/1 Running 026skube-systemkube-controller-manager-kind-01-control-plane 1/1 Running 024skube-systemkube-proxy-g4z7c1/1 Running 011skube-systemkube-scheduler-kind-01-control-plane1/1 Running 024slocal-path-storage local-path-provisioner-6bc4bddd6b-hg48b 1/1 Running 011s

6.切换kind集群

[root@Kind ~]# kubectl config use-context kind-kindSwitched to context "kind-kind".[root@Kind ~]# kubectl get nodesNAME STATUS ROLES AGE VERSIONkind-control-plane Readycontrol-plane 5h49m v1.27.3[root@Kind ~]# kubectl config use-context kind-kind-02Switched to context "kind-kind-02".[root@Kind ~]# kubectl get nodesNAMESTATUS ROLES AGE VERSIONkind-02-control-plane Readycontrol-plane 69m v1.27.3

7.指定镜像版本安装集群

kind create cluster --name kind-xyb --image kindest/node:v1.25.4
[root@Kind ~]# kind create cluster --name kind-xyb --image kindest/node:v1.25.3Creating cluster "kind-xyb" ... ✓ 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-xyb"You can now use your cluster with:kubectl cluster-info --context kind-kind-xybNot sure what to do next? Check out https://kind.sigs.k8s.io/docs/user/quick-start/[root@Kind ~]# kubectl config use-context kind-kind-xybSwitched to context "kind-kind-xyb".[root@Kind ~]# kubectl get nodes -ANAME STATUS ROLES AGE VERSIONkind-xyb-control-plane Readycontrol-plane 9m15s v1.25.3

8.删除Kind集群

[root@Kind ~]# kind get clusterskindkind-02[root@Kind ~]# kind delete cluster --name kind-02Deleting cluster "kind-02" ...Deleted nodes: ["kind-02-control-plane"][root@Kind ~]# kind get clusterskind[root@Kind ~]# kind delete cluster --name kindDeleting cluster "kind" ...Deleted nodes: ["kind-control-plane"][root@Kind ~]# kind get clustersNo kind clusters found.[root@Kind ~]#

9.查看kind 帮助命令

[root@Kind ~]# kind -hkind creates and manages local Kubernetes clusters using Docker container 'nodes'Usage:kind [command]Available Commands:build Build one of [node-image]completionOutput shell completion code for the specified shell (bash, zsh or fish)createCreates one of [cluster]deleteDeletes one of [cluster]exportExports one of [kubeconfig, logs]get Gets one of [clusters, nodes, kubeconfig]helpHelp about any commandloadLoads images into nodesversion Prints the kind CLI versionFlags:-h, --helphelp for kind--loglevel string DEPRECATED: see -v instead-q, --quiet silence all stderr output-v, --verbosity int32 info log verbosity, higher value produces more output--version version for kindUse "kind [command] --help" for more information about a command.

10.查看kind create帮助命令

[root@Kind ~]# kind create cluster -hCreates a local Kubernetes cluster using Docker container 'nodes'Usage:kind create cluster [flags]Flags:--config string path to a kind config file-h, --helphelp for cluster--image stringnode docker image to use for booting the cluster--kubeconfig string sets kubeconfig path instead of $KUBECONFIG or $HOME/.kube/config-n, --name string cluster name, overrides KIND_CLUSTER_NAME, config (default kind)--retainretain nodes for debugging when cluster creation fails--wait duration wait for control plane node to be ready (default 0s)Global Flags:--loglevel string DEPRECATED: see -v instead-q, --quiet silence all stderr output-v, --verbosity int32 info log verbosity, higher value produces more output

11.使用配置文件安装集群

# config.yamlkind: ClusterapiVersion: kind.x-k8s.io/v1alpha4name: xybdiynodes:- role: control-plane- role: worker- role: worker

该配置文件表示一共要创建 3 个节点,一个控制节点,两个工作节点,在创建集群的时候只需要通过 –config 参
数指定该文件即可:

kind create cluster --config config.yaml
[root@Kind ~]# kubectl create cluster --config config.yamlerror: unknown flag: --configSee 'kubectl create --help' for usage.[root@Kind ~]# kind create cluster --config config.yamlCreating cluster "xybdiy" ... ✓ Ensuring node image (kindest/node:v1.27.3)✓ Preparing nodes✓ Writing configuration✓ Starting control-plane ️ ✓ Installing CNI✓ Installing StorageClass✓ Joining worker nodes Set kubectl context to "kind-xybdiy"You can now use your cluster with:kubectl cluster-info --context kind-xybdiyHave a nice day! 
[root@Kind ~]# kubectl cluster-info --context kind-xybdiyKubernetes control plane is running at https://127.0.0.1:45025CoreDNS is running at https://127.0.0.1:45025/api/v1/namespaces/kube-system/services/kube-dns:dns/proxyTo further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.[root@Kind ~]# kind get clustersxybdiy[root@Kind ~]# kind get nodes -Axybdiy-workerxybdiy-worker2xybdiy-control-plane[root@Kind ~]# kind get clustersxybdiy[root@Kind ~]# kubectl get pods -ANAMESPACENAME READY STATUSRESTARTS AGEkube-systemcoredns-5d78c9869d-5hxw8 1/1 Running 011mkube-systemcoredns-5d78c9869d-ttwsq 1/1 Running 011mkube-systemetcd-xybdiy-control-plane1/1 Running 012mkube-systemkindnet-9ss5b1/1 Running 011mkube-systemkindnet-h875t1/1 Running 011mkube-systemkindnet-xqxsj1/1 Running 011mkube-systemkube-apiserver-xybdiy-control-plane1/1 Running 012mkube-systemkube-controller-manager-xybdiy-control-plane 1/1 Running 012mkube-systemkube-proxy-28sv8 1/1 Running 011mkube-systemkube-proxy-bb5z7 1/1 Running 011mkube-systemkube-proxy-cwd2k 1/1 Running 011mkube-systemkube-scheduler-xybdiy-control-plane1/1 Running 012mlocal-path-storage local-path-provisioner-6bc4bddd6b-t6b9l1/1 Running 011m

12.参考链接

1️⃣https://github.com/kubernetes-sigs/kind/releases

2️⃣https://github.com/kubernetes-sigs/kind/