Sub topic 11
Rolling updates step by step
This chapter assumes you can read a Deployment manifest, run kubectl apply and kubectl get, and know that a Deployment owns ReplicaSets, which own Pods. It also assumes you know that a template change creates a new ReplicaSet, and that a readiness probe decides when a Pod can take traffic.
What triggers a rolling update
A rolling update starts when you change anything under spec.template in a Deployment. A new image tag is the usual cause. Changing replicas does not start one, because scaling does not touch the Pod template.
The Deployment controller creates a new ReplicaSet for the changed template. It then moves Pods from the old ReplicaSet to the new one in small steps. Two settings limit how big each step can be:
maxSurgeis how many Pods abovereplicasmay exist during the update.maxUnavailableis how many Pods belowreplicasmay be unavailable.
With replicas: 3, maxSurge: 1 and maxUnavailable: 0, the Deployment never has fewer than 3 available Pods, and never more than 4 in total.
The steps
Walk through it with three replicas:
- The new ReplicaSet starts at 0 Pods. The old one has 3.
- The controller scales the new one to 1. Total is 4, which is the surge limit.
- The new Pod must pass its readiness probe. Until then it does not count as available, so the old Pods stay.
- Once it is ready, the old ReplicaSet scales to 2. Total is 3.
- The cycle repeats until the new ReplicaSet has 3 and the old one has 0.
This is why a readiness probe matters. Without one, a Pod counts as available as soon as its container starts, and the rollout can retire healthy Pods in favour of ones that are not yet serving.
Running it
Use the Deployment from the previous chapter, with maxSurge: 1, maxUnavailable: 0 and a readiness probe.
kubectl set image deployment/web web=nginx:1.26
kubectl rollout status deployment/web
kubectl get rs -l app=webrollout status blocks until the rollout finishes or fails. Afterwards get rs shows the new ReplicaSet at 3 desired Pods and the old one at 0. The old ReplicaSet is kept, not deleted, so you can roll back to it.
Common errors
- Editing only metadata. Changing labels or annotations on the Deployment itself does not change the template, so nothing rolls out.
- Setting both
maxSurgeandmaxUnavailableto 0. The controller could never make progress, so the API server rejects it. - No readiness probe. The rollout looks successful while new Pods are still starting.
- Wrong probe path. New Pods never become ready. The rollout stalls with old Pods still serving, which a later chapter covers.
Exercise
A Deployment has replicas: 4, maxSurge: 25% and maxUnavailable: 0. What is the largest total number of Pods during the update, and how many must be available at all times?
Show answer
Percentages for maxSurge round up, so 25% of 4 is 1. The total can reach 5 Pods. With maxUnavailable 0, at least 4 Pods must be available at all times.