How to rollback a failed Kubernetes deployment?

KubernetesKubernetesBeginner
Practice Now

Introduction

Kubernetes is a powerful container orchestration platform that simplifies the deployment and management of applications. However, even with Kubernetes, deployments can sometimes fail, leading to downtime and disruptions. In this tutorial, we'll explore how to troubleshoot and rollback a failed Kubernetes deployment, ensuring your application is running smoothly.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/TroubleshootingandDebuggingCommandsGroup(["`Troubleshooting and Debugging Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedCommandsGroup(["`Advanced Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedDeploymentGroup(["`Advanced Deployment`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/ConfigurationandVersioningGroup(["`Configuration and Versioning`"]) kubernetes/TroubleshootingandDebuggingCommandsGroup -.-> kubernetes/describe("`Describe`") kubernetes/TroubleshootingandDebuggingCommandsGroup -.-> kubernetes/logs("`Logs`") kubernetes/AdvancedCommandsGroup -.-> kubernetes/apply("`Apply`") kubernetes/AdvancedDeploymentGroup -.-> kubernetes/rollout("`Rollout`") kubernetes/ConfigurationandVersioningGroup -.-> kubernetes/version("`Version`") subgraph Lab Skills kubernetes/describe -.-> lab-416114{{"`How to rollback a failed Kubernetes deployment?`"}} kubernetes/logs -.-> lab-416114{{"`How to rollback a failed Kubernetes deployment?`"}} kubernetes/apply -.-> lab-416114{{"`How to rollback a failed Kubernetes deployment?`"}} kubernetes/rollout -.-> lab-416114{{"`How to rollback a failed Kubernetes deployment?`"}} kubernetes/version -.-> lab-416114{{"`How to rollback a failed Kubernetes deployment?`"}} end

Understanding Kubernetes Deployments

What is a Kubernetes Deployment?

A Kubernetes Deployment is a declarative way to manage the lifecycle of a set of replicated Pods. It ensures that a specified number of Pod replicas are running at any given time, and it provides a way to update those Pods in a controlled and automated manner.

Key Concepts of Kubernetes Deployments

  1. Replicas: The desired number of identical Pods that should be running at any given time.
  2. Rollout: The process of updating a Deployment to a new version, typically by changing the container image or other configuration.
  3. Revision: A specific version of a Deployment, identified by a unique revision number.
  4. Rollback: The process of reverting a Deployment to a previous revision, effectively undoing a failed rollout.

Anatomy of a Kubernetes Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: my-container
          image: my-image:v1
          ports:
            - containerPort: 80

This Deployment creates three replicas of a Pod with the my-app label, running the my-image:v1 container image.

Deployment Strategies

Kubernetes supports different deployment strategies, such as:

  • RollingUpdate: Gradually replaces old Pods with new ones.
  • Recreate: Terminates all existing Pods before creating new ones.

The choice of strategy depends on the application's requirements and the desired update behavior.

Troubleshooting Failed Deployments

Identifying Failed Deployments

You can identify a failed Kubernetes Deployment by running the following command:

kubectl get deployments

This will show the status of your Deployments, including any that have failed.

Common Causes of Deployment Failures

  1. Image Pull Errors: The container image specified in the Deployment cannot be pulled, often due to incorrect image name or tag, or lack of access to the image repository.
  2. Container Startup Failures: The container fails to start, due to issues with the application code, configuration, or dependencies.
  3. Resource Constraints: The Pod cannot be scheduled due to insufficient CPU, memory, or other resources on the cluster.
  4. Liveness/Readiness Probe Failures: The Pod's liveness or readiness probe fails, causing the Deployment to consider the Pod as unhealthy.

Troubleshooting Steps

  1. Check Deployment Status: Use kubectl describe deployment <deployment-name> to get detailed information about the Deployment, including any error messages or events.
  2. Inspect Pod Logs: Use kubectl logs <pod-name> to view the logs of the failed Pod, which can provide valuable clues about the root cause of the failure.
  3. Examine Events: Use kubectl get events to view the events related to the Deployment, which can help identify the underlying issues.
  4. Validate Configuration: Ensure that the Deployment manifest is correctly defined, with the appropriate container image, resource requests/limits, and probe configurations.

By following these troubleshooting steps, you can quickly identify and address the root cause of a failed Kubernetes Deployment.

Performing a Rollback

Understanding Rollbacks in Kubernetes

When a Kubernetes Deployment update fails, you can perform a rollback to revert the Deployment to a previous, working revision. This is a crucial feature that allows you to quickly recover from a failed deployment and minimize downtime.

Checking Deployment Revisions

You can view the revision history of a Deployment using the following command:

kubectl rollout history deployment <deployment-name>

This will show you the different revisions of the Deployment, along with the changes made in each revision.

Performing a Rollback

To roll back a Deployment to a previous revision, use the following command:

kubectl rollout undo deployment <deployment-name> --to-revision=<revision-number>

Replace <revision-number> with the specific revision you want to roll back to. This will update the Deployment to the specified revision, effectively undoing the failed deployment.

Monitoring the Rollback Process

You can monitor the progress of the rollback using the following command:

kubectl rollout status deployment <deployment-name>

This will show you the status of the Deployment as it rolls back to the previous revision.

Verifying the Rollback

After the rollback is complete, you can verify that the Deployment has been successfully rolled back by checking the Deployment status and the running Pods:

kubectl get deployments
kubectl get pods

Ensure that the Deployment is in a healthy state, and the Pods are running the expected container image.

By understanding the rollback process in Kubernetes, you can quickly recover from failed deployments and maintain the reliability of your applications.

Summary

Kubernetes provides robust mechanisms to manage and update your application deployments. By understanding the process of troubleshooting and rolling back failed deployments, you can quickly restore your cluster to a stable state and minimize the impact on your users. This tutorial has covered the key steps to identify and address deployment issues, as well as the techniques to undo changes and revert to a previous, working version of your application.

Other Kubernetes Tutorials you may like