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集羣?
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
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-01
Have a nice day! 👋
至此已得到一個可用的k8s集羣了:
root@e5pc-vm-01:~# kubectl get nodes
NAME STATUS ROLES AGE VERSION
myk8s-01-control-plane Ready control-plane,master 66s v1.21.1
kind創建k8s集羣的內幕
root@e5pc-vm-01:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 2b4cba85892a 2 days ago 72.8MB
kindest/node <none> 32b8b755dee8 9 months ago 1.12GB
root@e5pc-vm-01:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b4bde05b0190 kindest/node:v1.21.1 "/usr/local/bin/entr…" 12 minutes ago Up 12 minutes 127.0.0.1:42267->6443/tcp myk8s-01-control-plane
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程序的完整用法
- 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: worker
root@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集羣時非常有幫助的工具。它啓動快速,資源消耗很低,並且它也是CNCF認證兼容的安裝工具之一,大家可放心使用。