Sub topic 14
ReplicaSets and Deployments
This chapter assumes you know what a Pod is, how labels and selectors match objects, and how to read basic YAML. You should also know that controllers reconcile desired state with actual state.
Why Pods alone are not enough
A bare Pod is not replaced if its node fails or if someone deletes it. You also cannot scale it or update it in a controlled way. Kubernetes adds two workload objects on top of Pods to handle this.
- A ReplicaSet keeps a stated number of identical Pods running.
- A Deployment manages ReplicaSets so you can roll out new versions and roll them back.
ReplicaSets: keeping a count
A ReplicaSet has a Pod template, a replica count, and a label selector. Its controller counts the Pods that match the selector. If there are too few, it creates Pods from the template. If there are too many, it deletes some. This is the reconciliation loop from earlier chapters, applied to one number.
You rarely create ReplicaSets yourself. A Deployment creates and owns them for you.
Deployments: managing change
A Deployment declares the desired state of an application. The manifest below asks for three replicas of an nginx Pod.
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.27
ports:
- containerPort: 80The selector must match the labels in template.metadata.labels. If they differ, the API server rejects the Deployment.
Apply it and inspect the result:
kubectl apply -f deployment.yaml
kubectl get deployments
kubectl get pods -o wideThe Deployment controller creates one ReplicaSet for this Pod template. That ReplicaSet creates the three Pods.
How a rolling update works
When you change the Pod template, for example by setting a new image, the Deployment controller creates a new ReplicaSet. It scales the new one up and the old one down in small steps. Old Pods are replaced gradually, so some Pods are always serving traffic.
The pace is set by maxSurge and maxUnavailable in the Deployment's strategy.rollingUpdate field. The defaults for both are 25%. maxSurge is how many extra Pods may exist above the desired count during the update. maxUnavailable is how many Pods may be missing.
The old ReplicaSet is kept at zero replicas. This is what makes rollback possible.
Common errors
- Selector mismatch. The selector does not match the template labels, and the apply fails.
- Editing Pods directly. The ReplicaSet treats them as drift. It may replace them or ignore your change.
- Overlapping selectors. Two controllers with selectors that match the same Pods can fight over them. Give each workload a distinct label.
- Stuck rollouts. A bad image tag leaves new Pods in
ImagePullBackOff. The Deployment stops advancing but keeps the old Pods running.
Exercise
A Deployment named web has 3 replicas. You change its image from nginx:1.26 to nginx:1.27. How many ReplicaSets exist afterward, and what happens to the old one?
Show answer
Two ReplicaSets exist. The new one is scaled up to 3 Pods and the old one is scaled down to 0 but kept, so a rollback can reuse it.