Sub topic 14
Case study: Deployment, ReplicaSet and Pod controllers
This chapter assumes you know how the control loop, owner references and the Reconcile function work from earlier chapters. It follows one Deployment through three layers of objects to show how several simple controllers combine into rolling updates and self-healing.
Who owns what
A Deployment does not create Pods. Each layer manages only the layer directly below it.
- The Deployment controller creates and scales ReplicaSets.
- The ReplicaSet controller creates and deletes Pods.
- The scheduler and the kubelet place and run Pods. There is no separate "Pod controller" that creates anything.
The ReplicaSet name ends in a hash of the Pod template. Changing the template produces a new hash, so the Deployment controller creates a new ReplicaSet and leaves the old one in place.
Each controller's loop
The ReplicaSet controller selects Pods by label and owner reference. It compares the count with spec.replicas. If the count is too low it creates Pods, and if it is too high it deletes some. It never asks what happened to a Pod. It only counts what exists now, which is level-triggered logic.
The Deployment controller does the same for ReplicaSets. During a rolling update it scales the new ReplicaSet up and the old one down. maxSurge and maxUnavailable limit how far each step may go. It keeps old ReplicaSets, scaled to zero, up to revisionHistoryLimit (default 10). That is what makes kubectl rollout undo possible.
Watch it happen
kubectl create deployment web --image=nginx:1.27 --replicas=3
kubectl get pods -l app=web -w &
kubectl delete pod -l app=web --field-selector=status.phase=Running --wait=false
kubectl get replicaset -l app=web
kubectl describe deployment webThe deleted Pods change the observed count. The ReplicaSet controller sees fewer Pods than spec.replicas and creates replacements. The describe output shows the Deployment's scaling events and conditions.
Common mistakes
- Editing a ReplicaSet or Pod by hand. The owning controller reverts or replaces it.
- Using overlapping selectors across Deployments. Controllers can then count each other's Pods.
- Expecting
kubectl delete deploymentwith--cascade=orphanto remove Pods. It leaves the ReplicaSets and Pods behind. - Reading
readyReplicasas the target. It is observed state, so compare it withspec.replicas.
Exercise
You scale a Deployment's ReplicaSet to 5 with kubectl scale rs <name> --replicas=5 while the Deployment says replicas: 3. What happens, and why?
Show answer
The Deployment controller notices the ReplicaSet's spec.replicas differs from the Deployment's desired count and sets it back to 3. The ReplicaSet controller then deletes the extra Pods. Each controller drives its own object toward the desired state, and the Deployment is the source of intent for the ReplicaSet.