メモ置き場

kind (Kubernetes IN Docker)

DockerコンテナをNodeとして使って、Kubernetesクラスタをローカルで動かすためのツール。CIやローカル開発用。

# インストール
$ brew install kind

# クラスタの作成。--nameオプションでクラスタ名を指定する。デフォルトは`kind`
$ kind create cluster
Creating cluster "kind" ...
 ✓ Ensuring node image (kindest/node:v1.21.1) 🖼
 ✓ 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

Thanks for using kind! 😊

# 作成済みクラスタの取得
$ kind get clusters
kind

# クラスタを作成した際にkubectlのcontextが設定されているので、kubectlコマンドでそのままアクセスできる。
$ kubectl get nodes
NAME                 STATUS   ROLES                  AGE     VERSION
kind-control-plane   Ready    control-plane,master   2m24s   v1.21.1

# クラスタの削除
$ kind delete cluster
Deleting cluster "kind" ...

# 削除された
$ kind get clusters
No kind clusters found.
# 作成するクラスタの設定をYAMLに記載する
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
name: sample-cluster
nodes:
- role: control-plane
  image: kindest/node:v1.23.0@sha256:49824ab1727c04e56a21a5d8372a402fcd32ea51ac96a2706a12af38934f81ac
- role: worker
  image: kindest/node:v1.23.0@sha256:49824ab1727c04e56a21a5d8372a402fcd32ea51ac96a2706a12af38934f81ac
- role: worker
  image: kindest/node:v1.23.0@sha256:49824ab1727c04e56a21a5d8372a402fcd32ea51ac96a2706a12af38934f81ac
- role: worker
  image: kindest/node:v1.23.0@sha256:49824ab1727c04e56a21a5d8372a402fcd32ea51ac96a2706a12af38934f81ac
# 設定を元にクラスタを作成する
$ kind create cluster --config=cluster-config.yaml
Creating cluster "sample-cluster" ...
 ✓ Ensuring node image (kindest/node:v1.23.0) 🖼
 ✓ Preparing nodes 📦 📦 📦 📦
 ✓ Writing configuration 📜
 ✓ Starting control-plane 🕹️
 ✓ Installing CNI 🔌
 ✓ Installing StorageClass 💾
 ✓ Joining worker nodes 🚜
Set kubectl context to "kind-sample-cluster"
You can now use your cluster with:

kubectl cluster-info --context kind-sample-cluster

Have a nice day! 👋

# YAMLの通り、v1.23.0の3 WorkerNodeのクラスタが作成された
$ kubectl version
Client Version: version.Info{Major:"1", Minor:"23", GitVersion:"v1.23.1", GitCommit:"86ec240af8cbd1b60bcc4c03c20da9b98005b92e", GitTreeState:"clean", BuildDate:"2021-12-16T11:33:37Z", GoVersion:"go1.17.5", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"23", GitVersion:"v1.23.0", GitCommit:"ab69524f795c42094a6630298ff53f3c3ebab7f4", GitTreeState:"clean", BuildDate:"2021-12-08T00:29:07Z", GoVersion:"go1.17.3", Compiler:"gc", Platform:"linux/amd64"}

$ kubectl get nodes
NAME                           STATUS   ROLES                  AGE     VERSION
sample-cluster-control-plane   Ready    control-plane,master   2m45s   v1.23.0
sample-cluster-worker          Ready    <none>                 2m10s   v1.23.0
sample-cluster-worker2         Ready    <none>                 2m9s    v1.23.0
sample-cluster-worker3         Ready    <none>                 2m10s   v1.23.0

参考: https://kind.sigs.k8s.io/