
Rolling updates and rollbacks
Master the art of the safe transition. Learn to fine-tune your deployment strategies, monitor rollout health, and perform lightning-fast recoveries when production goes sideways.
Rolling Updates and Rollbacks: The Art of the Safe Transition
In Module 3, we introduced the concept of the Deployment and how it manages pods. We briefly touched on the idea of a "Rolling Update." But in a high-stakes production environment—where every second of downtime costs thousands of dollars—"briefly touching" on updates is not enough. You need to be a master of the transition.
How do you ensure that your new FastAPI version is actually working before K8s kills the old version? How do you handle a "Slow-rolling" failure where the app crashes only after 100 users hit it? How do you coordinate an update across multiple services?
In this lesson, we will deep dive into the mechanics of Rolling Updates. We will master the parameters of maxSurge and maxUnavailable, learn to use Rollout History, and understand the critical role of Readiness Probes in preventing a bad deployment from ever reaching your users.
1. The Strategy: RollingUpdate vs. Recreate
Kubernetes offers two primary ways to replace old Pods with new ones.
A. Recreate (The "Blunt Force" Method)
K8s kills ALL existing pods simultaneously and then starts the new ones.
- Pros: Simple. No risk of two different versions of your app being running at the same time (good for some legacy databases).
- Cons: DOWNTIME. Your app will be offline for the seconds (or minutes) it takes for the new pods to boot.
B. RollingUpdate (The "Smooth" Method)
K8s replaces old pods with new ones incrementally.
- Pros: ZERO DOWNTIME. Users never see an error.
- Cons: For a short period, some users will be on Version 1 and some on Version 2. Your backend must be "Backward Compatible."
2. Fine-Tuning the Rollout: Surge and Unavailability
Inside your Deployment YAML, you have two "Knobs" you can turn to control the speed and safety of an update.
maxSurge
How many extra pods can K8s create above your desired replica count during an update?
maxSurge: 25%: If you have 4 replicas, K8s can start 1 new pod before killing any old ones.maxSurge: 1: Exactly one extra pod.
maxUnavailable
How many pods can be "Down" at any given time during an update?
maxUnavailable: 0: CRITICAL FOR PRODUCTION. This tells K8s: "Do not kill an old pod until a new one is healthy and ready to take its place."
The "Gold Standard" Configuration:
spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
This ensures that your capacity never drops below 100%, and you only have one extra container's worth of resource overhead during the switch.
3. Monitoring the Rollout
Once you apply a change, you shouldn't just walk away. You need to watch the "Front Line."
# Watch the progress in real-time
kubectl rollout status deployment/ai-agent-deployment
# See the history of all changes
kubectl rollout history deployment/ai-agent-deployment
The "Deadlock" Scenario
If your new image has a bug that causes it to crash on boot, rollout status will show it "Waiting" forever. Because we set maxUnavailable: 0, K8s will Pause the rollout. It won't kill any more old pods because no new pods have successfully passed their readiness check.
- The Result: 3 old pods keep serving users. 1 new pod keeps crashing. The user never notices the failure. This is the ultimate safety net of Kubernetes.
4. Disaster Recovery: The Instant Rollback
Despite our best efforts, sometimes "v2" looks healthy but starts throwing internal errors once the load hits.
Reverting the Change:
# Immediately undo the last deployment
kubectl rollout undo deployment/ai-agent-deployment
Kubernetes will immediately reverse the process, scaling the "Old" ReplicaSet back up and the "New" one back down. In a matter of seconds, your production environment is back to the last known stable version.
5. Visualizing the Rollback Process
sequenceDiagram
participant User as Developer
participant RS_New as New RS (Broken v2)
participant RS_Old as Old RS (Stable v1)
User->>User: "Oh no! v2 is buggy!"
User->>RS_Old: Rollout Undo
RS_Old->>RS_Old: Scale Up to 1... 2... 3
RS_New->>RS_New: Scale Down to 2... 1... 0
Note over RS_Old, RS_New: Production restored to v1
6. Practical Example: A Continuous Deployment (CD) Script
In a professional GitHub Actions pipeline, you want to automate the verification of a rollout.
# 1. Apply the new version
kubectl apply -f k8s/deployment.yaml
# 2. Wait for it to finish (or timeout after 5 mins)
kubectl rollout status deployment/my-app --timeout=300s
# 3. If it fails, automatically rollback
if [ $? -ne 0 ]; then
echo "Deployment failed! Rolling back..."
kubectl rollout undo deployment/my-app
exit 1
fi
7. AI Implementation: Updating LangGraph Agents Safely
When you update an AI agent built with LangGraph, you might be changing the "System Prompt" or the "Tool Set."
The Blue-Green Consideration:
If the change is massive (e.g., switching from Claude 2 to Claude 3), a "Rolling Update" might be confusing for users who get different types of answers in the same session.
In this case, you might use a Blue-Green Deployment:
- Deploy the "Green" version as a completely separate Deployment.
- Test it thoroughly.
- Switch the Service selector to point to "Green."
- Delete the "Blue" version. This gives you a "Hard Switch" rather than a gradual transition.
8. Summary and Key Takeaways
- RollingUpdate: The standard for high-availability systems.
- maxUnavailable: 0: The most important setting for preventing downtime during errors.
- Readiness Probes: The "Signal" that tells K8s it's safe to continue the rollout.
- Rollout Status: Always watch the progress of your changes.
- Undo: Your "Big Red Button" for instant disaster recovery.
In the next lesson, we will look at how we target specific groups of pods for updates and monitoring using Labels and Selectors.
9. SEO Metadata & Keywords
Focus Keywords: Kubernetes rolling update tutorial, K8s maxSurge and maxUnavailable explained, zero-downtime deployment Kubernetes, Kubernetes rollback command undo, rollout status K8s CI/CD, blue-green vs rolling update K8s.
Meta Description: Master the operational transition of your applications on Kubernetes. Learn how to configure safe rolling updates, interpret rollout status, and perform lightning-fast rollbacks to ensure your production AI and web services stay online 24/7.