Kubernetes Practice (English) – Manual Blue/Green Deployment

Tram Ho

Introduce

When our application is running in production, deploying a new version of the application always requires no downtime, there are several ways to do this and one of the ways to help us avoid downtime is Blue/Green Deployment.

In this post we will learn how to do Blue/Green Deployment manually, in the next post I will show you how to do it automatically using Argo Rollouts. This article I refer to from CNCF Presentation Template K8s Deployment.

Steps to follow

In this article, I use Minikube to run Kubernetes Cluster. We will proceed in the following steps:

  1. Call the instance that is running and receiving traffic from our users as version 1
  2. We deploy a new version of the application as version 2
  3. Wait for version 2 to run
  4. Switch traffic from version 1 to version 2
  5. Turn off version 1

In Practice

Let’s practice, creating a file named app-v1.yaml to deploy our application version 1, configure it as follows:

In the file above, we declare a Deployment with two labels “app: my-app” and “version: v1.0.0”. I will explain why we need 2 labels.

Next, we create a Service for app-v1 with the file name is service.yaml.

The main point here is the selector, as we can see it matches both the app and version labels, and the version label is what we really need to care about. It is the key for us to switch traffic between two versions of the application.

Create Deployment and Service.

Check all pods are running, if you use Minikube then run as follows:

Otherwise, you can use port-forward.

After checking that our version 1 application is running, please turn off port-forward because we will continue to use it later.

Next, we will deploy the new version of the application (version 2). Create a file namedapp-v2.yaml with config as follows:

You notice in the section labels, we will declare the label version as v2.0.0. Next, we deploy the application version 2.

Wait for all Pods to be running state before we continue.

Check that the application version 2 was able to receive traffic.

Open another terminal and access the application version 2 and make sure it can receive traffic from the user.

If the version 2 application has run successfully and is able to receive traffic, then the next most important part is how to switch the traffic from version 1 to version 2. To do that, we simply update the label of the Service above to version 2. We can edit the YAML file or do it quickly with the patch command.

At this time, the traffic of the application is switched from version 1 to version 2.

Let’s test.

Ok, if you see the result is Version: v2.0.0, then we have successfully implemented Blue/Green Deployment. If something happens and you want to go back to version 1, we simply update the label version again.

Finally, we turn off the application version 1.

Done. Please like the DevOps VN page to receive notifications of the earliest posts.

Conclusion

So we have learned how to implement Blue/Green Deployment, as you can see, it’s not very complicated. But this is just a way to practice playing to know, so in the next post, we will learn how to implement Blue/Green Deployment for a real project with Argo Rollouts.

Share the news now

Source : Viblo