Sub topic 1
Spec is desired state, status is observed state
Every Kubernetes object answers two questions. The first is what you want. The second is what is actually true now. The API stores the answers in two separate fields, spec and status. Most of this tutorial is about reading status and the events that go with it, so this chapter sets up the distinction first.
Prerequisites
This chapter assumes you have:
- Basic kubectl usage (
get,describe,apply). - Familiarity with Pods, Deployments, and Nodes.
- The ability to read YAML and basic JSON structure.
- Access to a cluster, or a local one such as kind or minikube.
Two fields, two writers
The spec field holds the desired state. You write it, usually by applying a manifest. The status field holds the observed state. Controllers and the kubelet write it, and you normally don't.
| Field | Meaning | Who writes it |
|---|---|---|
| `spec` | What you want | You, or tools acting for you |
| `status` | What the cluster observes | Controllers, the scheduler, and the kubelet |
A manifest you apply can contain a status block, but the API server ignores it. Status is reported by the components that watch the real world.
A worked example
Apply this Deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: app
image: nginx:1.27Then read the object back with kubectl get deployment web -o yaml. Besides your spec, the output now has a status block like this:
status:
replicas: 3
readyReplicas: 2
availableReplicas: 2
updatedReplicas: 3
observedGeneration: 1Your spec asks for three replicas. The status says two are ready. The gap between the two is the information you need. It means the system is still working toward your goal, or something is blocking it.
Reconciliation
Kubernetes controllers run a loop. Each one compares spec with the observed state, acts to close any difference, and records what it sees in status. This is called reconciliation.
Because the loop never stops, status is a recent report rather than a guarantee. A Pod can crash a moment after status says it is fine.
Generation and observedGeneration
Each time you change spec, the API server increments metadata.generation. Many controllers copy the value they last acted on into status.observedGeneration. If the two numbers differ, the status you are reading describes the old spec, so don't trust it yet.
kubectl get deployment web -o jsonpath='{.metadata.generation} {.status.observedGeneration}{"\n"}'Matching numbers mean the controller has seen your latest change. This is the first check to run right after an apply.
Common mistakes
- Editing
statusby hand to "fix" a problem. The controller overwrites it on the next pass, so changespecor the underlying cause instead. - Reading
specto learn what is happening. It only records your intent. - Assuming a successful
kubectl applymeans the workload works. Apply only confirms that the API server accepted the spec.
Exercise
You apply a Deployment with replicas: 5. A minute later, kubectl shows status.readyReplicas: 3. Which field shows what you asked for, which shows what is happening, and what should you check to confirm the controller has seen your latest change?
Show answer
spec.replicas (5) is the desired state, and status.readyReplicas (3) is the observed state. Compare metadata.generation with status.observedGeneration. If they match, the controller has processed your latest spec and the gap is real. If they differ, the status describes an older spec.