使用kind快速搭建本地k8s集群

Kind是什么?
k8s集群的组成比较复杂,如果纯手工部署的话易出错且时间成本高。而本文介绍的Kind工具,能够快速的建立起可用的k8s集群,降低初学者的学习门槛。 Kind是Kubernetes In Docker的缩写,顾名思义,看起来是把k8s放到docker的意思。没错,kind创建k8s集群的基本原理就是:提前准备好k8s节点的镜像,通过docker启动容器,来模拟k8s的节点,从而组成完整的k8s集群。需要注意,kind创建的集群仅可用于开发、学习、测试等,不能用于生产环境。 kind有什么特点?

  • 创建、启动k8s集群非常快速,资源消耗较低。
  • 支持创建多节点的k8s集群,包括高可用模式。
  • kind 支持 Linux, macOS and Windows
  • 它是CNCF认证的k8s集群安装方式之一
如何安装kind?
kind表现上就是一个二进制程序,下载对应版本并增加执行权限即可:
curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.11.1/kind-linux-amd64 chmod +x ./kind mv ./kind /usr/bin/kind kind version

如何通过kind新建k8s集群?
kubectl是与k8s交互的客户端命令工具,因此需要先安装此工具。
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl kubectl version --client

然后通过一行命令就能够快速的创建k8s集群:
root@e5pc-vm-01:~# kind create cluster --name myk8s-01


Creating cluster "myk8s-01" ... ? Ensuring node image (kindest/node:v1.21.1) ? Preparing nodes ? Writing configuration ? Starting control-plane ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ? Installing CNI ? Installing StorageClass Set kubectl context to "kind-myk8s-01" You can now use your cluster with:kubectl cluster-info --context kind-myk8s-01Have a nice day!

至此已得到一个可用的k8s集群了:
root@e5pc-vm-01:~# kubectl get nodes NAMESTATUSROLESAGEVERSION myk8s-01-control-planeReadycontrol-plane,master66sv1.21.1

kind创建k8s集群的内幕
先查看多出来的docker镜像和容器:
root@e5pc-vm-01:~# docker images REPOSITORYTAGIMAGE IDCREATEDSIZE ubuntulatest2b4cba85892a2 days ago72.8MB kindest/node32b8b755dee89 months ago1.12GB


root@e5pc-vm-01:~# docker ps CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES b4bde05b0190kindest/node:v1.21.1"/usr/local/bin/entr…"12 minutes agoUp 12 minutes127.0.0.1:42267->6443/tcpmyk8s-01-control-plane

创建过程大概是:先获取镜像kindest/node:v1.21.1,然后启动容器myk8s-01-control-plane,启动的容器就是这个k8s集群的master节点,显然此集群只有master节点。 同时,kind create cluster命令还是将此新建的k8s集群的连接信息写入当前用户(root)的kubectl配置文件中,从而让kubectl能够与集群交互。配置文件内容如下:
root@e5pc-vm-01:~# cat.kube/config apiVersion: v1 clusters: - cluster: certificate-authority-data: xxxx server: https://127.0.0.1:42267 name: kind-myk8s-01 contexts: - context: cluster: kind-myk8s-01 user: kind-myk8s-01 name: kind-myk8s-01 current-context: kind-myk8s-01 kind: Config preferences: {} users: - name: kind-myk8s-01 user: client-certificate-data: xxxx client-key-data: xxxx

kind创建k8s集群的架构图为:
使用kind快速搭建本地k8s集群
文章图片


kind程序的完整用法
  • kind create cluster
    • --image 指定node镜像名称,默认是kindest/node
    • --name 指定创建集群的名称
    • --config kind-example-config.yaml
    • --kubeconfig string 指定生成的kubeconfig的文件路径。默认在$KUBECONFIG or $HOME/.kube/config
    • 常用:kind create cluster --config=xxxcfg --name=xxxname
  • kind delete cluster xxxx
  • kind get clusters:查看kind创建所有的k8s集群
  • kind get kubeconfig --name 集群name【必须填写--name参数,默认name是kind】
  • kind completion
  • kind export kubeconfig --name=xxx --kubeconfig=kubeconfigpath
    • 导出kind中的k8s集群的kubeconfig连接配置文件
kind的高级用法
1、通过配置文件创建k8s集群 前面展示的集群创建操作是最简单的单master集群,kind当然可以创建更完整的集群。这就需要用到配置文件模式来创建集群:
root@e5pc-vm-01:~# cat kindcfg-tccc.biz.yml kind: Cluster apiVersion: kind.x-k8s.io/v1alpha4 nodes: - role: control-plane - role: worker - role: worker - role: workerroot@e5pc-vm-01:~# kind create cluster --name myk8s-02 --config./kindcfg-tccc.biz.yml

2、设置新建的k8s集群从私有http仓库加载镜像 配置文件如下:
kind: Cluster apiVersion: kind.x-k8s.io/v1alpha4 containerdConfigPatches: - |- [plugins."io.containerd.grpc.v1.cri".registry.mirrors."harbor.xxxx.top:60666"] endpoint = ["http://harbor.xxxx.top:60666"] nodes: - role: control-plane - role: worker

3、完整的配置文件示例 地址:https://raw.githubusercontent.com/kubernetes-sigs/kind/main/site/content/docs/user/kind-example-config.yaml
# this config file contains all config fields with comments # NOTE: this is not a particularly useful config file kind: Cluster apiVersion: kind.x-k8s.io/v1alpha4 # patch the generated kubeadm config with some extra settings kubeadmConfigPatches: - | apiVersion: kubelet.config.k8s.io/v1beta1 kind: KubeletConfiguration evictionHard: nodefs.available: "0%" # patch it further using a JSON 6902 patch kubeadmConfigPatchesJSON6902: - group: kubeadm.k8s.io version: v1beta2 kind: ClusterConfiguration patch: | - op: add path: /apiServer/certSANs/- value: my-hostname # 1 control plane node and 3 workers nodes: # the control plane node config - role: control-plane # the three workers - role: worker - role: worker - role: worker

总结
【使用kind快速搭建本地k8s集群】kind是学习和测试k8s集群时非常有帮助的工具。它启动快速,资源消耗很低,并且它也是CNCF认证兼容的安装工具之一,大家可放心使用。
参考资料
  • 官方文档:https://kind.sigs.k8s.io/
  • GitHub主页:https://github.com/kubernetes-sigs/kind/

    推荐阅读