Sub topic 3
RollingUpdate versus Recreate
This chapter assumes you can read a Deployment manifest, know that a Deployment owns ReplicaSets, and know that changing the Pod template triggers a rollout. It covers the two built-in update strategies and how to choose between them.
Where the strategy lives
The strategy is set in spec.strategy.type of a Deployment. Two values are allowed: RollingUpdate (the default) and Recreate. If you omit the field, Kubernetes uses RollingUpdate.
In both cases the Deployment creates a new ReplicaSet for the new Pod template. The strategies differ in how the old and new ReplicaSets are scaled.
RollingUpdate
With RollingUpdate, the Deployment scales the new ReplicaSet up and the old one down in steps. Old and new Pods run side by side for a while. The pace of each step is bounded by maxSurge and maxUnavailable, which a later chapter tunes in detail.
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0With four replicas, these settings let the Deployment create one extra Pod. It removes an old Pod only after a new Pod is ready. Capacity never drops below four ready Pods, so the service stays up during the update.
The cost is that two versions run at the same time. Clients can reach either version, so the new version must tolerate the old one's traffic, data formats, and database schema.
Recreate
With Recreate, the Deployment first scales the old ReplicaSet to zero. It waits for those Pods to terminate, then creates the new Pods.
strategy:
type: RecreateThere is no rollingUpdate block for this type, and you must not set one. Between the old Pods stopping and the new Pods becoming ready, the application serves nothing. That gap is real downtime.
Choosing between them
| Concern | RollingUpdate | Recreate |
|---|---|---|
| Downtime | None if readiness and surge settings are sound | Yes, between old stop and new ready |
| Versions running together | Yes, during the rollout | Never |
| Extra capacity needed | Up to `maxSurge` Pods | None beyond the replica count |
| Typical use | Stateless web and API services | Apps that cannot share state across versions |
Use Recreate when two versions must never run together. Common cases are an application that takes an exclusive lock on a resource, or a schema migration that the old code cannot survive. It also fits a Pod that mounts a ReadWriteOnce volume that can be attached to only one node at a time. A rolling update could leave the new Pod unable to start while the old Pod still holds the volume.
Use RollingUpdate for nearly everything else.
Observing the difference
Switch a Deployment to Recreate and change the image. Watch the Pods in a second terminal.
kubectl patch deployment web --type=json \
-p='[{"op":"remove","path":"/spec/strategy/rollingUpdate"},{"op":"replace","path":"/spec/strategy/type","value":"Recreate"}]'
kubectl set image deployment/web web=nginx:1.27
kubectl get pods -l app=web -wYou will see every old Pod move to Terminating before any new Pod appears. The patch removes rollingUpdate first because the API server rejects a Recreate Deployment that still carries rolling update parameters.
Exercise
A Deployment runs one replica that mounts a ReadWriteOnce volume. A rolling update leaves the new Pod stuck in ContainerCreating while the old Pod keeps running. Which strategy would avoid this, and what do you give up?
Show answer
Use Recreate. It terminates the old Pod, which releases the volume, before the new Pod starts. You give up availability, because the service is down between the old Pod stopping and the new Pod becoming ready.