Sub topic 2
Spec describes intent, status describes reality
This chapter assumes you can read YAML manifests, use kubectl get and kubectl describe, and know what a Deployment and a Pod are. It covers how every Kubernetes object separates what you want from what the cluster has observed.
Two halves of one object
Most Kubernetes objects have a spec and a status.
specis the desired state. You write it, usually withkubectl apply.statusis the observed state. A controller writes it, and you normally don't edit it.
This split lets a controller work without asking anyone what to do. It reads the spec, looks at the world, acts, and records what it saw in the status.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
generation: 2
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.27
status:
observedGeneration: 2
replicas: 3
readyReplicas: 2
conditions:
- type: Available
status: "True"Here the spec asks for three replicas. The status reports that three Pods exist and two are ready. The two blocks disagree, so the controller has more work to do. That disagreement is the signal that drives the control loop.
Who writes what
| Field | Written by | Meaning |
|---|---|---|
| `spec` | The user, or a tool acting for the user | What you want |
| `status` | The controller | What it has seen so far |
| `metadata.generation` | The API server | Counter that increases when the spec changes |
| `status.observedGeneration` | The controller | The last generation it processed |
Is the status current?
A status can lag behind the spec. Suppose you change the image. The API server raises metadata.generation from 1 to 2. Until the controller handles that change, status.observedGeneration is still 1. In that window, readyReplicas describes the old spec, not the new one.
So compare the two numbers before you trust a status. If observedGeneration is lower than generation, the controller has not caught up yet. kubectl rollout status uses this check.
Keeping the halves apart
Many resource types expose status as a subresource, a separate endpoint at /status. This has two effects.
- A normal update to the object ignores any changes to
status. This prevents a user from overwriting what the controller reported. - A controller writing to
/statuscannot change the spec.
Because of this, the two sides do not overwrite each other. Permissions can also differ: a user may be allowed to edit Deployments but not deployments/status.
Common mistakes
- Reading
statusas a promise. It is a report of what the controller saw, and it may be stale. - Putting observed data in
spec, such as a computed IP address. Keep spec for intent only. - Editing
statusby hand in a manifest. The API server drops it on apply for resources with a status subresource, and the controller overwrites it anyway.
Exercise
A Deployment shows metadata.generation: 5 and status.observedGeneration: 4, with readyReplicas: 3 and spec.replicas: 3. Can you conclude the latest spec is fully running? Why or why not?
Show answer
No. The controller has not yet processed generation 5, so the status describes the earlier spec. The three ready replicas may be running the old configuration. Wait until observedGeneration equals generation, then check the readiness numbers.