Browse topics

Rolling updates and rollback

Version
1.37
Review
Draft

Summary#

With the default RollingUpdate strategy, a Deployment replaces old Pods gradually. maxSurge limits extra Pods during the update. Pods still terminating can temporarily push the total above that limit. maxUnavailable limits how far available replicas can drop below the desired count. kubectl rollout undo returns the Pod template to an earlier revision.

Syntax#

sh
kubectl set image deployment/NAME CONTAINER=IMAGE
kubectl rollout status deployment/NAME --timeout=2m
kubectl rollout history deployment/NAME
kubectl rollout undo deployment/NAME [--to-revision=N]
yaml
spec:
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0

Example#

The new image cannot be pulled. The rollout stalls, both old Pods stay available, and an undo restores the previous template.

sh
(
  set -eu
  ns="ref-rollout-$$"
  kubectl create namespace "$ns" >/dev/null
  trap 'kubectl delete namespace "$ns" >/dev/null' EXIT

  kubectl apply -n "$ns" -f - >/dev/null <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 2
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: app
          image: registry.k8s.io/pause:3.10
EOF
  kubectl rollout status -n "$ns" deployment/web --timeout=120s >/dev/null

  kubectl set image -n "$ns" deployment/web app=registry.invalid/web:missing
  if kubectl rollout status -n "$ns" deployment/web --timeout=15s >/dev/null 2>&1; then
    echo "rollout finished"
  else
    echo "rollout did not finish"
  fi
  kubectl get -n "$ns" deployment web -o jsonpath='available={.status.availableReplicas}{"\n"}'

  kubectl rollout undo -n "$ns" deployment/web
  kubectl rollout status -n "$ns" deployment/web --timeout=120s >/dev/null
  kubectl get -n "$ns" deployment web -o jsonpath='image={.spec.template.spec.containers[0].image}{"\n"}'
  kubectl rollout history -n "$ns" deployment/web | grep '^[0-9R]'
)

Expected output#

Text
deployment.apps/web image updated
rollout did not finish
available=2
deployment.apps/web rolled back
image=registry.k8s.io/pause:3.10
REVISION  CHANGE-CAUSE
2         <none>
3         <none>

kubectl also warns on standard error that the undo does not update the annotation recorded by kubectl apply.

Common mistakes#

An undo changes the live Deployment, not your manifest. Fix or revert the manifest too, or the next apply can bring the bad template back. When progressDeadlineSeconds elapses without progress, the Deployment reports Progressing=False with reason ProgressDeadlineExceeded. Rollback is a separate action.

Version applicability#

The example targets Kubernetes 1.37. maxSurge and maxUnavailable both default to 25% and cannot both be zero. An undo restores only the Pod template, not the replica count. The old --record flag, once used to fill CHANGE-CAUSE, is deprecated.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0