Session 3: rolling updates and rollback
A Deployment does not just keep N pods running. It also owns how you upgrade from one version to the next. This is where the rolling update strategy comes in.
What a rolling update does.
When you change the image tag on a Deployment, Kubernetes creates a new ReplicaSet for the new image, then gradually shifts pods from the old ReplicaSet to the new one. It brings up one new pod, waits for it to be Ready, then kills one old pod. Repeat until every pod is on the new version.
You control the speed and safety of the rollout with two knobs:
- maxSurge: how many extra pods can exist above the desired count during the rollout (default 25%).
- maxUnavailable: how many pods can be unavailable at once (default 25%).
With 4 replicas, at any moment during a rollout you have 3 to 5 pods, with 3 always Ready.
What rollback does.
Every rollout creates a new ReplicaSet. Old ReplicaSets stick around at size 0 but hold the revision history. If the new version is bad, roll back to any previous ReplicaSet.
kubectl commands I ran today:
kubectl create deployment demo --image=nginx:1.24 --replicas=4kubectl set image deployment/demo nginx=nginx:1.25(trigger the update)kubectl rollout status deployment/demo(watch it complete)kubectl rollout history deployment/demo(see revisions)kubectl rollout history deployment/demo --revision=1(details of one revision)kubectl rollout undo deployment/demo(roll back to previous)kubectl rollout undo deployment/demo --to-revision=1(roll back to a specific revision)kubectl get replicasets(old and new ReplicaSets side by side)
Things I noticed:
- Old ReplicaSets stay at size 0 after a rollout completes. That is the revision history.
- The container name in
set image(nginx in the command above) must match the container name in the pod spec, not the image name. rollout statusblocks the terminal until the rollout completes. Useful in CI.- Rollback is fast because Kubernetes just scales the old ReplicaSet back up. Nothing to build, nothing to pull.
The mental model.
A Deployment is a rollout controller. It manages a sequence of ReplicaSets, each one a version of your service. The current ReplicaSet has N pods. The previous ones sit at 0 but hold the config to bring them back at any time.
Next: Services, how one pod finds another.