Curious Pandas

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.

14 min+35 XP for finishing

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.
Visual guideOne promise, five loops
One promise, five loopsYou store a Deployment once, wanting three copies. It creates a ReplicaSet, which keeps comparing want with have and creates a pod with a new name to close any gap. The scheduler assigns the new pod a node, and the kubelet on that node starts its containers.you, oncekubectl createDeploymentwant: 3 copiesReplicaSetwant 3, have 2?new podnew name, no nodekubeletstarts containersschedulerpicks a nodestorescreatescreatesassigns noderuns itOne promise, five loopsYou store a Deployment once, wanting three copies. It creates a ReplicaSet, which keeps comparing want with have and creates a pod with a new name to close any gap. The scheduler assigns the new pod a node, and the kubelet on that node starts its containers.you, oncekubectl createDeploymentwant: 3 copiesReplicaSetwant 3, have 2?new podnew name, no nodekubeletstarts containersschedulerpicks a nodestorescreatescreatesassigns noderuns it
  1. You store a promise once: a Deployment that wants three copies. Nothing has run yet.
  2. The Deployment creates a ReplicaSet, whose loop keeps comparing want with have. Delete a pod and it sees 2 where it wants 3.
  3. It closes the gap by creating a pod - a new one, with a new name. The pod has no node yet.
  4. The scheduler watches for pods with no node, and picks one.
  5. 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.

Three pandas, alwayst = 0s

Click any pod to describe it.

Cluster
Waiting for a node
Running & ReadyStartingPendingCrashing / can't pull

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.

Check yourself+10 XP

You delete a pod that belongs to a Deployment with 3 replicas. What happens?

Check yourself+10 XP

Who decides which node a new pod runs on?

Do it for real · your machine+50 XP bonus

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 another

Then do lab 01: it adds the Service and route that turn running pods into a URL, and its check.sh grades you.

Self-reported. We can't see your machine, and we don't want to. This is a bonus, never required.

Sources

Finished reading?

Missions and quizzes above earn their own XP. This marks the lesson done.