Managing Rollouts and Rollbacks in Kubernetes

Tutorial 3 of 5

1. Introduction

In this tutorial, we'll learn how to manage rollouts and rollbacks in Kubernetes. Rollouts and rollbacks are crucial in application deployment as they provide you with the ability to update and revert applications in a Kubernetes cluster without causing downtime.

You will learn how to:

  • Deploy a new application version using Kubernetes rollouts
  • Monitor the status of a rollout
  • Rollback to an older version of the application if something goes wrong

Prerequisites:
- Basic knowledge of Kubernetes
- A running Kubernetes cluster
- kubectl installed and configured to interact with your cluster

2. Step-by-Step Guide

Understanding Rollouts

In Kubernetes, a Deployment resource is responsible for managing the lifecycle of stateless applications. When you update the Deployment spec, Kubernetes starts a rollout, which is the process of moving from one version of the application to another.

Understanding Rollbacks

Rollbacks are the process of reverting to a previous version of your application. Kubernetes keeps track of the changes made to the Deployment spec in Revisions. You can rollback to any Revision using a single command.

Monitoring Rollouts

To monitor the status of a rollout, you can use the kubectl rollout status command followed by the Deployment name.

3. Code Examples

Deploying a New Application Version

# Create a Deployment
kubectl create deployment my-app --image=my-app:1.0

# Update the Deployment to a new version
kubectl set image deployment/my-app my-app=my-app:1.1

In the above example, we first create a Deployment named my-app with the image version 1.0. Then, we trigger a rollout by updating the image to 1.1.

Monitoring a Rollout

# Check the status of the rollout
kubectl rollout status deployment/my-app

The kubectl rollout status command will show the progress of the rollout, and return when the rollout is complete.

Performing a Rollback

# Rollback to the previous version
kubectl rollout undo deployment/my-app

If something goes wrong with version 1.1, you can rollback to the previous version using the kubectl rollout undo command.

4. Summary

In this tutorial, we learned how to manage application rollouts and rollbacks in Kubernetes. We learned how to deploy a new version of an application, monitor the status of the rollout and revert to a previous version if necessary.

To continue learning, you can explore other Kubernetes resources such as Services, and how they interact with Deployments.

5. Practice Exercises

  1. Create a new Deployment with an image of your choice. Then, update the Deployment to a new image version. Monitor the status of the rollout.

  2. After the rollout is complete, perform a rollback to the previous version. Check the status of the rollback.

  3. Create two Deployments, each with a different image. Update both Deployments to a new image version simultaneously. Monitor the status of the rollouts, and rollback if necessary.

Remember to practice regularly, and don't be afraid to make mistakes. That's how you learn!