How To Do Blue-Green Deployments in Kubernetes

How To Do Blue-Green Deployments in Kubernetes

Let’s dive into something cool: Blue-green deployments in DevOps. No jargon, just plain talk for tech enthusiasts like you!

What’s Blue-Green Deployment?

It’s a slick way to update your apps in DevOps. Keeps things running while you make changes. Fancy way of saying “zero downtime and low risks.”1

Here’s What We’ll Do:

We’ll show you how to do it in Kubernetes, step by step. No theory, just hands-on stuff.

But First, You Need:

  • Some Kubernetes knowledge. You should know about pods, deployments, services, and ingress. Don’t worry, we’ll keep it simple.
  • Familiarity with containers and a tool called ‘kubectl’ in Kubernetes. If you’ve dabbled in containers, you’re good to go.

Step 1: Get Your Kubernetes Cluster Ready

You need a Kubernetes playground. Grab one locally with Minikube or go for the big leagues with cloud-managed options like Amazon EKS, Google GKE, or Microsoft AKS.

Step 2: Get Your App Ready

Imagine you’ve got a web app in Node.js. For this journey, it needs to be tucked into a container and have versions. Don’t worry; Docker’s your friend for containerizing your app.

No more jargon, just action. Let’s dive into Blue-Green deployments in Kubernetes together!

Step 3: Create the Blue Deployment

First, create the blue deployment for your application, representing the stable version.2

  • Create a Kubernetes Deployment manifest file, such as “blue-deployment.yaml,” and define the deployment’s specifications.
  • Specify the image and tag for the stable version of your application.
  • Apply the deployment manifest using the following command:
kubectl apply -f blue-deployment.yaml

Step 4: Create the Blue Service and Ingress

Next, create a Kubernetes Service to expose the blue deployment and an Ingress to handle external traffic.

  • Create a Service manifest file, such as “blue-service.yaml,” and define the service specifications.
  • Apply the service manifest using the following command:
kubectl apply -f blue-service.yaml
  • Set up an Ingress controller if you haven’t already. Refer to the official documentation for your preferred Ingress controller, such as NGINX Ingress Controller or Traefik, for installation and configuration instructions.
  • Create an Ingress manifest file, such as “blue-ingress.yaml,” and define the ingress specifications, including the rules for routing traffic to the blue service.
  • Apply the ingress manifest using the following command:
kubectl apply -f blue-ingress.yaml

Step 5: Verify the Initial Setup

Before proceeding further, verify that the blue deployment, service, and ingress are functioning correctly.

  • Check the status of the blue deployment using the following command:
kubectl get deployments
  • Verify that the blue service is running:
kubectl get services
  • Test the application by accessing the service through the ingress URL or using port-forwarding if necessary.

Step 6: Create the Green Deployment

Now, create the green deployment for the new version of your application.

  • Create a Kubernetes Deployment manifest file, such as “green-deployment.yaml,” and define the deployment’s specifications.
  • Specify the image and tag for the new version of your application.
  • Apply the deployment manifest using the following command:
kubectl apply -f green-deployment.yaml

Step 7: Update the Ingress to Route Traffic

To switch traffic to the green deployment, update the ingress rules to route traffic accordingly.

  • Edit the existing ingress manifest file or create a new one, such as “blue-green-ingress.yaml.”
  • Modify the ingress rules to route traffic to the green service instead of the blue service.
  • Apply the updated ingress manifest using the following command:
kubectl apply -f blue-green-ingress.yaml

Step 8: Verify the Green Deployment

After updating the ingress, verify that the green deployment is running correctly.

  • Check the status of the green deployment using the following command:
kubectl get deployments
  • Test the application again to ensure the new version is functioning as expected.

Step 9: Rollback (if necessary)

If any issues arise with the green deployment, you can quickly roll back to the blue deployment.

  1. Edit the ingress manifest file or create a new one to point back to the blue service.
  2. Apply the updated ingress manifest to route traffic back to the blue deployment.

Step 10: Clean Up

Once you are confident in the new version and have successfully completed the blue-green deployment, clean up the blue deployment and associated resources to optimize resource utilization.

References

  1. Using blue-green deployment to reduce downtime |    Cloud Foundry Docs. (n.d.). https://docs.cloudfoundry.org/devguide/deploy-apps/blue-green.html ↩︎
  2. Tripathy, B. (2023, May 19). Blue-Green Deployment using Kubernetes – Bubu Tripathy – Medium. Medium. https://medium.com/@bubu.tripathy/blue-green-deployment-using-kubernetes-be994df956b4 ↩︎
Published