Sub topic 9
How a Template Change Creates a New ReplicaSet
This chapter assumes you can read a Deployment manifest, run kubectl get and kubectl describe, and know that a ReplicaSet owns Pods through a label selector.
What counts as a template change
A Deployment does not run Pods itself. It owns ReplicaSets, and each ReplicaSet owns Pods. The Deployment controller compares the Pod template in spec.template with the template of its current ReplicaSet. If they differ, it creates a new ReplicaSet.
Only changes under spec.template trigger this. Changing the image, environment variables, resource limits, probes or template labels all count. Changing spec.replicas does not, because the replica count is not part of the template. Scaling only edits the desired count on the existing ReplicaSet.
| Change | New ReplicaSet? |
|---|---|
| `kubectl scale --replicas=5` | No |
| New image tag in `spec.template` | Yes |
| New environment variable in `spec.template` | Yes |
| Edit to `strategy` or `revisionHistoryLimit` | No |
The pod-template-hash label
Several ReplicaSets for the same Deployment share the same app: web label. If their selectors were only app: web, they would compete for the same Pods. To prevent this, the controller adds a pod-template-hash label to each ReplicaSet and to the Pods it creates. The value is a hash of the Pod template. The ReplicaSet name is the Deployment name plus that hash, such as web-7d4b9c8f6.
The controller also adds the hash to the ReplicaSet's selector. Each ReplicaSet therefore matches only its own Pods, even though all of them carry app: web.
Seeing it happen
Start from the Deployment in the previous chapters, then change the image and list the ReplicaSets.
kubectl set image deployment/web web=nginx:1.26
kubectl rollout status deployment/web
kubectl get rs -l app=web --show-labelsYou should see two ReplicaSets. The new one has the desired replica count. The old one is scaled to zero but still exists. The --show-labels output shows a different pod-template-hash on each.
If you change the image back to nginx:1.25, the hash matches the old template again. The controller finds the existing ReplicaSet and scales it up instead of creating a third one. This reuse is how rollbacks work.
Common errors
- Expecting a rollout from a scale change. Scaling never creates a ReplicaSet.
- Editing a ReplicaSet directly. The Deployment controller reverts changes that do not match its template.
- Deleting old ReplicaSets by hand. This removes the revision history that rollbacks need. Use
revisionHistoryLimitto control how many are kept. - Changing the selector. The selector is immutable in
apps/v1. Changing it requires a new Deployment.
Exercise
You run kubectl scale deployment/web --replicas=5 and then kubectl set image deployment/web web=nginx:1.26. How many new ReplicaSets are created, and why?
Show answer
One. The scale command only changes the desired count on the existing ReplicaSet, so it creates none. The image change alters spec.template, so the Deployment gets a new template hash and creates one new ReplicaSet.