簡介
kind是另一個Kubernetes SIG項目,但它與minikube有很大區別。它可以將集羣遷移到Docker容器中,這與生成虛擬機相比,啓動速度大大加快。簡而言之,kind是一個使用Docker容器節點運行本地Kubernetes集羣的工具(CLI)。
接下來,我們開始吧!
前期準備
想要順利完成本教程,你需要在本地系統中準備好以下程序:
- Go
- 需要運行的Docker服務
安裝
使用以下命令下載和安裝kind
二進制文件:
GO111MODULE=”on” go get sigs.k8s.io/[email protected]
確保kind二進制文件是存在的
> kind version
kind v0.8.1 go1.14.2 darwin/amd64
現在,我們應該能夠使用kind
CLI來啓動一個Kubernetes集羣:
Usage:
kind [command]Available Commands:
build Build one of [node-image]
completion Output shell completion code for the specified shell
create Creates one of [cluster]
delete Deletes one of [cluster]
export Exports one of [kubeconfig, logs]
get Gets one of [clusters, nodes, kubeconfig]
help Help about any command
load Loads images into nodes
version Prints the kind CLI version
在本文中,我們將聚焦於create
、get
和delete
命令。
創建一個集羣
執行以下命令即可創建一個集羣:
kind create cluster
> kind create cluster
Creating cluster "kind" ...
✓ Ensuring node image (kindest/node:v1.18.2) 🖼
✓ Preparing nodes 📦
✓ Writing configuration 📜
✓ Starting control-plane 🕹️
✓ Installing CNI 🔌
✓ Installing StorageClass 💾
Set kubectl context to "kind-kind"
You can now use your cluster with:kubectl cluster-info --context kind-kind Have a nice day! 👋
將通過拉取最新的Kubernetes節點(v 1.18.2)來創建一個Kubernetes集羣。剛剛我們已經創建了一個v 1.18.2的Kubernetes集羣。
在創建集羣的過程中如果我們沒有--name
參數,那麼集羣名稱將會默認設置爲kind。
創建特定版本的K8S集羣
我們可以傳–image參數來部署一個特定版本的Kubernetes集羣。
使用的命令爲:
kind create cluster --image kindest/node:v1.15.6
> kind create cluster --image kindest/node:v1.15.6 --name kind-1.15.6
Creating cluster "kind" ...
✓ Ensuring node image (kindest/node:v1.15.6) 🖼
✓ Preparing nodes 📦
✓ Writing configuration 📜
✓ Starting control-plane 🕹️
✓ Installing CNI 🔌
✓ Installing StorageClass 💾
Set kubectl context to "kind-kind"
You can now use your cluster with:kubectl cluster-info --context kind-kind Have a nice day! 👋
列出部署的集羣
輸入命令:kind get clusters
> kind get clusters
kind
kind-1.15.6
這應該列出我們此前創建的兩個不同K8S版本的集羣。
爲kubectl設置上下文
創建集羣之後,kubectl會指出最近創建的K8S集羣。
讓我們來檢查一下所有可用的上下文。
> kubectl config get-contexts
CURRENT NAME CLUSTER
kind-kind kind-kind
* kind-kind-1.15.6 kind-kind-1.15.6
從輸出中,我們可以得到結論,kubectl上下文目前已經被設置爲最新的集羣,即kind-1.15.6。(上下文名稱是以kind爲前綴的)
要將kubectl上下文設置爲版本是1.18.2的kind集羣,我們需要進行如下操作:
> kubectl config set-context kind-kind
Context "kind-kind" modified.
要驗證kubectl是否指向正確的集羣,我們需要檢查節點:
> kubectl get nodes
NAME STATUS ROLES AGE VERSION
kind-1.18.2-control-plane Ready master 8m20s v1.18.2
刪除某個集羣
要刪除一個特定的羣集,可以在--name
參數中把集羣名稱傳遞給刪除命令。
命令爲:kind delete cluster --name kind
> kind delete cluster --name kind
Deleting cluster "kind" ...
刪除所有集羣
如果你想一次性刪除所有集羣,請執行:
kind delete clusters –all
> kind delete clusters --all
Deleted clusters: ["kind-1.15.6"]
kind的優勢是什麼?
kind(Kubernetes in Docker)是一個基於Docker構建的Kubernetes集羣的工具。它經過CNCF認證,並且支持多節點集羣,包括高可用集羣。並且支持Linux、macOS以及Windows操作系統,操作簡單,學習成本低,非常適合用來在本地搭建基於Kubernetes的開發/測試環境。
本文轉載自公衆號RancherLabs(ID:RancherLabs)。
原文鏈接: