Desired state and self-healing
You don't tell Kubernetes what to do. You tell it what you want, and it keeps checking. Make a promise, break it on purpose, and watch it get kept.
Most tools take orders: start this, stop that. Kubernetes takes promises. You say “I want three copies of this app running”, and that sentence gets stored. Then a set of small programs called controllers keep comparing what you asked for with what actually exists, and fix the difference. Forever.
That’s the whole idea. The rest of Kubernetes is that loop applied to more kinds of things.
The chain of command
- A Deployment holds your promise: which image, how many copies.
- It creates a ReplicaSet, which owns one particular version of the pod template and keeps the right number of pods alive.
- Pods are the running copies, one or more containers each.
- The scheduler notices pods with no node yet and picks one for each.
- The kubelet on each node notices pods assigned to it and starts their containers.
- You store a promise once: a Deployment that wants three copies. Nothing has run yet.
- The Deployment creates a ReplicaSet, whose loop keeps comparing want with have. Delete a pod and it sees 2 where it wants 3.
- It closes the gap by creating a pod - a new one, with a new name. The pod has no node yet.
- The scheduler watches for pods with no node, and picks one.
- The kubelet on that node sees a pod assigned to it and starts the containers. Nobody called anybody: each loop found its own work.
Nobody calls anybody. Each piece watches for work and does its own part. The simulator below runs those same loops every tick.
Why “desired state” matters
You didn’t write “if a pod dies, start another”. Nobody did. The controller saw want 3, have 2 and closed the gap, the same way it would after a node crash, an eviction, or you fat-fingering kubectl delete. Recovery isn’t a feature someone remembered to add. It falls out of the design.
The same idea leads straight to GitOps later in the course. If the desired state lives in git, a controller can keep the cluster matching the repository, and the repository becomes the source of truth.
You delete a pod that belongs to a Deployment with 3 replicas. What happens?
Deleting a pod doesn't change the promise. The ReplicaSet's loop sees the gap and creates a fresh pod. To actually have fewer, change the promise: kubectl scale.
Who decides which node a new pod runs on?
The scheduler assigns pods to nodes; the kubelet on that node then starts them. Separate loops, each with one job.
Everything above ran in your browser. Now do it on a real cluster with tilt lab 01-deploy-a-service (profile minimal). You need Docker, kind and Tilt, about 15 GB of free disk and 8 GB of spare RAM. Setup guide · Can my machine run it?
With the tilt platform running (./scripts/platform.sh up):
kubectl create deployment hello2 --image=nginx --replicas=3
kubectl get pods -w # leave running in one terminal
kubectl delete pod <one of them> # in anotherThen do lab 01: it adds the Service and route that turn running pods into a URL, and its check.sh grades you.
Sources
Missions and quizzes above earn their own XP. This marks the lesson done.