Rollback Kubernetes Deployments

Rollback Kubernetes Deployments

Estimated reading time: 3 minutes

Rolling back a Kubernetes deployment is an essential capability for managing application stability. Whether it’s due to a failed update or an unexpected bug, Kubernetes makes it easy to revert to a previous deployment version. This guide explains how to perform deployment rollbacks in Kubernetes.



What is a Kubernetes Deployment Rollback?

In Kubernetes, a rollback is the process of reverting a deployment to a previous revision. Each deployment update creates a new revision, allowing administrators to restore a known working state when something goes wrong.

Key Features:

  • Kubernetes maintains revision history for deployments.
  • Rollbacks are quick and do not require downtime when properly managed.
  • Custom configurations allow fine-grained control over rollback behavior.

Pre-Requisites

  1. Kubernetes Cluster: Ensure you have a running Kubernetes cluster.
  2. Kubectl Access: The kubectl CLI tool must be configured to interact with the cluster.
  3. Deployment Resource: A deployment resource must already exist in the cluster.

Checking Deployment History

Before rolling back, it’s important to review the deployment’s revision history.

Command:

Example:

Output:

This command shows all recorded revisions for the specified deployment.



Performing a Rollback

To rollback a deployment, use the kubectl rollout undo command.

Command:

Example:

This restores the deployment to the most recent working revision.


Rolling Back to a Specific Revision

If you need to rollback to a specific revision, specify the revision number.

Command:

Example:

This command restores the deployment to revision 2.



Verifying the Rollback

After performing a rollback, verify the deployment status to ensure the process was successful.

Command:

Example:

Output:


Best Practices for Rollbacks

1. Set Change Causes: Annotate deployments with --record during updates to maintain clear revision histories.

2. Monitor Deployment Health: Use kubectl rollout status to track progress and identify issues.

3. Limit Revision History: Configure the revisionHistoryLimit field in your deployment manifest to control how many revisions are stored:

4. Automate Alerts: Use monitoring tools like Prometheus or Grafana to trigger alerts during deployment failures.



Example Scenario: Rolling Back a Failed Update

Update a deployment:

Check the status:

If the update causes errors, rollback to the previous version:

Confirm the rollback:


Common Issues and Solutions

No Revisions Available:

  • Cause: Deployment has no prior revisions.
  • Solution: Ensure updates are performed with the --record flag.

Rollback Fails:

  • Cause: Underlying pods may not start successfully.
  • Solution: Inspect pod logs with:

Slow Rollback:

  • Cause: High resource usage or insufficient capacity.
  • Solution: Monitor cluster resources and optimize autoscaling.


  1. Kubernetes Official Documentation: Deployments
  2. Kubernetes Official Documentation: Rollout Management

Leave a Reply

Your email address will not be published. Required fields are marked *