The API and the loopLesson 1 of 8
- Desired state and the reconcile loop, not reconciled
- Control plane and nodes, not reconciled
- API machinery: groups, versions and resource versions, not reconciled
- kubectl and kubeconfig, not reconciled
- Namespaces, labels and annotations, not reconciled
- Declarative management and server-side apply, not reconciled
- Owners, finalizers and garbage collection, not reconciled
- Custom resources and operators, not reconciled
Desired state and the reconcile loop
Kubernetes is an API that stores what should exist, plus controllers that keep comparing that record with the world and acting on the difference.
Shows the lesson one block at a time. Arrow keys step, number keys jump to a scene, and Escape returns to the page where you were.
A container runtime solves one machine: it pulls an image and runs a process inside namespaces and cgroups. Kubernetes solves the rest of the problem, which turns out to be a different kind of problem. This lesson is about the idea everything else in the guide hangs on: you store a description of what should exist, and a set of independent loops keeps making the world match it.
Reconcile, in 6 scenes
The problem it solves
Once an application needs more than one machine, every operational question changes shape. Which machine has room for this container? Who restarts it when the process exits, or when the machine under it disappears at night? How do you run five copies instead of three, and how do clients find whichever copies exist this minute? How do you swap version 1.4 for 1.5 without dropping requests?
The traditional answer is a script, or a configuration-management run, that performs steps: SSH to host A, start a container, add it to the load balancer, move to host B. Scripts encode how, and they assume the world holds still while they run. When host B dies at step three, the script fails halfway, and a person reads the logs to work out what state the fleet is in.
Kubernetes moves that work into the cluster. A cluster is a set of nodes that run your containers, plus a control plane that stores what you asked for and runs the processes that act on it. The next lesson takes the control plane apart. Here the point is the contract: you hand the cluster a record of the end state, and the cluster keeps working towards it for as long as the record exists.
Declarative, not imperative
An imperative instruction names an action: “start a container from nginx:1.30 on node A”. A declarative record names a result: “three pods from this template should exist”. Kubernetes takes the second kind. The upstream overview puts it bluntly: Kubernetes is “a set of independent, composable control processes that continuously drive the current state towards the provided desired state. It shouldn’t matter how you get from A to C.”
Here is a declarative record of a small web tier:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.30
Nothing in it says which node, in what order, or what to do if a node fails. kubectl apply -f web.yaml sends it to the API server, which validates it and stores it. That is the whole of what kubectl does. Every later effect, from choosing nodes to pulling the image, comes from components reacting to the stored record.
Three consequences follow, and the rest of the guide leans on all of them:
- Applying is repeatable. Applying the same file twice changes nothing the second time, because the stored record already says that. Scripts that start containers do not have this property.
- Drift is corrected. If a pod dies, or someone deletes one by hand, the record still says three, so a third pod comes back.
- The record can live in version control. A Git repository of manifests is a complete description of what should run. Delivery tools that sync Git to a cluster depend on this.
Imperative commands such as kubectl create deployment and kubectl scale exist too. They are shortcuts that write the same kind of record through flags instead of a file. kubectl scale deployment web --replicas=5 starts no pods; it changes one number in a stored object.
Spec and status
Almost every object carries two nested fields. specThe desired state of an object: what you, or a controller acting for you, want to exist. statusThe observed state of an object, written by the Kubernetes components that act on it. The upstream definition is precise: status is “supplied and updated by the Kubernetes system and its components”.
The difference between the two is the work the cluster still has to do. This lesson’s cluster already holds the record above, applied from web.yaml, with its three pods running. You can watch the gap open and close:
kubectl get deployment web -o jsonpath='{.spec.replicas} {.status.readyReplicas}'
3 3
kubectl scale deployment web --replicas=5
deployment.apps/web scaled
kubectl get deployment web
NAME READY UP-TO-DATE AVAILABLE AGE web 3/5 5 3 20s
kubectl rollout status deployment/web
Waiting for deployment "web" rollout to finish: 3 of 5 updated replicas are available... deployment "web" successfully rolled out
kubectl get deployment web
NAME READY UP-TO-DATE AVAILABLE AGE web 5/5 5 5 21s
READY 3/5 reads as status over spec. The scale command changed spec.replicas immediately. The two extra pods still had to be created, scheduled and started, and only once they were ready did the deployment controller raise status.readyReplicas to five; kubectl rollout status waits for exactly that.
Writers are strict about which half they touch:
| Field | Written by |
|---|---|
Deployment spec | You, through kubectl or a delivery tool |
Deployment status | The deployment controller |
ReplicaSet spec | The deployment controller, which owns the ReplicaSet |
Pod spec.nodeName | The scheduler, through a binding |
Pod status | The kubelet on the pod’s node |
Press Play and the stage changes this page’s web by two replicas, up from three or down from five, and lights each writer in the table as it acts: you from the audience, then the ReplicaSet controller, the scheduler and each node’s kubelet.
kind cluster kx
Starting
Switch to Model to see the set lit by whoever acts.
$
Underneath
A controller’s output is often another object’s spec. The deployment controller writes ReplicaSet specs, and the ReplicaSet controller writes pods. Each layer treats the layer above it exactly as you treat the Deployment.
The reconcile loop
controllerA control loop that watches the cluster’s shared state through the API server and makes changes that move the current state towards the desired state. The upstream docs use a thermostat: you set the temperature you want, the room has the temperature it has, and the thermostat switches equipment on or off to close the gap. It never runs a fixed program of “heat for 20 minutes”.
Every Kubernetes controller runs the same four steps:
- Observe. Read the desired state (the object’s spec) and the actual state (the objects and resources it governs).
- Diff. Work out what differs.
- Act. Make the changes that reduce the difference, through the API server.
- Repeat. Go back to observing, whether the action worked or not.
reconcileOne pass of a controller over one object: compare spec with observed state and act on the difference. Controllers learn about changes by watching the API server, so they react within moments of a write. What they act on, though, is the current state, not the event. This is what level-triggered means. An edge-triggered design reacts to “pod deleted” and breaks if that message is lost. A level-triggered controller asks “how many pods match, and how many should?” and gets the right answer however it arrived there. A controller that crashes, restarts and relists the world repairs anything it missed, and it can safely reconcile the same object twice.
The upstream docs draw a consequence worth keeping: because failures and fixes happen all the time, a cluster may never reach a stable state, and that does not matter as long as the controllers keep running and keep making useful changes.
ExplainerThe reconcile loop
The Deployment goes at once; the garbage collector deletes its dependents after.
Each action runs as a kubectl command; it shows here.
Watch stream0 queued
- Nothing queued, nothing in flight.
and 0 more
Cache, by ownerReferences What the controllers have been told, not yet what is queued.
Empty: no Deployment, ReplicaSet or Pod named web.
No change delivered yet.
Delivered to the informers of
Every controller that watches the object's kind; the one that owns it acts.
- Observe
- What the controller that owns the object reads from its cache.
- Diff
- What the object asks for against what exists.
- Act
- The writes that close the gap, each queued as a change of its own.
Apply the Deployment. Its changes queue in the watch stream, and Step delivers them one at a time.
Deleting a pod by hand is the simplest way to watch a controller work:
kubectl get pods
NAME READY STATUS RESTARTS AGE web-89c8ffd94-dq87d 1/1 Running 0 20s web-89c8ffd94-nshlv 1/1 Running 0 20s web-89c8ffd94-pggpj 1/1 Running 0 20s
kubectl delete pod web-89c8ffd94-dq87d --wait=false
pod "web-89c8ffd94-dq87d" deleted from default namespace
kubectl get pods
NAME READY STATUS RESTARTS AGE web-89c8ffd94-9zhhn 0/1 Pending 0 0s web-89c8ffd94-dq87d 1/1 Terminating 0 20s web-89c8ffd94-nshlv 1/1 Running 0 20s web-89c8ffd94-pggpj 1/1 Running 0 20s
kubectl wait --for=delete pod/web-89c8ffd94-dq87d --timeout=60s
pod/web-89c8ffd94-dq87d condition met
kubectl get pods
NAME READY STATUS RESTARTS AGE web-89c8ffd94-9zhhn 1/1 Running 0 2s web-89c8ffd94-nshlv 1/1 Running 0 22s web-89c8ffd94-pggpj 1/1 Running 0 22s
The replacement appeared before the old pod finished terminating. A pod that is shutting down no longer counts towards the ReplicaSet’s replicas, so the controller saw two of three and created one. --wait=false returns as soon as the API server accepts the delete; without it, kubectl delete blocks until the pod is gone, which is what kubectl wait --for=delete does here.
Break it from the cluster’s side as well. Delete a pod picks one of web’s pods and runs the same command, and the panel follows the ReplicaSet until it is whole again.
Delete a pod
- Runs kubectl delete pod <pod>. Its ReplicaSet notices one replica missing and makes a new pod.
Each button runs its command on the cluster. This line follows what happens next.
Objects and controllers
Each built-in workload kind is an object that some controller watches:
- The deployment controller watches Deployments and manages ReplicaSets, one per pod template, to roll out changes.
- The ReplicaSet controller watches ReplicaSets and creates or deletes pods to match
spec.replicas. - The Job controller creates pods until a Job’s completions are reached, then marks the Job finished.
- The node controller watches node heartbeats and marks silent nodes unhealthy.
- The EndpointSlice controller keeps a list of the pods behind each Service, with whether each one is ready.
Components outside kube-controller-manager follow the same shape. The scheduler watches pods that have no node and writes a binding for each. The kubelet watches pods bound to its node and drives the container runtime until the containers match the pod spec. kube-proxy watches Services and EndpointSlices and programs Service virtual-IP translation on its node with iptables or nftables rules. kube-proxy does not route traffic between pods; that is the job of the network (CNI) plugin.
Controllers chain because one’s output is the next one’s input. Owner references, metadata that records which object created which, tie the chain together. They let the garbage collector delete a ReplicaSet’s pods when the ReplicaSet goes, and they let you predict what happens when something in the chain disappears:
kubectl get rs
NAME DESIRED CURRENT READY AGE web-89c8ffd94 3 3 3 20s
kubectl delete rs web-89c8ffd94
replicaset.apps "web-89c8ffd94" deleted from default namespace
kubectl get rs
NAME DESIRED CURRENT READY AGE web-89c8ffd94 3 3 0 0s
kubectl rollout status deployment/web
Waiting for deployment "web" rollout to finish: 0 of 3 updated replicas are available... deployment "web" successfully rolled out
kubectl get rs
NAME DESIRED CURRENT READY AGE web-89c8ffd94 3 3 3 1s
The garbage collector removed the old ReplicaSet’s pods. The deployment controller found no ReplicaSet for its template and created one, with the same name because the name comes from a hash of the unchanged template. That ReplicaSet created three pods. The rule generalises: delete something a controller manages and the controller recreates it from the owner’s spec. Delete the Deployment itself and nothing recreates it, because nothing owns it. You are its controller.
The pattern is open. A CustomResourceDefinition adds a kind to the API, and a controller you install, often called an operator, watches it and reconciles it the same way the built-in controllers do. Later lessons build on that.
What it leaves to you
Kubernetes is deliberately narrow. The upstream overview lists what it does not do, and each item is a separate component in a real platform:
- Building images. Kubernetes runs images that already exist in a registry. Turning source into images is a build tool’s job.
- Delivery pipelines. Kubernetes does not deploy source code or decide when a change ships. CI systems test and build; GitOps tools such as Argo CD or Flux sync manifests from Git into the cluster.
- Observability. Kubernetes exposes metrics endpoints, events and container logs, but it does not dictate or ship a logging, monitoring or alerting stack. Prometheus, OpenTelemetry and log pipelines are add-ons.
- Policy. Pod Security Admission and ValidatingAdmissionPolicy are built in. Richer policy engines such as Kyverno or OPA Gatekeeper are installed as admission webhooks.
- Application services. Databases, caches and message buses can run on Kubernetes, but it does not provide them.
- Configuration languages. The API accepts declarative objects; templating them is Helm’s or Kustomize’s job.
- Machines. Kubernetes does not provision, patch or repair the servers it runs on. Node provisioning belongs to your infrastructure tooling or a cluster autoscaler.
Most of these tools are themselves controllers. A GitOps tool reconciles a cluster towards a Git commit; a policy engine is a controller for policy objects. Once you can read the reconcile loop, the ecosystem stops looking like a pile of unrelated products.
kubectl --context kind-kx create deployment hello --image=nginx:1.30 --replicas=3kubectl --context kind-kx get deployment hello -o jsonpath='{.spec.replicas} {.status.readyReplicas}{"\n"}'kubectl --context kind-kx delete pod -l app=hello --wait=falsekubectl --context kind-kx get pods -l app=hellokubectl --context kind-kx delete deployment hello
For your own kind cluster kx: copy a command and paste it into your terminal. Run in the simulator runs the same command in this page's simulated cluster instead; nothing on this page touches your machine. All three pods are replaced within seconds, with different names. After the last command nothing comes back, because nothing owns the Deployment.
Actual state
Check your understanding
Your first answer to each check is the one that counts. After a miss you can answer again; passing every check reconciles the lesson.
Lesson not yet reconciled
Desired: 4 objectives, 0 metReconciled: 4 of 4 objectives met
- Describe Kubernetes as an API of desired state plus controllers that converge actual state on it. (not yet met)
- Explain spec versus status and who writes each. (not yet met)
- Predict what a controller does when an object it manages disappears. (not yet met)
- List what Kubernetes leaves to other tools, such as builds, delivery pipelines, observability and policy. (not yet met)
Pass every check above to reconcile this lesson.
Every check has passed, so the desired state is met. First-try accuracy: 0 of 5. This lesson's review cards are in your queue.
Changed since the notes
This lesson draws on an earlier set of study notes. Where the documentation contradicts a claim in them, qualifies it or has moved past it, the claim is listed here with the corrected statement and the source it was checked against.
Show 2 corrections
The notes saidMore recently, Kubernetes standardized on CRI / dropped Docker support
CorrectedCRI dates from 2016. dockershim was deprecated in v1.20 (2020) and removed in v1.24 (2022).
The notes saidkube-proxy handles network routing
Correctedkube-proxy programs Service virtual-IP translation using iptables, nftables or IPVS. Routing Pod-to-Pod traffic is the CNI's job.
Every correction across the guide, by note and topic.
Sources
Checked against kubernetes.io and AWS documentation for Kubernetes 1.37 on .
- kubernetes.io/docs/concepts/overview
- kubernetes.io/docs/concepts/overview/working-with-objects
- kubernetes.io/docs/concepts/architecture/controller
- kubernetes.io/docs/concepts/overview/working-with-objects/object-management
- kubernetes.io/docs/concepts/architecture/cri
- kubernetes.io/blog/2022/02/17/dockershim-faq
- kubernetes.io/blog/2016/12/container-runtime-interface-cri-in-kubernetes
- kubernetes.io/docs/reference/networking/virtual-ips
Desired state and the reconcile loop
↓ ↑ step, 1 to 6 scene, Esc leave