Overview
Kubernetes has steadily become the go-to solution for container orchestration, easing the deployment, scaling, and operation of application containers across clusters of hosts. However, even with the best planning, deployments might not always go according to plan. When an update to a Kubernetes deployment doesn’t behave as expected, a rollback is often necessary. This guide will provide you with a practical approach to rolling back deployments in Kubernetes, so you can recover quickly from unforeseen issues with your application deployments.
Understanding Rollbacks
A rollback in Kubernetes reverts a Deployment to a previous state. This can be an earlier version of your application or a previous configuration that was known to work. Rollbacks are crucial for maintaining service stability and uptime.
Prerequisites
Before getting started with step #1, make sure you have:
- kubectl command line tool installed
- Basic understanding of Kubernetes concepts (Pods, Deployments, etc.)
- Access to a running Kubernetes cluster
Step-by-Step Instructions
Step 1: Deploying Your Application
Before you can rollback, you need a deployment. Here’s an example Deployment manifest to create a simple nginx application:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 2
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports:
- containerPort: 80
Use kubectl to apply this deployment:
kubectl apply -f nginx-deployment.yaml
Step 2: Updating the Deployment
Let’s update the nginx image to version 1.18.1:
kubectl set image deployment/nginx-deployment nginx=nginx:1.18.1 --record
This will trigger a rolling update.
Step 3: Verifying the Update
Check the status of the rollout:
kubectl rollout status deployment/nginx-deployment
If something went wrong with the update and your application stops functioning correctly, you’ll want to rollback.
Step 4: Rolling Back a Deployment
Here’s how to rollback to the previous version of your application:
kubectl rollout undo deployment/nginx-deployment
This will revert the deployment to the previous state. If you want to rollback to a specific revision:
kubectl rollout undo deployment/nginx-deployment --to-revision=2
Step 5: Monitoring Rollback Success
After rolling back, monitor your application to ensure it’s functioning as expected.
kubectl get deployments
kubectl describe deployments nginx-deployment
Best Practices for Rollbacks
- Test deployments in a staging environment before pushing to production.
- Use the –record flag with kubectl to keep a history of updates which helps in rollback scenarios.
- Regularly back up your cluster’s state, so you can restore from backups if needed.
- Monitor your deployments closely after making changes, to quickly catch any issues.
- Ensure you understand the involved components’ version histories.
For more complex rollbacks, you may need to perform additional steps such as rolling back a ConfigMap or Secret that was changed along with the deployment. This typically involves reverting the config changes manually and triggering another update.
Conclusion
Rolling back deployments in Kubernetes allows you to quickly revert changes when things don’t go as planned. It is a powerful feature that can save you from extended downtime or unexpected behavior in your applications. By following this practical guide and making careful updates with close monitoring, you should have more confidence in managing rollbacks and maintaining a stable service environment.