Sub topic 1
How a Deployment manages ReplicaSets
Prerequisites
This chapter assumes you know:
- Pods, labels, and selectors.
- How to read and apply a YAML manifest.
- Basic
kubectluse (get,apply,describe). - What a container image and an image tag are.
The three layers
A Deployment does not run Pods itself. It owns ReplicaSets, and each ReplicaSet owns Pods.
- The Pod runs your containers.
- The ReplicaSet keeps a set number of identical Pods running. It only knows one Pod template.
- The Deployment holds the desired Pod template and the update policy. It creates one ReplicaSet for each distinct template and changes their replica counts to move from the old version to the new one.
This split is what makes rolling updates possible. A ReplicaSet cannot change its template. To change the template, the Deployment creates a new ReplicaSet and shifts Pods over to it.
A minimal Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.26
ports:
- containerPort: 80Apply it and list what was created:
kubectl apply -f web.yaml
kubectl get deployment web
kubectl get replicasets -l app=web
kubectl get pods -l app=webYou will see one ReplicaSet named like web-6d8f7c5b9, and three Pods named like web-6d8f7c5b9-abcde. The middle part of the name is the pod-template-hash, a hash of the Pod template.
How the Deployment tells ReplicaSets apart
The Deployment controller adds a pod-template-hash label to each ReplicaSet and to the Pods it creates. It also adds the label to the ReplicaSet's selector. Without it, two ReplicaSets of the same Deployment would both match every Pod with app: web, and they would fight over the same Pods.
You can check this directly:
kubectl get pods -l app=web --show-labels
kubectl get replicaset -l app=web -o jsonpath='{.items[0].metadata.ownerReferences[0].name}'The second command prints web. That owner reference is how Kubernetes knows the ReplicaSet belongs to the Deployment. If you delete the Deployment, the garbage collector deletes the ReplicaSets and then the Pods.
What the Deployment controller does
The controller compares the Deployment's template with the templates of its existing ReplicaSets. It then takes one of these actions:
- If a ReplicaSet already matches the template, the controller scales that ReplicaSet to the desired
replicas. It scales the others down. - If none matches, it creates a new ReplicaSet with the new template.
- If only
replicaschanged, it scales the current ReplicaSet and creates nothing new.
When a new ReplicaSet is created, the controller does not switch everything at once. By default (the RollingUpdate strategy), it raises the new ReplicaSet's count and lowers the old one's in steps. The details of that pacing are covered in later chapters. The point here is that the Deployment only changes replica counts on ReplicaSets. The ReplicaSets create and delete the Pods.
Old ReplicaSets stay around
After an update, the old ReplicaSet is scaled to zero. It is not deleted.
kubectl get replicasets -l app=webNAME DESIRED CURRENT READY
web-6d8f7c5b9 0 0 0
web-7b9c4d8f6 3 3 3The idle ReplicaSet keeps its old template. That saved template is what a rollback uses later. How many are kept is set by revisionHistoryLimit, which defaults to 10.
Exercise
You run kubectl scale replicaset web-6d8f7c5b9 --replicas=5 on the ReplicaSet that belongs to the Deployment web, which has replicas: 3. What happens, and why?
Show answer
The Deployment controller notices that the ReplicaSet's count differs from the Deployment's desired count and scales it back to 3. The Deployment is the source of truth for replica counts, so you should change spec.replicas on the Deployment instead.
Exercise
Why does the Deployment controller add a pod-template-hash label to each ReplicaSet and its Pods?
Show answer
It keeps the selectors of the Deployment's ReplicaSets from overlapping. Each ReplicaSet selects only the Pods created from its own template, even though all of them share the label app: web.