Rollout and Versioning in Kubernetes and Blue-Green Deployment

Rollout and Versioning in Kubernetes and Blue-Green Deployment

When managing applications in Kubernetes, one of the critical aspects is handling updates and deployments efficiently. Kubernetes provides robust mechanisms for rollout and versioning, ensuring that updates can be managed with minimal downtime and maximum reliability. In this blog, we'll explore the concepts of rollout and versioning in Kubernetes, and introduce the blue-green deployment strategy as a method to achieve seamless updates.

🗼Rollout and Versioning

🎗️Viewing Rollout Status

To monitor the progress of a deployment rollout, you can use the following command:

kubectl rollout status deployment/<deployment-name>

This command provides real-time feedback on the status of the deployment, helping you ensure that your application is being updated correctly.

🎗️Viewing Deployment History

Kubernetes keeps track of the deployment history, allowing you to review past versions and changes. You can view this history with the command:

kubectl rollout history deployment/<deployment-name>

This command is particularly useful for auditing and understanding the changes that have been made over time.

🎗️Updating the Deployment Image

One of the most common updates in a deployment is changing the container image. For example, to update the image for the nginx container in the myapp-deployment deployment to version 1.9.1, you would use:

kubectl set image deployment/myapp-deployment nginx=nginx:1.9.1

This command triggers a new rollout with the updated image.

🎗️How Deployment Upgrades Work

When you update a deployment, Kubernetes creates a new ReplicaSet with the updated configuration. It then gradually scales up the new ReplicaSet while scaling down the old one, ensuring that the desired number of replicas is maintained throughout the process. This strategy helps in achieving a smooth transition with minimal downtime.

🎗️Undoing Deployment Changes

If something goes wrong with the new deployment, Kubernetes allows you to roll back to a previous version easily:

kubectl rollout undo deployment/<deployment-name>

This command reverts the deployment to the previous ReplicaSet configuration, providing a quick recovery mechanism in case of issues.

🗼Blue-Green Deployment

The blue-green deployment strategy is a technique that reduces downtime and risk by running two identical production environments, referred to as Blue and Green. At any given time, only one of these environments (let's say Blue) is live, serving all production traffic. The other environment (Green) is used to deploy and test the new version of the application.

Here’s how blue-green deployment works in Kubernetes:

  1. Set Up Identical Environments: Create two identical deployments, one for the Blue environment and one for the Green environment. Initially, the Blue environment serves all the traffic.

  2. Deploy to Green Environment: Deploy the new version of your application to the Green environment.

  3. Test the Green Environment: Once the Green environment is ready, perform thorough testing to ensure that the new version works as expected.

  4. Switch Traffic: If the new version passes all tests, switch the traffic from the Blue environment to the Green environment. This can be achieved by updating the service to point to the Green deployment.

  5. Monitor and Rollback: Monitor the Green environment closely after the switch. If any issues arise, you can quickly switch the traffic back to the Blue environment.

🎗️Example Commands for Blue-Green Deployment

To create a new deployment for the Green environment:

kubectl create deployment myapp-green --image=myapp:2.0.0

To update the service to point to the Green deployment:

kubectl patch service myapp-service -p '{"spec":{"selector":{"app":"myapp-green"}}}'

To roll back to the Blue environment in case of issues:

kubectl patch service myapp-service -p '{"spec":{"selector":{"app":"myapp-blue"}}}'

🗼Conclusion

Kubernetes provides powerful tools for managing rollout and versioning, ensuring that application updates can be handled with confidence. By leveraging these capabilities, along with deployment strategies like blue-green deployment, you can achieve smooth and reliable updates with minimal downtime. This ensures that your applications remain highly available and performant even during updates.

Did you find this article valuable?

Support Ashutosh Mahajan's blog by becoming a sponsor. Any amount is appreciated!