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-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叢集的內幕
先檢視多出來的docker映象和容器:
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
建立過程大概是:先獲取映象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程式的完整用法
- 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認證相容的安裝工具之一,大家可放心使用。