Sub topic 6
Deployments: Managing ReplicaSets for You
Prerequisites
This chapter assumes you know what a Pod is and how labels and selectors work. It also assumes you know how a ReplicaSet keeps a fixed number of Pods running. You should be able to use kubectl get, describe and apply.
What a ReplicaSet cannot do
A ReplicaSet keeps the Pod count correct, but it does not manage updates. If you change the image in its Pod template, existing Pods keep running the old image. Only Pods created later use the new template. To roll out a new version, you would have to delete Pods yourself and accept either downtime or scripting.
What a Deployment adds
A Deployment is a controller that owns ReplicaSets, and each ReplicaSet owns its Pods. You declare the desired state: a selector, a Pod template and a replica count. The Deployment controller creates a ReplicaSet for the current template. When the template changes, it creates a new ReplicaSet and shifts replicas from the old one to the new one. It keeps old ReplicaSets, scaled to zero, as revision history for rollbacks.
Seeing the chain
Apply the Deployment manifest from the previous section, then list each layer:
kubectl apply -f deployment.yaml
kubectl get deployment web
kubectl get rs -l app=web
kubectl get pods -l app=webThe ReplicaSet name is the Deployment name plus a hash, such as web-7d9c5b6f8. Pod names add one more suffix. The hash comes from the Pod template. The Deployment also adds it to the ReplicaSet's selector and Pod labels as pod-template-hash. This keeps ReplicaSets from the same Deployment from claiming each other's Pods.
Which changes create a new ReplicaSet
Only changes to spec.template create a new ReplicaSet. Changing spec.replicas rescales the current ReplicaSet and creates nothing new.
| Change | Effect |
|---|---|
| Image, env vars, resources, probes in `spec.template` | New ReplicaSet and a rollout |
| `spec.replicas` | Existing ReplicaSet is rescaled |
| `spec.selector` | Rejected, because the selector is immutable in `apps/v1` |
Common errors
- Selector does not match template labels. The API server rejects the manifest, because the Deployment would never find its own Pods.
- Overlapping selectors. If two Deployments select the same labels, their ReplicaSets can adopt each other's Pods and the counts will behave unpredictably. Give each workload a distinct label.
- Expecting a rollout after a replica change. Scaling does not create a revision. Only template changes do.
Exercise
You run kubectl scale deployment/web --replicas=5 and then kubectl get rs -l app=web. Do you see a new ReplicaSet? Explain, and say which kind of change would create one.
Show answer
No. Scaling only changes the desired count of the existing ReplicaSet, so you see the same ReplicaSet with 5 desired Pods. A new ReplicaSet appears only when spec.template changes, for example kubectl set image deployment/web web=nginx:1.26.