使用 Kind 快速建立本地 Kubernetes 集群

內容概述

Kind(Kubernetes in Docker)是一個用於在本地使用 Docker 容器節點運行 Kubernetes 集群的工具。它主要用於測試 Kubernetes 本身,但也可用於本地開發或持續整合(CI)。

應用場景

  • 在開發環境中測試 Kubernetes 應用程式。
  • 在 CI 管道中進行 Kubernetes 集群的自動化測試。
  • 學習和實驗 Kubernetes 的新功能。

技術特點

  • 支持多節點(包括高可用性)集群。
  • 支持從源碼構建 Kubernetes 版本。
  • 兼容 Linux、macOS 和 Windows 平台。
  • 通過 CNCF 認證的 Kubernetes 安裝工具。

其他整合

Kind 可以與各種 CI 工具(如 GitHub Actions、Jenkins)集成,用於自動化測試流程。

安裝步驟

  1. 確保已安裝 Docker。
  2. 使用 Go 安裝 Kind:
go install sigs.k8s.io/kind@v0.26.0
  1. 將 Kind 可執行文件添加到系統的 PATH 中。

存取和操作

1. 創建集群:

kind create cluster –name test

Creating cluster “test” …
✓ Ensuring node image (kindest/node:v1.30.0) 🖼
✓ Preparing nodes 📦
✓ Writing configuration 📜
✓ Starting control-plane 🕹️
✓ Installing CNI 🔌
✓ Installing StorageClass 💾
Set kubectl context to “kind-test”
You can now use your cluster with:

kubectl cluster-info –context kind-test

Have a nice day! 👋

2. 取得設定檔:

kind export kubeconfig –name test

Set kubectl context to “kind-test”

3. 列出所有集群:

kind get clusters

test

4. 刪除集群:

kind delete cluster –name test

Deleting cluster “test” …
Deleted nodes: [“test-control-plane”]

小技巧

使用配置文件自定義集群設置,例如多節點配置。

kind create cluster –name test –config config.yaml

Creating cluster “test” …
✓ Ensuring node image (kindest/node:v1.30.0) 🖼
✓ Preparing nodes 📦 📦 📦 📦
✓ Writing configuration 📜
✓ Starting control-plane 🕹️
✓ Installing CNI 🔌
✓ Installing StorageClass 💾
✓ Joining worker nodes 🚜
Set kubectl context to “kind-test”
You can now use your cluster with:

kubectl cluster-info –context kind-test

Thanks for using kind! 😊

❯ kind get clusters
test
❯ kubectl get node
NAME STATUS ROLES AGE VERSION
test-control-plane Ready control-plane 40s v1.30.0
test-worker Ready 18s v1.30.0
test-worker2 Ready 17s v1.30.0
test-worker3 Ready 18s v1.30.0

config.yaml設定檔範例如下:

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
# One control plane node and three "workers".
#
# While these will not add more real compute capacity and
# have limited isolation, this can be useful for testing
# rolling updates etc.
#
# The API-server and other control plane components will be
# on the control-plane node.
#
# You probably don't need this unless you are testing Kubernetes itself.
nodes:
- role: control-plane
- role: worker
- role: worker
- role: worker

在創建集群時指定 Kubernetes 版本:

kind create cluster –image kindest/node:v1.31.2

Creating cluster “test2” …
✓ Ensuring node image (kindest/node:v1.31.2) 🖼
✓ Preparing nodes 📦
✓ Writing configuration 📜
✓ Starting control-plane 🕹️
✓ Installing CNI 🔌
✓ Installing StorageClass 💾
Set kubectl context to “kind-test2”
You can now use your cluster with:

kubectl cluster-info –context kind-test2

Thanks for using kind! 😊

教學影片

參考連結

使用 Kind,您可以在本地輕鬆建立和管理 Kubernetes 集群,無論是開發、測試還是學習,都能滿足需求。

返回頂端