Kind 是我很喜歡也一直在參與的項目,我計劃將 Kind 相關的文章寫成一個系列。(flag++) 這是第一篇。node
Kind 是 Kubernetes In Docker 的縮寫,顧名思義是使用 Docker 容器做爲 Node 並將 Kubernetes 部署至其中的一個工具。官方文檔中也把 Kind 做爲一種本地集羣搭建的工具進行推薦。linux
Kind 使用 Golang 進行開發,在倉庫的 Release 頁面,已經上傳了構建好的二進制,支持多種操做系統,可直接按需下載進行使用。git
e.g.github
# 下載最新的 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 的開發環境,那你能夠直接經過源碼進行安裝。golang
e.g.docker
go get -u sigs.k8s.io/kind
複製代碼
運行完上述命令後,會將 kind
的可執行文件放到 $(go env GOPATH)/bin
文件夾內,你可能須要將此目錄加入到 $PATH
中。api
或者也能夠先 clone 源代碼再經過 go build
進行構建。網絡
Kind 的主要功能目前須要有 Docker 環境的支持,可參考 Docker 官方文檔進行安裝。工具
若是須要操做集羣,則須要安裝 kubectl
命令行。安裝方法可參考官方文檔ui
如下的演示均使用最新的代碼(即經過源碼安裝)。
搭建單節點集羣是 Kind 最基礎的功能。
e.g.
master $ kind create cluster --name moelove
Creating cluster "moelove" ...
✓ Ensuring node image (kindest/node:v1.13.4) 🖼
✓ Preparing nodes 📦
✓ Creating kubeadm config 📜
✓ 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
NAME STATUS ROLES AGE VERSION
moelove-control-plane Ready master 2m v1.13.4
複製代碼
以上命令中,kind get kubeconfig-path --name="moelove"
會返回該指定集羣配置文件所在的路徑。
能夠看到單節點的 Kubernetes 已經搭建成功。
kindest/node:v1.13.4
鏡像,該鏡像目前託管於 Docker Hub 上,下載時間取決於網絡情況。kubeadm
進行集羣的建立,對 kubeadm
有所瞭解的人都知道它默認使用的鏡像在國內下載不到,因此須要本身解決網絡問題。或者參考下面的方式:Kind 在建立集羣的時候,支持經過 --config
的參數傳遞配置文件給 Kind,在國內,咱們能夠經過使用國內鏡像源的方式來加速集羣的建立。(這個方法也適用於直接經過 kubeadm 搭建 Kubernetes 集羣)
咱們先經過如下命令刪除剛纔搭建的集羣:
master $ kind delete cluster --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 📦
✓ Creating kubeadm config 📜
✓ 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 📦📦📦📦📦📦📦
✓ Starting the external load balancer ⚖️
✓ Creating kubeadm config 📜
✓ Starting control-plane 🕹️
✓ Joining more control-plane nodes 🎮
✓ Joining worker nodes 🚜
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
NAME STATUS ROLES AGE VERSION
moelove-ha-control-plane Ready master 3m42s v1.13.4
moelove-ha-control-plane2 Ready master 3m24s v1.13.4
moelove-ha-control-plane3 Ready master 2m13s v1.13.4
moelove-ha-worker Ready <none> 96s v1.13.4
moelove-ha-worker2 Ready <none> 98s v1.13.4
moelove-ha-worker3 Ready <none> 95s v1.13.4
複製代碼
能夠看到已經成功建立了多 master 的 Kubernetes 集羣。
這是使用 Kind 搭建本地 Kubernetes 集羣的第一篇,同時本篇的內容也是《Kubernetes 從上手到實踐》第 4 節內容的補充,搭配食用效果更佳 :)
能夠經過下面二維碼訂閱個人文章公衆號【MoeLove】