Workloads: Deployments, rollouts and jobs · 5 MIN READ

Rolling updates: maxSurge, maxUnavailable, rollout history, undo and stalled rollouts

Configure a Deployment's update strategy, predict how many Pods exist during a rollout, follow and steer it with kubectl rollout, and recover from a release that never becomes ready.

Changing a Deployment's Pod template starts a rollout: a new ReplicaSet grows while the old one shrinks. How fast that happens, how much spare capacity it needs and how much serving capacity it may lose are all settings. Just as important is what happens when the new version never becomes ready: Kubernetes stops, keeps the old Pods serving, and leaves the decision to roll back to you.

kubernetes · A MAP OF THE IDEA

  1. Edit image and change-cause
  2. kubectl apply
  3. Gate on rollout status
  4. Undo or fix forward

Useful when

  • Capacity bounds are explicit and predictable at every step.
  • A release that never gets ready stalls instead of replacing healthy Pods.

Watch out

  • Old and new versions serve together and must be compatible.
  • Nothing rolls back automatically when the deadline passes.

Implementation & operation cost: Spare capacity for surge Pods, backward-compatible releases and a pipeline that waits on rollout status.

01

RollingUpdate and Recreate

RollingUpdate, the default, replaces Pods gradually: the Deployment controller scales the new ReplicaSet up and the old one down in steps, and only counts new Pods once they are available, meaning ready for minReadySeconds. Old and new versions serve side by side for a while, so every release must be compatible with the previous one, including database schema, API responses and session formats. Recreate deletes every old Pod first and only then creates new ones. It causes downtime, but it is the right choice when two versions must never run at once, for example a single writer holding a ReadWriteOnce volume, or a migration the old code cannot tolerate.

02

maxSurge and maxUnavailable arithmetic

maxSurge is how many Pods may exist above replicas during the update; maxUnavailable is how many of the desired replicas may be unavailable. Both accept a number or a percentage of replicas and both default to 25%. Percentages round up for surge and down for unavailability. With replicas: 10 that is a surge of 3 and an unavailability of 2: at most 13 Pods exist and at least 8 are available at every moment. With replicas: 4 both become 1. maxSurge: 1 with maxUnavailable: 0 replaces one Pod at a time without ever dropping below full capacity, at the price of room for one extra Pod; maxSurge: 0 with maxUnavailable: 1 needs no spare room but runs one Pod short. Both cannot be zero.

yaml
spec:
  replicas: 10
  minReadySeconds: 5
  progressDeadlineSeconds: 600
  revisionHistoryLimit: 10
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 25%        # ceil(10 * 0.25) = 3 extra Pods allowed
      maxUnavailable: 25%  # floor(10 * 0.25) = 2 Pods may be unavailable
Read the code

During a rollout of this Deployment the controller keeps between 8 and 13 Pods, at least 8 of them available. minReadySeconds makes a new Pod count as available only after it has stayed ready for 5 seconds, which catches versions that crash shortly after starting. The last two fields show their defaults: ten minutes without progress marks the rollout as failed, and ten old ReplicaSets are kept for rollbacks.

03

Steering a rollout with kubectl rollout

rollout status waits until the rollout finishes and exits non-zero if it fails or times out, which makes it a natural deployment gate in CI. rollout history lists revisions, one per ReplicaSet, and --revision=N shows one revision's template. rollout undo copies the previous revision's template, or the one named with --to-revision, back into the Deployment; the old ReplicaSet scales up again and receives a new revision number, so numbers only grow. rollout pause stops the controller from acting on template changes, so you can change image, resources and environment and release them as one rollout with rollout resume. rollout restart sets a kubectl.kubernetes.io/restartedAt annotation in the template, which replaces every Pod through a normal rolling update without changing the image.

bash
kubectl rollout status deploy/web --timeout=5m
kubectl rollout history deploy/web
kubectl rollout history deploy/web --revision=2
kubectl rollout undo deploy/web --to-revision=1
kubectl rollout pause deploy/web
kubectl set image deploy/web web=gopherforge/web:v2
kubectl set resources deploy/web -c web --requests=cpu=100m,memory=64Mi
kubectl rollout resume deploy/web
kubectl rollout restart deploy/web
Read the code

The pause, set image, set resources and resume sequence produces a single new ReplicaSet that carries both changes, instead of two rollouts back to back. undo --to-revision=1 is how you return to a known-good version when the previous one is bad as well. Every command here only edits the Deployment object; the controller does the actual work.

04

Change-cause and the progress deadline

The CHANGE-CAUSE column of rollout history comes from the kubernetes.io/change-cause annotation on each ReplicaSet, which the controller copies from the Deployment. The --record flag that used to set it is deprecated, so set the annotation yourself, ideally in the same manifest change as the new image so that one apply carries both. progressDeadlineSeconds, 600 by default, is how long a rollout may go without progress, such as a new Pod becoming available. When it passes, the Progressing condition turns False with reason ProgressDeadlineExceeded and rollout status fails. Kubernetes does not roll back by itself: the old ReplicaSet keeps serving within the maxUnavailable budget while you choose between undo and a fix forward. A paused rollout is not checked against the deadline.

05

Try it in the lab terminal

The manifest uses maxSurge: 1 and maxUnavailable: 0, a short progressDeadlineSeconds of 60 so the failure shows quickly, and a readiness probe on /readyz, explained in the probes lesson. The change-cause annotation lives in the manifest and names the image, so one sed edit changes both and a single apply records them together. gopherforge/web:v2-broken starts but never passes its readiness probe.

bash
kind create cluster
cat > web.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
  annotations:
    kubernetes.io/change-cause: "deploy gopherforge/web:v1"
spec:
  replicas: 4
  progressDeadlineSeconds: 60
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: web
        image: gopherforge/web:v1
        ports:
        - containerPort: 8080
        readinessProbe:
          httpGet:
            path: /readyz
            port: 8080
          periodSeconds: 2
EOF
kubectl apply -f web.yaml
kubectl rollout status deploy/web
kubectl expose deploy web --port 80 --target-port 8080
sed -i 's/web:v1/web:v2/g' web.yaml
kubectl apply -f web.yaml
kubectl rollout status deploy/web
kubectl rollout history deploy/web
sed -i 's/web:v2/web:v2-broken/g' web.yaml
kubectl apply -f web.yaml
kubectl rollout status deploy/web
kubectl get rs
traffic run svc/web --rps 50 --duration 5s
kubectl rollout undo deploy/web
kubectl rollout history deploy/web
Read the code

The v2 rollout reports 1, 2 and 3 out of 4 new replicas updated, then 1 old replicas are pending termination, and history lists revisions 1 and 2 with their causes. The v2-broken rollout stops at 1 out of 4 and ends with error: deployment "web" exceeded its progress deadline. get rs shows the new ReplicaSet at 1 desired, 0 ready, beside the v2 ReplicaSet with 4 ready, and traffic run reports 100.0% ok from those four Pods. After undo, history lists 1, 3 and 4: revision 2 became 4. web.yaml still says v2-broken, so fix it before the next apply.

TAKE THIS WITH YOU

A rolling update is bounded by maxSurge and maxUnavailable and only advances as new Pods become available, so a release that never gets ready stalls while the old Pods keep serving. Record a change-cause with every change, gate deployments on rollout status, and roll back deliberately with rollout undo.

Check your understanding

A Deployment with 10 replicas uses the default 25% maxSurge and maxUnavailable. Which bounds does the controller keep during a rollout?

Go to the source

Progress stays in this browser. No account needed.