K8s by Example: Rolling Updates

Rolling updates replace Pods incrementally. Old Pods serve traffic while new ones start. Readiness probes gate traffic to new Pods. This enables zero-downtime deployments.

deployment-rolling.yaml

Rolling updates are the default strategy for Deployments. maxUnavailable controls how many Pods can be down. maxSurge controls how many extra Pods can exist during update.

spec:
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 0
      maxSurge: 1
  replicas: 3
deployment-strategy.yaml

First example: fast rollout with 25%, allows some downtime. Second example: maxUnavailable: 0 ensures zero downtime because new Pods must be ready before old ones terminate. Values can be absolute numbers or percentages.

rollingUpdate:
  maxUnavailable: 25%
  maxSurge: 25%
---
rollingUpdate:
  maxUnavailable: 0
  maxSurge: 1
terminal

Trigger rollout by changing the Pod template. Image tag, env vars, or labels all trigger updates. Use rollout status to watch progress and pause/resume for canary-style deploys.

$ kubectl set image deployment/my-app app=my-app:v2.0.0
deployment.apps/my-app image updated

$ kubectl rollout status deployment/my-app
Waiting for deployment "my-app" rollout to finish: 1 of 3 updated...
Waiting for deployment "my-app" rollout to finish: 2 of 3 updated...
deployment "my-app" successfully rolled out

$ kubectl rollout pause deployment/my-app
deployment.apps/my-app paused

$ kubectl rollout resume deployment/my-app
deployment.apps/my-app resumed
terminal

Rollback instantly to a previous revision. Old ReplicaSets are preserved for this purpose. Use history to see available revisions.

$ kubectl rollout history deployment/my-app
REVISION  CHANGE-CAUSE
1         Initial deployment
2         kubectl set image deployment/my-app app=my-app:v1.1.0
3         kubectl set image deployment/my-app app=my-app:v2.0.0

$ kubectl rollout undo deployment/my-app
deployment.apps/my-app rolled back

$ kubectl rollout undo deployment/my-app --to-revision=1
deployment.apps/my-app rolled back
deployment-recreate.yaml

Recreate strategy stops all old Pods before starting new ones. Use when you can’t run two versions simultaneously (database schema incompatibility, license limits). Causes downtime during updates.

spec:
  strategy:
    type: Recreate

Index | GitHub | Use arrow keys to navigate |