Skip to main content
Kubernetes coreLesson 2 of 13

Desired State and the Control Loop

You do not tell Kubernetes "do this". You write down "it should look like this", and controllers keep comparing that with how things are now and fix the difference. Follow this comparison loop, and how resources find each other with labels and selectors. How a Pod starts, part by part, is covered in How a Pod Starts.

Desired (spec)3
Current (status)0
Controller decision—
Service targets—
Manifest in the API serverPods · app=webUserkubectl applyDesired statespecreplicas: 3Current statestatusControllerCompare and fixService webSelects app=webweb-aapp=webweb-bapp=webweb-capp=webweb-dapp=webMust be 3
RequestResponse / reportWatchAppears in a later step

What you will learn

  • Imperative vs declarative
  • Why you read spec and status side by side
  • A control loop repeats "compare → act"
  • An unreachable goal means waiting, not an error
  • Resources connect by labels, not names

Myths and facts​

Common mythIn reality
If apply succeeds, all Pods are upOnly the desired state is recorded. The actual state takes time to catch up
Lack of resources causes an errorNo error. Pods wait as Pending and keep retrying until room opens up
Users edit status tooKubernetes fills status. Users write only spec
A Service finds targets by Pod nameIt uses a label selector. New Pods with matching labels join automatically
A wrong selector is rejected on saveIt saves fine, with 0 targets

The control loop actually bringing a Pod back continues in When a Pod Dies (Self-Healing).

Try it yourself​

kubectl create deployment demo-web --image=nginx --replicas=3 # Submit the desired state (throwaway)
kubectl get deploy demo-web # READY: current/desired
kubectl get deploy demo-web -o jsonpath='{.spec.replicas} {.status.readyReplicas}{"\n"}' # spec vs status
kubectl get svc <service> -o jsonpath='{.spec.selector}' # Which labels the Service selects
kubectl get endpointslices -l kubernetes.io/service-name=<service> # 0 endpoints? Suspect the selector
kubectl delete deployment demo-web # Cleanup

Check yourself

  1. You change replicas from 3 to 4, but the nodes have no free resources. What do you see?
  2. All web Pods are Running, but the Service web reaches none of them and has 0 endpoints. What should you suspect first?
  3. kubectl apply returned success. What does that tell you?

Further reading: Controllers (Kubernetes docs) · Labels and Selectors · How a Pod Starts · When a Pod Dies (Self-Healing)