Course contents · Lesson 17 of 22

Create a Local Kubernetes Cluster with kind

Hands-On with kind · Lesson 17 of 22 · 12 min Hands-on

Every Kubernetes lesson so far said "read along for now": you had no cluster. A cloud cluster costs money, and nobody wants to practise on their team's real one. In this lesson you build a local Kubernetes cluster with kind on your own laptop, for free, look inside it, then delete it and make another. Every later lab uses it.

What kind is: Kubernetes in Docker

kind stands for Kubernetes IN Docker: a free, open-source tool from the Kubernetes project, designed first for testing Kubernetes itself. It makes each cluster node, a machine that runs pods, out of one Docker container.

The node container runs what any node from lesson 8 runs: the kubelet and containerd as services, and, on a control-plane node, the API server, etcd, scheduler and controller manager as pods. Your pods are containers nested inside it. To allow that, Docker runs it privileged, with far more access to your machine than an ordinary container gets.

The API server listens on port 6443 inside the node, and Docker publishes that port on 127.0.0.1 at a random free port, so only your laptop can reach it. In the diagram, notice that the whole cluster is one container, inside Docker's hidden Linux VM on a Mac or Windows laptop (lesson 1).

Architecture diagram. Your laptop holds kubectl, which reads the context kind-lab from ~/.kube/config, and Docker, which on a Mac or Windows laptop runs in a hidden Linux VM. Inside Docker is one container, lab-control-plane, from the image kindest/node:v1.37.0, acting as one Kubernetes node. Inside it: kube-apiserver listening on port 6443, the control plane pods etcd, kube-scheduler and kube-controller-manager, the kubelet and containerd as services that start every pod, and other pods such as CoreDNS, kindnet, kube-proxy and later your apps. kubectl sends HTTPS to 127.0.0.1 at a random port, forwarded to port 6443
The whole cluster is one container, yet kubectl talks to it as to any real cluster.

A new cluster is ready in a minute or two once the node image is downloaded, and deleting the container deletes it. On a single node, kind removes the control-plane taint, a marker that normally keeps app pods off control-plane nodes, so that node runs your apps too.

Install Docker, kubectl and kind

Docker runs the node container: Docker Desktop on macOS and Windows, Docker Engine on Linux, with at least 2 CPUs and 4 GB of memory for it. kubectl talks to the API server. kind creates and deletes clusters. Install kubectl from the Kubernetes tools page and kind from its quick start.

This course was checked with kind v0.33.0. Each kind release has a default node image, the image node containers start from; for v0.33.0 it is kindest/node:v1.37.0, Kubernetes 1.37.0. Package managers install the newest kind; to match exactly, download v0.33.0. Then check each tool:

docker --version
kubectl version --client
kind --version

You should see a line starting Docker version, then Client Version: v1.37.0 or near it plus a Kustomize Version line, then kind version 0.33.0. kubectl has no --version flag, and it is supported within one minor version of the API server, so 1.36 to 1.38 works. The usual failure is command not found: open a new terminal or recheck your PATH.

Create the cluster and meet your kubeconfig

First check that Docker is running: docker info must finish without an error saying it cannot connect to the Docker daemon. Then create a cluster called lab. Without --name, kind calls it kind. The first run downloads the node image, which can take a few minutes:

kind create cluster --name lab

You should see each step ticked off. This is kind v0.33.0's output, with its small icons trimmed; the last line is a friendly message picked at random:

Creating cluster "lab" ...
 ✓ Ensuring node image (kindest/node:v1.37.0)
 ✓ Preparing nodes
 ✓ Writing configuration
 ✓ Starting control-plane
 ✓ Installing CNI
 ✓ Installing StorageClass
Set kubectl context to "kind-lab"
You can now use your cluster with:

kubectl cluster-info --context kind-lab

Have a nice day!

The diagram explains each line. kubeadm is the official tool that bootstraps a cluster: it creates certificates and starts the control plane. The CNI (Container Network Interface) plugin, kindnet, gives pods IP addresses and connects them. The StorageClass serves PersistentVolumeClaims from lesson 14.

Step-by-step diagram in six numbered boxes, each with the line kind prints. 1, pull the node image kindest/node:v1.37.0, downloaded the first time only: Ensuring node image. 2, start the node container, docker run creates lab-control-plane: Preparing nodes. 3, write a kubeadm config file inside the node: Writing configuration. 4, kubeadm init starts the API server, etcd, scheduler and controller manager: Starting control-plane. 5, add the kindnet pod network and local-path storage: Installing CNI and Installing StorageClass. 6, write the kubeconfig cluster, user and context kind-lab and make it current: Set kubectl context. A note: kind then returns without waiting for Ready, so the node may show NotReady for a few seconds; add --wait 60s to make kind wait
Only the last step touches your own files.

That step edits your kubeconfig, lesson 8's file telling kubectl which cluster to use: ~/.kube/config, unless the KUBECONFIG environment variable names another. kind adds three entries named kind-lab: a cluster (the API server's address and certificate), a user (credentials) and a context pairing them. It makes kind-lab the current context, the one kubectl uses by default:

kubectl config current-context

It should print kind-lab; kubectl config get-contexts lists every context, starring the current one. If kind instead stopped with node(s) already exist for a cluster with the name "lab", you made one earlier: use it, or delete it as shown below.

First look inside the cluster

Start with the nodes. This lists every node in the cluster and says whether each one is ready to run pods:

kubectl get nodes

You should see one node, the container from the diagram, with ROLES control-plane and the kubelet's version; your age will differ:

NAME                STATUS   ROLES           AGE   VERSION
lab-control-plane   Ready    control-plane   62s   v1.37.0

If STATUS says NotReady, wait half a minute and retry: kind returns before the pod network is up unless you add --wait 60s when creating. Next, list pods in every namespace; -A means --all-namespaces:

kubectl get pods -A

You should see lesson 8's control plane as pods named after their node, plus kind's helpers. Suffixes and ages will differ:

NAMESPACE            NAME                                        READY   STATUS    RESTARTS   AGE
kube-system          coredns-7d764666f9-4xk2m                    1/1     Running   0          70s
kube-system          coredns-7d764666f9-9pqzt                    1/1     Running   0          70s
kube-system          etcd-lab-control-plane                      1/1     Running   0          78s
kube-system          kindnet-5w2rx                               1/1     Running   0          70s
kube-system          kube-apiserver-lab-control-plane            1/1     Running   0          78s
kube-system          kube-controller-manager-lab-control-plane   1/1     Running   0          78s
kube-system          kube-proxy-hx8nq                            1/1     Running   0          70s
kube-system          kube-scheduler-lab-control-plane            1/1     Running   0          78s
local-path-storage   local-path-provisioner-7dc846544d-q7lzd     1/1     Running   0          70s

kindnet is the network plugin, kube-proxy writes Service rules, CoreDNS answers lesson 11's DNS names, and local-path-provisioner creates volumes. Finally, ask where the API server is:

kubectl cluster-info

You should see an address on your own laptop; the port is random, so yours will differ:

Kubernetes control plane is running at https://127.0.0.1:41539
CoreDNS is running at https://127.0.0.1:41539/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

docker ps shows one container, lab-control-plane, with 127.0.0.1:41539->6443/tcp under PORTS: the same port. If kubectl says The connection to the server 127.0.0.1:41539 was refused, that container has stopped, perhaps after a restart; delete the cluster and create it again.

Delete, recreate and add nodes

A kind cluster is disposable, so deleting one is routine:

kind delete cluster --name lab

You should see Deleting cluster "lab" ..., then Deleted nodes: ["lab-control-plane"]. kind removes the container and the kind-lab kubeconfig entries, leaving no current context if it was current. Running it twice is safe: the second run prints only the first line and no error. Recreating is faster, as the image is already downloaded.

Optionally, a kind config file, YAML describing the cluster, can add nodes; the lab's multi-node.yaml asks for one control-plane node and two workers:

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker

Run kind create cluster --name multi --config multi-node.yaml. kind switches kubectl to kind-multi, and kubectl get nodes lists multi-control-plane, multi-worker and multi-worker2, the workers with ROLES <none>. Then run kind delete cluster --name multi and kubectl config use-context kind-lab.

If a create fails, follow the flowchart. When Docker, the name (kind get clusters lists yours), memory and ports are fine, --retain keeps the failed node container, and kind export logs --name lab copies its logs to a folder whose path it prints.

Flowchart. kind create cluster fails. Check 1, is Docker running, with docker info printing no error? No: start Docker Desktop or the Docker service, then retry. Yes: check 2, is the name taken, with kind get clusters listing lab? Yes: kind delete cluster --name lab, then create it again. No: check 3, enough memory and CPU? No: give Docker at least 2 CPUs and 4 GB of memory. Yes: check 4, is a port fixed in your config in use? Yes: free it or choose another. No: still failing, so keep the evidence with kind create cluster --name lab --retain and kind export logs --name lab, read the logs, then delete the cluster
Cheap checks come first; logs are for when they all pass.

Check yourself

Straight after kind create cluster, kubectl get nodes shows NotReady. Is the cluster broken?

Probably not. kind returns before the pod network is running unless you pass --wait, and a node stays NotReady until its network plugin is up, so wait and check again.

You deployed an app to lab, then created multi. Now kubectl get pods finds no pods. Where did your app go?

Nowhere: it is still in lab. Creating multi switched kubectl's current context to kind-multi, an empty cluster. kubectl config use-context kind-lab points kubectl back.

A teammate says kind is only a simulator, so nothing learned on it carries over to EKS. Why are they wrong, and what does differ?

The node runs the real kubelet, containerd, API server and etcd, so objects and commands behave the same. The surroundings differ: with no cloud load balancer, a LoadBalancer Service stays pending (lesson 11).

Go deeper