Sub topic 3
Creating a Cluster with kind
This chapter assumes you have a working container runtime such as Docker, kubectl installed, and the kind binary on your PATH. It also assumes you can read YAML.
How kind works
kind stands for "Kubernetes in Docker". It runs each Kubernetes node as a container. The container holds the kubelet, a container runtime, and the control-plane components for that node. No virtual machine is involved, so a cluster starts in under a minute on most machines.
kind writes an entry into your kubeconfig file when it creates a cluster. The cluster name you choose determines the context name: a cluster named dev gets the context kind-dev.
Create the cluster
kind create cluster --name dev
kubectl cluster-info --context kind-dev
kubectl get nodesThe first run downloads the node image, which is large, so it takes longer than later runs. When the command finishes, kubectl get nodes should show one node named dev-control-plane with the status Ready.
Without --name, kind uses the name kind and the context kind-kind.
Add a configuration file
The default cluster has one node. To change its shape, pass a configuration file. This one adds a worker node and maps host port 8080 to port 30080 on the control-plane container. Port 30080 is a NodePort that a Service can use later.
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
extraPortMappings:
- containerPort: 30080
hostPort: 8080
protocol: TCP
- role: workerSave it as kind-config.yaml and create the cluster from it:
kind create cluster --name dev --config kind-config.yamlkind refuses to create a cluster if one with the same name already exists. Delete the old one first with kind delete cluster --name dev.
Common errors
| Symptom | Likely cause |
|---|---|
| `failed to create cluster` or a Docker connection error | The Docker daemon is not running, or your user cannot access it |
| `node(s) already exist for a cluster with the name "dev"` | A cluster with that name exists. Delete it or pick another name |
| `kubectl` talks to the wrong cluster | The active context is not `kind-dev`. Pass `--context kind-dev` or switch contexts |
| Port mapping does not work | The mapping can only be set at creation time. Recreate the cluster with the updated config |
Exercise
You run kind create cluster --name demo. What is the kubectl context name, and how would you check that the node is ready without switching contexts?
Show answer
The context is kind-demo. Run kubectl get nodes --context kind-demo and check that the node demo-control-plane has the status Ready.