使用 Kind 搭建你的本地 Kubernetes 叢集
Kind 是我很喜歡也一直在參與的專案,我計劃將 Kind 相關的文章寫成一個系列。(flag++) 這是第一篇。
Kind 介紹
Kind 是 Kubernetes In Docker 的縮寫,顧名思義是使用 Docker 容器作為 Node 並將 Kubernetes 部署至其中的一個工具。官方文件中也把 Kind 作為一種本地叢集搭建的工具進行推薦。
安裝
二進位制安裝
Kind 使用 Golang 進行開發,在倉庫的 Release 頁面,已經上傳了構建好的二進位制,支援多種作業系統,可直接按需下載進行使用。
e.g.
# 下載最新的 0.2.0 版本 wget -O /usr/local/bin/kind https://github.com/kubernetes-sigs/kind/releases/download/0.2.0/kind-linux-amd64 && chmod +x /usr/local/bin/kind
通過原始碼安裝
如果你本地已經配置好了Golang 的開發環境,那你可以直接通過原始碼進行安裝。
e.g.
go get -u sigs.k8s.io/kind
執行完上述命令後,會將kind
的可執行檔案放到$(go env GOPATH)/bin
資料夾內,你可能需要將此目錄加入到$PATH
中。
或者也可以先 clone 原始碼再通過go build
進行構建。
依賴
kubectl
搭建單節點叢集
以下的演示均使用最新的程式碼(即通過原始碼安裝)。
基礎用法
搭建單節點叢集是 Kind 最基礎的功能。
e.g.
master $ kind create cluster --name moelove Creating cluster "moelove" ... ✓ Ensuring node image (kindest/node:v1.13.4) ✓ Preparing nodes :package: ✓ Creating kubeadm config :scroll: ✓ Starting control-plane️ Cluster creation complete. You can now use the cluster with: export KUBECONFIG="$(kind get kubeconfig-path --name="moelove")" kubectl cluster-info
以上命令中,--name
是可選引數,如不指定,預設創建出來的叢集名字為kind
。
我們根據命令執行完的輸出進行操作:
master $ export KUBECONFIG="$(kind get kubeconfig-path --name="moelove")" master $ kubectl cluster-info Kubernetes master is running at https://localhost:34458 KubeDNS is running at https://localhost:34458/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'. master $ kubectl get nodes NAMESTATUSROLESAGEVERSION moelove-control-planeReadymaster2mv1.13.4
以上命令中,kind get kubeconfig-path --name="moelove"
會返回該指定叢集配置檔案所在的路徑。
可以看到單節點的 Kubernetes 已經搭建成功。
注意
-
預設情況下,Kind 會先下載
kindest/node:v1.13.4
映象,該映象目前託管於 Docker Hub 上,下載時間取決於網路狀況。 -
Kind 實際使用
kubeadm
進行叢集的建立,對kubeadm
有所瞭解的人都知道它預設使用的映象在國內下載不到,所以需要自己解決網路問題。或者參考下面的方式:
Kind 在建立叢集的時候,支援通過--config
的引數傳遞配置檔案給 Kind,在國內,我們可以通過使用國內映象源的方式來加速叢集的建立。(這個方法也適用於直接通過 kubeadm 搭建 Kubernetes 叢集)
我們先通過以下命令刪除剛才搭建的叢集:
master $ kind deletecluster --name moelove Deleting cluster "moelove" ... $KUBECONFIG is still set to use /root/.kube/kind-config-moelove even though that file has been deleted, remember to unset it
接下來,將下面的配置內容儲存至一個 YAML 檔案中,比如名為kind-config.yaml
kind: Cluster apiVersion: kind.sigs.k8s.io/v1alpha3 kubeadmConfigPatches: - | apiVersion: kubeadm.k8s.io/v1beta1 kind: ClusterConfiguration metadata: name: config networking: serviceSubnet: 10.0.0.0/16 imageRepository: registry.aliyuncs.com/google_containers nodeRegistration: kubeletExtraArgs: pod-infra-container-image: registry.aliyuncs.com/google_containers/pause:3.1 - | apiVersion: kubeadm.k8s.io/v1beta1 kind: InitConfiguration metadata: name: config networking: serviceSubnet: 10.0.0.0/16 imageRepository: registry.aliyuncs.com/google_containers nodes: - role: control-plane
我們使用該配置檔案搭建叢集。
master $ kind create cluster --name moelove --config kind.yaml Creating cluster "moelove" ... ✓ Ensuring node image (kindest/node:v1.13.4) ✓ Preparing nodes :package: ✓ Creating kubeadm config :scroll: ✓ Starting control-plane️ Cluster creation complete. You can now use the cluster with: export KUBECONFIG="$(kind get kubeconfig-path --name="moelove")" kubectl cluster-info
上面通過--config
將我們的配置檔案傳遞給 Kind 用於搭建叢集,推薦國內使用者使用這種方式
。
搭建高可用叢集
Kind 也支援搭建高可用的 K8S 叢集,不過只能通過配置檔案來實現。可以直接將下面的內容儲存至檔案中,再將配置檔案傳遞給 Kind 即可。
e.g.
kind: Cluster apiVersion: kind.sigs.k8s.io/v1alpha3 kubeadmConfigPatches: - | apiVersion: kubeadm.k8s.io/v1beta1 kind: ClusterConfiguration metadata: name: config networking: serviceSubnet: 10.0.0.0/16 imageRepository: registry.aliyuncs.com/google_containers nodeRegistration: kubeletExtraArgs: pod-infra-container-image: registry.aliyuncs.com/google_containers/pause:3.1 - | apiVersion: kubeadm.k8s.io/v1beta1 kind: InitConfiguration metadata: name: config networking: serviceSubnet: 10.0.0.0/16 imageRepository: registry.aliyuncs.com/google_containers nodes: - role: control-plane - role: control-plane - role: control-plane - role: worker - role: worker - role: worker
我們使用以下的命令來搭建高可用的 Kubernetes 叢集:
master $ kind create cluster --name moelove-ha --config kind-ha-config.yaml Creating cluster "moelove-ha" ... ✓ Ensuring node image (kindest/node:v1.13.4) ✓ Preparing nodes :package::package::package::package::package::package::package: ✓ Starting the external load balancer ⚖️ ✓ Creating kubeadm config :scroll: ✓ Starting control-plane️ ✓ Joining more control-plane nodes :video_game: ✓ Joining worker nodes :tractor: Cluster creation complete. You can now use the cluster with: export KUBECONFIG="$(kind get kubeconfig-path --name="moelove-ha")" kubectl cluster-info master $ export KUBECONFIG="$(kind get kubeconfig-path --name="moelove-ha")" master $ kubectl cluster-info Kubernetes master is running at https://localhost:44019 KubeDNS is running at https://localhost:44019/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
可以做下簡單的驗證:
master $ kubectl get nodes NAMESTATUSROLESAGEVERSION moelove-ha-control-planeReadymaster3m42sv1.13.4 moelove-ha-control-plane2Readymaster3m24sv1.13.4 moelove-ha-control-plane3Readymaster2m13sv1.13.4 moelove-ha-workerReady<none>96sv1.13.4 moelove-ha-worker2Ready<none>98sv1.13.4 moelove-ha-worker3Ready<none>95sv1.13.4
可以看到已經成功建立了多 master 的 Kubernetes 叢集。
總結
這是使用 Kind 搭建本地 Kubernetes 叢集的第一篇,同時本篇的內容也是《Kubernetes 從上手到實踐》 第 4 節內容的補充,搭配食用效果更佳 :)
可以通過下面二維碼訂閱我的文章公眾號【MoeLove】