Kubernetes Using Kubernetes Deployments for Stateless Workloads Created: 25 Jul 2026 Updated: 25 Jul 2026

Kubernetes Rolling Updates and Rollbacks

When you deploy a new version of your application, you do not want to take the old version down all at once — that would cause downtime. Rolling updates solve this by gradually replacing old Pods with new ones. If something goes wrong, rollbacks let you revert to the previous version with a single command.

Think of it like replacing light bulbs in a long hallway one at a time. The hallway is never dark because most bulbs are always on. If the new bulbs are the wrong color, you can swap the old ones back in.

Rolling updates are the default strategy for Kubernetes Deployments. You control the speed and safety of the rollout with two parameters: maxSurge and maxUnavailable.

Core Concepts

Step 1 — Deployment Strategy Types

Kubernetes Deployments support two strategy types:

StrategyBehaviorDowntime?
RollingUpdate (default)Gradually replaces old Pods with new ones.No
RecreateTerminates all old Pods first, then creates new ones.Yes

Use Recreate only when your app cannot tolerate running two versions simultaneously (e.g., database schema conflicts). In all other cases, use RollingUpdate.

Step 2 — maxSurge and maxUnavailable

These two parameters control the pace of a rolling update:

  1. maxSurge — the maximum number of Pods that can be created above the desired replica count during the update. Can be an absolute number or a percentage.
  2. maxUnavailable — the maximum number of Pods that can be unavailable during the update. Can be an absolute number or a percentage.
spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0

With these settings, Kubernetes creates 1 new Pod first, waits until it is ready, then terminates 1 old Pod. This ensures zero downtime — there is always at least the desired number of Pods running.

Step 3 — How a Rolling Update Works

When you update the container image (or any field in the Pod template), Kubernetes:

  1. Creates a new ReplicaSet with the updated Pod template.
  2. Scales the new ReplicaSet up gradually (respecting maxSurge).
  3. Scales the old ReplicaSet down gradually (respecting maxUnavailable).
  4. When all new Pods are ready and all old Pods are terminated, the update is complete.

Step 4 — Rollback with Revision History

Kubernetes keeps a history of Deployment revisions. If a new version is buggy, you can roll back instantly:

kubectl rollout undo deployment/<deployment-name>

This reverts to the previous revision. To go back to a specific revision:

kubectl rollout undo deployment/<deployment-name> --to-revision=2

The number of revisions kept is controlled by spec.revisionHistoryLimit (default: 10).

Step 5 — The Recreate Strategy

When set to Recreate, Kubernetes terminates all existing Pods before creating new ones. This causes downtime but guarantees that only one version runs at a time.

spec:
strategy:
type: Recreate

Hands-On: Kubernetes Commands

Trigger a Rolling Update

kubectl set image deployment/<name> <container>=<new-image>

Example:

kubectl set image deployment/logistics-api logistics-api=myregistry.azurecr.io/logistics-api:2.0

Watch the Rollout Progress

kubectl rollout status deployment/<name>

This command blocks until the rollout is complete or fails.

View Rollout History

kubectl rollout history deployment/<name>

Shows all revisions with their change causes.

View Details of a Specific Revision

kubectl rollout history deployment/<name> --revision=2

Roll Back to the Previous Version

kubectl rollout undo deployment/<name>

Roll Back to a Specific Revision

kubectl rollout undo deployment/<name> --to-revision=2

Pause and Resume a Rollout

kubectl rollout pause deployment/<name>
kubectl rollout resume deployment/<name>

Pausing lets you make multiple changes to the Deployment without triggering multiple rollouts.

Step-by-Step Example

Scenario — Zero-Downtime Update of a Logistics API

You will deploy version 1.0 of a logistics-api, then perform a rolling update to version 2.0, and finally roll back to version 1.0 if needed.

Step 1 — Deploy Version 1.0

Save the following as logistics-api-deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
name: logistics-api
labels:
app: logistics-api
annotations:
kubernetes.io/change-cause: "Initial release v1.0"
spec:
replicas: 3
revisionHistoryLimit: 5
selector:
matchLabels:
app: logistics-api
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
template:
metadata:
labels:
app: logistics-api
version: v1
spec:
containers:
- name: logistics-api
image: myregistry.azurecr.io/logistics-api:1.0
ports:
- containerPort: 8080
env:
- name: ASPNETCORE_URLS
value: "http://+:8080"
resources:
requests:
cpu: "200m"
memory: "256Mi"
limits:
cpu: "1"
memory: "512Mi"
readinessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 15
periodSeconds: 20
kubectl apply -f logistics-api-deployment.yaml

Step 2 — Verify All Pods Are Running

kubectl get pods -l app=logistics-api

You should see 3 Pods in Running state with READY 1/1.

Step 3 — Trigger a Rolling Update to Version 2.0

kubectl set image deployment/logistics-api logistics-api=myregistry.azurecr.io/logistics-api:2.0
kubectl annotate deployment/logistics-api kubernetes.io/change-cause="Update to v2.0" --overwrite

Step 4 — Watch the Rollout

kubectl rollout status deployment/logistics-api

You will see messages like "Waiting for deployment ... rollout to finish: 1 out of 3 new replicas have been updated..." and eventually "deployment ... successfully rolled out".

Step 5 — Check Rollout History

kubectl rollout history deployment/logistics-api

Shows revision 1 (v1.0) and revision 2 (v2.0).

Step 6 — Roll Back to Version 1.0

kubectl rollout undo deployment/logistics-api
kubectl rollout status deployment/logistics-api

The Deployment reverts to the v1.0 Pod template. Verify:

kubectl describe deployment/logistics-api | findstr Image

Summary

  1. RollingUpdate (default) replaces Pods gradually — no downtime.
  2. Recreate terminates all old Pods first — causes downtime but guarantees single-version.
  3. maxSurge controls how many extra Pods can exist during update.
  4. maxUnavailable controls how many Pods can be down during update.
  5. Use kubectl rollout undo to instantly roll back to a previous revision.
  6. Use kubectl rollout pause/resume to batch multiple changes into one rollout.
  7. Set revisionHistoryLimit to control how many old ReplicaSets are kept.


Share this lesson: