Kubernetes  

How to Rollback Deployment in Kubernetes Safely?

Introduction

In modern cloud-native applications, deployments happen frequently. Teams release new features, bug fixes, and updates regularly. However, not every deployment goes as planned. Sometimes, a new release introduces bugs, performance issues, or crashes.

This is where rollback in Kubernetes becomes extremely important.

Rollback allows you to safely return to a previous stable version of your application without downtime or major impact on users.

What is Rollback in Kubernetes?

Rollback in Kubernetes means reverting your application to a previous working version.

πŸ‘‰ Example:

  • Version 1 β†’ Working fine

  • Version 2 β†’ Bug introduced

πŸ‘‰ Rollback β†’ Go back to Version 1

Kubernetes makes this easy using its built-in Deployment history feature.

Why Rollback is Important in Kubernetes

1. Quick Recovery from Failed Deployments

If a new deployment fails, rollback helps you recover instantly.

πŸ‘‰ This reduces downtime and protects user experience.

2. Safer Releases

Teams can deploy confidently knowing rollback is available.

3. Maintain Application Stability

Rollback ensures your system remains stable even after failed updates.

4. Essential for CI/CD Pipelines

Rollback is a key part of modern DevOps and Kubernetes CI/CD pipelines.

How Kubernetes Handles Deployment Versions

Kubernetes keeps track of deployment history using ReplicaSets.

πŸ‘‰ Each deployment creates a new ReplicaSet.

  • Old version β†’ Old ReplicaSet

  • New version β†’ New ReplicaSet

This allows Kubernetes to switch between versions easily.

Step-by-Step: Rollback Deployment in Kubernetes

Let’s understand how to rollback safely.

Step 1: Check Deployment Status

First, check your deployment status:

kubectl get deployments

πŸ‘‰ This shows all running deployments.

Step 2: View Deployment History

kubectl rollout history deployment <deployment-name>

πŸ‘‰ Example:

kubectl rollout history deployment my-app

Output

  • Revision 1

  • Revision 2

πŸ‘‰ Each revision represents a version.

Step 3: Inspect Specific Revision (Optional)

kubectl rollout history deployment my-app --revision=1

πŸ‘‰ Helps you understand what changed.

Step 4: Perform Rollback

To rollback to previous version:

kubectl rollout undo deployment my-app

πŸ‘‰ This rolls back to the last working version.

Step 5: Rollback to Specific Revision

kubectl rollout undo deployment my-app --to-revision=1

πŸ‘‰ Useful when you want a specific version.

Step 6: Verify Rollback

kubectl rollout status deployment my-app

πŸ‘‰ Ensures rollback is successful.

Real-World Example

Imagine you deployed a new version with a bug:

kubectl apply -f deployment.yaml

After deployment:

  • Users report errors

  • API starts failing

πŸ‘‰ Immediate fix:

kubectl rollout undo deployment my-app

πŸ‘‰ Within seconds, system returns to stable state.

Understanding Rolling Updates and Rollbacks

Kubernetes uses rolling updates by default.

πŸ‘‰ This means:

  • Gradually replaces old pods

  • Minimizes downtime

If something fails:

  • Rollback restores previous pods

Best Practices for Safe Rollback in Kubernetes

1. Always Use Deployment (Not Pods)

Deployments support rollback.

πŸ‘‰ Pods alone do not support versioning.

2. Use Readiness and Liveness Probes

These ensure only healthy pods receive traffic.

πŸ‘‰ Helps detect failures early.

3. Set Proper Resource Limits

Avoid crashes due to memory or CPU issues.

4. Use Versioned Images

Do not use "latest" tag.

πŸ‘‰ Example:

myapp:v1
myapp:v2

5. Monitor Application with Logs and Metrics

Use tools like:

  • Prometheus

  • Grafana

πŸ‘‰ Helps identify issues quickly.

6. Test Before Production Deployment

Always test in staging environment.

Common Mistakes to Avoid

  • ❌ Using "latest" Docker tag

  • ❌ Not checking rollout status

  • ❌ Skipping monitoring

  • ❌ Not maintaining deployment history

Advanced Tip: Pause and Resume Deployment

You can pause deployment before making changes:

kubectl rollout pause deployment my-app

After updates:

kubectl rollout resume deployment my-app

πŸ‘‰ Useful for controlled releases.

Real-World Use Cases

Rollback in Kubernetes is used in:

  • Production outage recovery

  • Buggy feature deployment

  • Failed microservices updates

πŸ‘‰ Almost every production system uses rollback strategy.

Summary

Rollback in Kubernetes is a powerful feature that allows you to safely revert to a previous stable version of your application. By using commands like kubectl rollout undo and understanding deployment history, you can quickly recover from failures and maintain system stability. Following best practices like versioned images, monitoring, and proper testing ensures that your Kubernetes deployments remain reliable and production-ready.