In this article we will talk about how we can configure our kind cluster in a declarative way

Prerequisites
If you haven’t read my previous articles on setting up a kind cluster and loading Docker images to cluster, please check below
Introduction
- kind cluster can be created and configured in a declarative way
- We can define our desired state for kind cluster in a YAML file
- YAML file follows Kubernetes conventions and versioning
- Parameters passed in the CLI take precedence over their equivalent in config file
Usage
Simple Cluster
Creating a simple cluster having one control plane and two worker nodes
$ cat kind.yml
apiVersion: kind.x-k8s.io/v1alpha4
kind: Cluster
name: dev
nodes:
- role: control-plane
- role: worker
- role: worker
$ kind create cluster --config kind.yml
Creating cluster "dev" ...
✓ Ensuring node image (kindest/node:v1.26.3) 🖼
✓ Preparing nodes 📦 📦 📦
✓ Writing configuration 📜
✓ Starting control-plane 🕹️
✓ Installing CNI 🔌
✓ Installing StorageClass 💾
✓ Joining worker nodes 🚜
Set kubectl context to "kind-dev"
You can now use your cluster with:
kubectl cluster-info --context kind-dev
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
dev-control-plane Ready control-plane 53s v1.26.3
dev-worker Ready <none> 16s v1.26.3
dev-worker2 Ready <none> 29s v1.26.3
HA Cluster
Creating a cluster having multiple control planes for high availability
$ cat kind.yml
apiVersion: kind.x-k8s.io/v1alpha4
kind: Cluster
name: dev
nodes:
- role: control-plane
- role: control-plane
- role: control-plane
- role: worker
- role: worker
- role: worker
$ kind create cluster --config kind.yml
Creating cluster "dev" ...
✓ Ensuring node image (kindest/node:v1.26.3) 🖼
✓ Preparing nodes 📦 📦 📦 📦 📦 📦
✓ Configuring the external load balancer ⚖️
✓ Writing configuration 📜
✓ Starting control-plane 🕹️
✓ Installing CNI 🔌
✓ Installing StorageClass 💾
✓ Joining more control-plane nodes 🎮
✓ Joining worker nodes 🚜
Set kubectl context to "kind-dev"
You can now use your cluster with:
kubectl cluster-info --context kind-dev
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
dev-control-plane Ready control-plane 2m32s v1.26.3
dev-control-plane2 Ready control-plane 2m18s v1.26.3
dev-control-plane3 Ready control-plane 74s v1.26.3
dev-worker Ready <none> 56s v1.26.3
dev-worker2 Ready <none> 56s v1.26.3
dev-worker3 Ready <none> 68s v1.26.3
Changing Kubernetes version
We can change the Kubernetes version by setting the node’s container image and it’s mandatory to include sha256 value with image
$ cat kind.yml
apiVersion: kind.x-k8s.io/v1alpha4
kind: Cluster
name: dev
nodes:
- role: control-plane
image: kindest/node:v1.27.0@sha256:c6b22e613523b1af67d4bc8a0c38a4c3ea3a2b8fbc5b367ae36345c9cb844518
- role: worker
image: kindest/node:v1.27.0@sha256:c6b22e613523b1af67d4bc8a0c38a4c3ea3a2b8fbc5b367ae36345c9cb844518
- role: worker
image: kindest/node:v1.27.0@sha256:c6b22e613523b1af67d4bc8a0c38a4c3ea3a2b8fbc5b367ae36345c9cb844518
$ kind create cluster --config kind.yml
Creating cluster "dev" ...
✓ Ensuring node image (kindest/node:v1.27.0) 🖼
✓ Preparing nodes 📦 📦 📦
✓ Writing configuration 📜
✓ Starting control-plane 🕹️
✓ Installing CNI 🔌
✓ Installing StorageClass 💾
✓ Joining worker nodes 🚜
Set kubectl context to "kind-dev"
You can now use your cluster with:
kubectl cluster-info --context kind-dev
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
dev-control-plane Ready control-plane 61s v1.27.0
dev-worker Ready <none> 42s v1.27.0
dev-worker2 Ready <none> 41s v1.27.0
Cleanup
Delete the cluster after use
$ kind delete cluster --name dev
Deleting cluster "dev" ...
Deleted nodes: ["dev-worker2" "dev-worker" "dev-control-plane"]