DEV Community

Cover image for Rollback Kubernetes Deployment: Rolling Updates and Rollout Undos
Avesh
Avesh

Posted on

Rollback Kubernetes Deployment: Rolling Updates and Rollout Undos

Introduction

Kubernetes has revolutionized the way we deploy and manage applications, enabling teams to maintain high availability and reliability. Among its many features, Kubernetes deployments stand out as a robust mechanism for application version management. In this article, we will explore the concepts of rolling updates and rollbacks, their significance, and practical implementation. You will learn how to ensure application uptime during changes and revert to stable states in case of issues.

This guide covers:

  1. Understanding deployments
  2. Performing rolling updates
  3. Executing rollbacks effectively

1. Understanding Kubernetes Deployments

A Kubernetes Deployment is a controller that manages the lifecycle of your application. It ensures that the desired state of your application matches the current state, making deployments reliable and repeatable.

Key Concepts:

  • Replicas: The number of identical pod instances running at any time.
  • Desired State: The configuration defined in the deployment manifest.
  • Pod Template: The blueprint for creating pods, including container specifications and environment variables.

Features of Deployments:

  • Scaling: Seamlessly increase or decrease replicas to match demand.
  • Self-Healing: Automatically replace failed pods.
  • Version Control: Maintain a history of application versions for rollback.

Example Manifest:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app-container
        image: my-app:v1.0
        ports:
        - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

In the above manifest, Kubernetes ensures three pods of my-app are running using the specified image version v1.0.


2. Implementing Rolling Updates

A rolling update allows you to update an application without downtime by incrementally replacing old pods with new ones.

Key Parameters:

  • maxUnavailable: Maximum number of pods that can be unavailable during the update (default: 25%).
  • maxSurge: Maximum number of additional pods that can run during the update (default: 25%).

Example Deployment with Rolling Update Strategy:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 3
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
      maxSurge: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app-container
        image: my-app:v1.0
        ports:
        - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

In this configuration:

  • Kubernetes ensures only one pod is unavailable at a time (maxUnavailable: 1).
  • It can create one additional pod during the update process (maxSurge: 1).

3. Performing a Rolling Update

To update the application with a new image version:

Step-by-Step:

  1. Update the Deployment Image:
   kubectl set image deployment/my-app my-app-container=my-app:v1.1
Enter fullscreen mode Exit fullscreen mode
  1. Monitor the Update Progress:
   kubectl rollout status deployment/my-app
Enter fullscreen mode Exit fullscreen mode
  1. Verify the Update: Ensure the pods are running the new version:
   kubectl get pods -l app=my-app
Enter fullscreen mode Exit fullscreen mode

Kubernetes incrementally updates the pods, ensuring minimal downtime.


4. Rollout Undo and Rollback Strategy

If issues arise after an update, you can revert to the previous stable state using a rollback.

Performing a Rollout Undo:

  1. Undo the Deployment:
   kubectl rollout undo deployment/my-app
Enter fullscreen mode Exit fullscreen mode
  1. View Rollout History:
   kubectl rollout history deployment/my-app
Enter fullscreen mode Exit fullscreen mode

Output Example:

   REVISION  CHANGE-CAUSE
   1         Initial deploy
   2         Updated image to v1.1
Enter fullscreen mode Exit fullscreen mode
  1. Rollback to a Specific Revision:
   kubectl rollout undo deployment/my-app --to-revision=1
Enter fullscreen mode Exit fullscreen mode

Practical Example:

Suppose v1.1 introduces a bug. Reverting to v1.0 ensures application stability:

kubectl rollout undo deployment/my-app
Enter fullscreen mode Exit fullscreen mode

This command restores the application to the previous version (v1.0).


5. Best Practices for Rollbacks

  • Thorough Testing: Validate changes in staging environments.
  • Versioning: Use semantic versioning for container images (e.g., my-app:v1.0, my-app:v1.1).
  • Annotations: Add metadata to deployments for clarity:
  metadata:
    annotations:
      change-cause: "Updated image to v1.1 for bug fix"
Enter fullscreen mode Exit fullscreen mode
  • Monitoring: Use tools like Prometheus and Grafana to track metrics and identify issues.

6. Advanced Rollback Techniques

Canary Deployments:

Deploy the update to a small subset of users first. For example:

replicas: 5
strategy:
  type: RollingUpdate
  rollingUpdate:
    maxUnavailable: 0
    maxSurge: 2
Enter fullscreen mode Exit fullscreen mode

This ensures gradual adoption with monitoring before full rollout.

Blue-Green Deployments:

Maintain two environments (blue: stable, green: updated). Switch traffic to the green environment only after validation.

Strategy Downtime Rollback Complexity
Rolling Update Minimal Moderate
Blue-Green None Low

7. Troubleshooting Rollback Issues

  1. Check Pod Events:
   kubectl describe pods
Enter fullscreen mode Exit fullscreen mode
  1. View Logs:
   kubectl logs <pod-name>
Enter fullscreen mode Exit fullscreen mode
  1. Monitor Deployment:
   kubectl get pods --watch
Enter fullscreen mode Exit fullscreen mode

Common Issues:

  • Pods stuck in Pending or CrashLoopBackOff.
  • Misconfigured selectors in the deployment manifest.

Conclusion

Mastering rolling updates and rollbacks in Kubernetes ensures high availability and stability for your applications. By following the techniques and best practices outlined in this guide, you can confidently manage deployments and mitigate risks.

Call to Action

  • Share your experiences with rolling updates and rollbacks in the comments below.
  • Explore the official Kubernetes documentation and practice deployment strategies in your cluster.

Top comments (0)