How to Rollback Your Kubernetes Deployment with Kubectl

Learn how to rollback a Kubernetes deployment to a previous version using kubectl commands. Follow our guide to ensure stability in your cluster.

Patrick Londa
Author
Mar 13, 2023
 • 
 min read
Share this post

If you encounter performance issues or bugs caused by a new Deployment to your Kubernetes cluster, you should rollback to a previous version while you investigate. By doing this, you’ll minimize downtime and be able to resolve issues like data loss or security vulnerabilities introduced by the new Deployment.

In this guide, we’ll show you how to roll back your Deployment, verify that it was successful, and get details about your problematic deployment.

How to Roll Back Your Kubernetes Deployment

You can use the kubectl commands to roll back to a previously tested Kubernetes Deployment. Here’s how:

  1. Type the command to obtain the list and status of all the Deployments in your cluster:
kubectl get deployments

You’ll see an output that looks like this:

NAME                 READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment     3/3     3            3           30m
  1. Use the following command to obtain the revision history and the number of each revision for the Deployment you want to roll out. Here <deployment_name> is the name of the Deployment you want to roll back.
kubectl rollout history deployment/<deployment_name>

You’ll see an output that looks like this:

deployments "nginx-deployment"
REVISION    CHANGE-CAUSE
1           kubectl apply --filename=https://blinkops.com/examples/controllers/nginx-deployment.yaml
2           kubectl set image deployment/nginx-deployment nginx=nginx:1.12.1
3           kubectl set image deployment/nginx-deployment nginx=nginx:1.121

You can see in this example there is a typo in the 3rd revision, updating image “1.121” instead of image “1.12.1”.

  1. Next, type a rollout undo deployment command where <revision_number> is the specific number of the Kubernetes deployment you want to roll back to, and the undo command will deploy that revision. But you must remember to test the rollout in a staging environment for incident response before performing it in production, as it can have unintended consequences.
kubectl rollout undo deployment/<deployment_name> --to-revision=<revision_number>

How to Check if Your Rollback Was Successful

You can check whether the rollback to a previous Kubernetes Deployment was successful with a couple commands.

  1. This status command will show you the status of whether your rollback was successful or not:
kubectl rollout status deployment/<deployment_name> 

If the status displays a message like "successful rollout" or "rolled out," it means you succeeded in deploying the previous Kubernetes revision

  1. This command will show you the revision history of the deployment you rolled back to. Verify that the deployment has been updated correctly.
kubectl rollout history deployment/<deployment_name>

Automating Your Deployment Rollbacks with Blink:

Rolling back a problematic deployment isn’t hard, but it does require you to remember which commands to run and what information to look for. In the time it takes to manually rollback your deployment, your application may experience downtime or subpar performance.

With Blink, you can use event triggers like a PagerDuty or Datadog incident to automatically rollback to the previous deployment and update the incident ticket.

Blink Automation: Rollback Kubernetes Deployment and Resolve Datadog Incident
Blink Automation: Rollback Kubernetes Deployment and Resolve Datadog Incident

This automation in the Blink library is triggered when a related Datadog incident is created. When it runs, it does the following steps:

  1. Updates the incident timeline with the automation execution URL.
  2. Rolls back the Kubernetes Deployment.
  3. Runs a Service health-check.
  4. If the Service is healthy, it then resolves the Datadog incident.

You can import this automation from the library into your account and customize it based on your organization’s needs. For example, you can drag-and-drop new actions into the canvas to add notifications, approval steps, and conditional subflows.

You can also kick off other automations to gather troubleshooting information so your development team can quickly understand what went wrong.

With Blink, you can build your own automation from scratch or use one of our 5K pre-built automations today.

Get started with Blink today to see how easy automation can be.

Expert Tip