Objects and the control loop
Kubernetes is a database of desired state plus controllers that keep making reality match it.
A database and some loops
Kubernetes: Up & Running introduces Kubernetes through three ideas: declarative configuration, self-healing, and immutability. The architecture follows from them:
- The API server is the front door. Every read and write goes through it, and it persists objects in etcd.
- Objects (Deployments, Services, ConfigMaps, and others) each have a
spec, what you want, and astatus, what was last observed. - Controllers watch objects and act to make reality match the spec. The Deployment controller manages ReplicaSets, the ReplicaSet controller manages pods, and the endpoint controllers manage Service endpoints.
- The scheduler assigns pending pods to nodes.
- On each node, the kubelet runs the pods assigned to it, runs their probes, and reports status.
No component "runs your app" in the imperative sense. You write desired state, and a chain of controllers converges on it.
Reconciliation changes how you fix things
Because controllers continuously compare and correct, actions outside the spec are temporary:
- Delete a pod owned by a ReplicaSet, and a new one is created from the same template.
- Edit a pod directly, and the change is lost the next time the pod is replaced.
- Scale by hand under a GitOps or autoscaling controller, and the scale is corrected back.
The lesson is to change the input. For a crash loop after a configuration change, that means fixing the ConfigMap or Deployment template, then triggering a rollout if the pods only read the configuration at start. Deleting pods only replays the same failure.
Follow the owners
Every managed object records its owner references. A pod named waybill-7c9d8f-abcde belongs to ReplicaSet waybill-7c9d8f, which belongs to Deployment waybill. kubectl get pod <name> -o jsonpath='{.metadata.ownerReferences}' shows the chain. Fixes almost always belong at the top of the chain.
Evidence lives in the API
Before opening a shell on a node, read what the cluster already recorded:
kubectl get deploy,rs,pods -l app=waybill -o wide
kubectl describe pod <pod> # conditions, container states, last termination reason, events
kubectl get events --sort-by=.lastTimestamp -n waybill
kubectl logs <pod> --previous # output from the container instance that just crashed
kubectl get deploy waybill -o yaml # full spec and status, including conditions
describe and events tell you which component acted and why: FailedScheduling, BackOff, Unhealthy, OOMKilled, FailedMount. logs --previous matters for crash loops, because the current container may have no output yet.
Declarative changes, reviewed
The book and the documentation both recommend declarative object configuration: manifests in version control, applied with kubectl apply or a GitOps controller. That makes the desired state reviewable, diffable (kubectl diff -f), and recoverable, as in the Infrastructure as Code track for infrastructure. Imperative commands remain useful for investigation and emergencies. Write any emergency change back to the source of truth, or the next reconcile will undo it.
Key terms
- Desired state
- What an object's
specasks for, such as three replicas of this pod template. - Controller
- A control loop that watches objects and acts to move the cluster's actual state toward their spec.
- Reconciliation
- The continuous compare-and-correct cycle controllers perform. Changes you make outside the spec get reverted.
- Owner reference
- Metadata linking an object to the object that manages it, for example Pod → ReplicaSet → Deployment.
Read further
- Kubernetes Up & Running, 3rd edition, Ch. 1, "Introduction" (Purchase)
The arguments for declarative configuration, self-healing systems, and immutability. These three ideas explain most of Kubernetes' behaviour. - Kubernetes Documentation, Concepts, Concepts → Overview → "Kubernetes Components", and Concepts → Cluster Architecture → "Controllers" (Free online (CC BY 4.0))
The control plane and node components, and the controller pattern (control loops that move current state toward desired state). - Kubernetes Documentation, Concepts, Concepts → Overview → "Objects In Kubernetes" (Free online (CC BY 4.0))
Object spec and status, and how the API represents both. Note the difference between imperative commands and declarative object configuration.