Rolling Updates and Rollbacks
🎯 Introduction
We previously used a Deployment to run two order Pods. Now we want to release a new version, changing the /api/info response from v1 to v2.
If we delete both old Pods before starting new ones, there may be a period when no instance can handle orders. Can we start the new version, check that it works, and then gradually stop the old version?
The default Deployment strategy, RollingUpdate, is designed for this. We will update the order service from v1 to v2, then deliberately deploy an invalid image to learn how to recover the previous working version.
🧱 What is a rolling update?
Suppose the orders Deployment currently runs two Pods using course-orders:v1.
When updating to v2, Kubernetes does not simply stop both old Pods at once. In the diagram, watch when new Pods appear and when old Pods exit.
How do two order Pods roll from v1 to v2?
Read from top to bottom. Kubernetes waits for the new version to be ready before phasing out the old one.
- 1
Before the update: two old Pods
Both v1 Pods are ready and handling order requests.
Pod v1
Ready
Pod v1
Ready
- 2
Start one new Pod
The new v2 Pod is starting. Both v1 Pods remain temporarily.
Pod v1
Ready
Pod v1
Ready
Pod v2
Starting
- 3
Continue replacing Pods
Once the first v2 Pod is ready, remove one v1 Pod and start the next v2 Pod.
Pod v1
Ready
Pod v2
Ready
Pod v2
Starting
- 4
Update complete: two new Pods
Both v2 Pods are ready and all old Pods have exited.
Pod v2
Ready
Pod v2
Ready
This is one possible sequence for the default rolling update strategy with two replicas. Terminating Pods are omitted. Readiness and rollout settings affect the actual sequence.
Why wait for new Pods to become ready? Starting a container does not mean Node.js can handle requests. Readiness probes help Kubernetes determine whether a new Pod should receive Service traffic.
Rolling updates aim to minimize release interruptions, but cannot guarantee zero downtime on their own. We will learn the basic replacement process without covering complex rollout parameters.
🛠 Releasing order service v2
1. Change the Node.js code
Return to orders-app, open server.js, and find /api/info.
The original response is:
app.get('/api/info', (req, res) => {
res.json({ service: 'orders', version: 'v1' })
})
Change version to v2:
app.get('/api/info', (req, res) => {
res.json({ service: 'orders', version: 'v2' })
})
This changes the application code itself. The next image you build will contain the new endpoint response.
APP_VERSIONin the ConfigMap article was an environment variable for observing configuration changes. Here we change the response written inserver.js. Changing a configuration variable is not the same as upgrading the application image.
2. Build the image and load it into kind
Run this in orders-app, where the Dockerfile is located:
docker build -t course-orders:v2 .
Simulated output excerpt:
[+] Building ... FINISHED
=> exporting to image
=> => naming to docker.io/library/course-orders:v2
After a successful build, load the image into the course's kind cluster:
kind load docker-image course-orders:v2 --name node-course
Simulated output:
Image: "course-orders:v2" with ID "sha256:..." not yet present on node "node-course-control-plane", loading...
Why is kind load still needed? The image was built in your local Docker environment, while kind nodes have their own image storage. Load it first so new Pods can find course-orders:v2 directly.
3. Observe Kubernetes replacing Pods
First, confirm that the old version is running:
kubectl get deployment orders -n course
Simulated output:
NAME READY UP-TO-DATE AVAILABLE AGE
orders 2/2 2 2 30m
To observe old and new Pods coexisting, open two terminals.
Terminal A: start watching Pods.
kubectl get pods -n course -l app=orders -w
-w watches changes continuously. Leave this terminal running.
Terminal B: change the Deployment image to v2.
kubectl set image deployment/orders orders=course-orders:v2 -n course
Simulated output:
deployment.apps/orders image updated
deployment/orders identifies the Deployment. orders=course-orders:v2 changes the container named orders to the v2 image.
In terminal A, you may observe changes like these during the update. Names and ordering are illustrative:
NAME READY STATUS AGE
orders-old-a 1/1 Running 10m
orders-old-b 1/1 Running 10m
orders-new-c 0/1 ContainerCreating 2s
orders-new-c 1/1 Running 8s
orders-old-a 1/1 Terminating 10m
...
You do not need to memorize the names. Focus on two things: old Pods may still run as new Pods appear, and old Pods gradually exit once new Pods become ready.
The local order service starts quickly, so the update may finish almost instantly. Missing some intermediate output does not mean a rolling update did not occur.
When finished observing, press Ctrl+C in terminal A to stop watching.
4. Confirm the release succeeded
In terminal B, check the Deployment:
kubectl rollout status deployment/orders -n course --timeout=120s
Simulated output:
deployment "orders" successfully rolled out
The new version has met the Deployment's rollout completion conditions. Check the final Pod count:
kubectl get pods -n course -l app=orders
Simulated output:
NAME READY STATUS RESTARTS AGE
orders-new-c 1/1 Running 0 48s
orders-new-d 1/1 Running 0 36s
Finally, verify that the endpoint returns v2. Run this in terminal A:
kubectl port-forward -n course service/orders 8080:80
Simulated output:
Forwarding from 127.0.0.1:8080 -> 3000
Leave terminal A running, then run this in terminal B:
curl http://127.0.0.1:8080/api/info
Simulated response:
{"service":"orders","version":"v2"}
This confirms that the application returns the version from the new code.
Note: kubectl port-forward service/orders selects a single matching Pod for forwarding; it does not cycle through all order Pods. We use kubectl get pods -w to observe replacement and curl to verify the endpoint after the update completes. Repeated curl requests do not demonstrate load balancing between old and new Pods. Forwarding may also stop if the selected Pod exits.
After verification, press Ctrl+C in terminal A.
🔄 How do you roll back a failed release?
v2 is now working. Suppose the next release specifies an incorrect image name and Kubernetes cannot start new Pods. How can you recover?
1. Deliberately deploy a nonexistent image
Run:
kubectl set image deployment/orders orders=course-orders:v999 -n course
Simulated output:
deployment.apps/orders image updated
image updated only means the desired image name in the Deployment changed. It does not mean new Pods can start.
Inspect the Pods:
kubectl get pods -n course -l app=orders
Simulated output; names distinguish old and new Pods:
NAME READY STATUS RESTARTS AGE
orders-v2-a 1/1 Running 0 3m
orders-v2-b 1/1 Running 0 3m
orders-v999-c 0/1 ImagePullBackOff 0 35s
You may see ErrImagePull before ImagePullBackOff. The new Pod cannot obtain its image. The old v2 Pods normally remain temporarily, rather than all stopping immediately because of the invalid image.
Check rollout status with a bounded wait:
kubectl rollout status deployment/orders -n course --timeout=30s
Simulated output excerpt:
Waiting for deployment "orders" rollout to finish...
error: timed out waiting for the condition
This timeout means the rollout did not finish within the waiting period. It does not mean Kubernetes rolled back automatically.
To inspect the failure cause, run:
kubectl describe pods -n course -l app=orders
The new Pod's Events may include:
Warning Failed Failed to pull image "course-orders:v999"
Normal BackOff Back-off pulling image "course-orders:v999"
2. Restore the working v2 release
Deployments can retain previous Pod template revisions. Roll back with:
kubectl rollout undo deployment/orders -n course
Simulated output:
deployment.apps/orders rolled back
Because we deployed v2 immediately before changing the image to invalid v999, this attempts to restore the preceding v2 template.
Wait for recovery to complete:
kubectl rollout status deployment/orders -n course --timeout=120s
Simulated output:
deployment "orders" successfully rolled out
Finally, check the Deployment's current image:
kubectl get deployment orders -n course -o jsonpath='{.spec.template.spec.containers[0].image}{"\n"}'
Simulated output:
course-orders:v2
The invalid image has been removed and the Deployment is back on v2. You can also restart port forwarding and request /api/info to confirm it still returns v2.
3. Inspect rollout history
kubectl rollout history deployment/orders -n course
Simulated output; revision numbers depend on how many earlier changes you made:
deployment.apps/orders
REVISION CHANGE-CAUSE
6 <none>
7 <none>
REVISION is the Deployment revision number, not an image tag. CHANGE-CAUSE may show <none> because Kubernetes does not automatically write a full release description for every change.
Rollback scope:
rollout undoprimarily restores a previous Pod template, such as its image and container configuration. It does not undo database migrations automatically. If old and new applications share a database, schema and API compatibility still matter throughout the rollout.
❓ Questions and answers
Q: Does a rolling update guarantee that no user request fails?
No. Gradual Pod replacement reduces interruption risk, but readiness checks, shutdown handling, available capacity, and other factors still matter.
Q: Why check rollout status after a successful kubectl set image?
Changing the image name in a Deployment does not mean new Pods are running. Missing images or application startup failures can stall the rollout.
Q: Does rollback also update my local deployment.yaml?
No. kubectl set image and kubectl rollout undo modify the cluster's Deployment, not your local YAML. After the exercise, update the image in local deployment.yaml to the final course-orders:v2 so applying the old file later does not revert it to v1. Keep all other fields.
🧪 Exercise
Complete a successful release → failed release → rollback recovery sequence:
- Confirm that the order Deployment has two ready v2 Pods.
- Deliberately change the image to nonexistent
course-orders:missingand inspect the new Pod's status and events. - Recover with
kubectl rollout undoand confirm that the Deployment usescourse-orders:v2afterward. - Explain why a successful
kubectl set imagecommand with an invalid image does not mean the release succeeded.
Success criteria: Observe an image pull error on a new Pod, restore two ready v2 Pods through rollback, and explain why rolling updates do not delete all old Pods at the start.
🎉 You now know how a Deployment gradually replaces order service v1 with v2, and how to investigate, roll back, and verify recovery when a new version cannot start.
