Create a Local Kubernetes Cluster with kind
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).
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.
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.
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
- Setup Kubernetes cluster monitoring the easy way: installs Prometheus and Grafana on any cluster; this lesson's multi-node cluster gives it the several nodes it asks for.