Blue-Green Deployments on Kubernetes: A CKAD Exam Must-Know

Naman SharmaNaman Sharma
3 min read
Concept
Imagine you have an application that is currently running on a server. You want to deploy a new version of the application, but you don't want to take the old version down because you're not sure if the new version will work properly.
Long_Story_Short
With blue-green deployment, you create two identical servers. One server is running the old version of the application, and the other server is running the new version of the application {Demonstrated as blue and green bubbles in the picture below}. You then start sending traffic to the new server, and if everything works properly, you can take the old server down.

Hypothetical Scenario for non-technicals

Imagine you have two identical cars. You're driving the old car, and you want to try out the new car. You don't want to sell the old car until you're sure that the new car is better.

So, you park the old car in the garage, and you start driving the new car. If you like the new car better, you can sell the old car. If you face some issues with the new car, you can just go back to driving the old car.

This diagram will help you understand it better...

Try to relate the above diagram with the scenarios you just read and you're good to go ahead

💡
Now coming back to learning Kubernetes. We will now talk about how to implement this in actual practice on your Kubernetes cluster

Here's the plan...

  1. Firstly, we must create two Deployment objects, one for the old version of the application and one for the new version of the application.

  2. Specify the replicas field to 0 for the new Deployment object, considering it is not available initially.

  3. Create a Service object that exposes both Deployment objects.

    💡
    Service objects are based on labels of the deployment, note that in this case, both versions of blue and green deployments have the same label
  4. We might also wanna use an Nginx(optional) or a cloud-based load balancer to distribute traffic between the two Deployment objects

  5. At last, when we are fully confident with the new version of the application, set the replicas field to 1 for the new(GREEN) Deployment object and 0 for the old(BLUE) Deployment object.

  6. The load balancer will automatically start sending all the traffic to the new(GREEN) Deployment object and you will be able to phase out the older(BLUE) one.

LET'S code this in ACTION

apiVersion: apps/v1
kind: Deployment
metadata:
  name: Blue-old-version
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
      version: old
  template:
    metadata:
      labels:
        app: my-app
        version: old
    spec:
      containers:
      - name: my-app
        image: my-app:old

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: Green-new-version
spec:
  replicas: 0
  selector:
    matchLabels:
      app: my-app
      version: new
  template:
    metadata:
      labels:
        app: my-app
        version: new
    spec:
      containers:
      - name: my-app
        image: my-app:new

---
apiVersion: v1
kind: Service
metadata:
  name: my-app
spec:
  selector:
    app: my-app
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80

Apply this yaml Code/Manifest with {kubectl apply -f file.yaml} This manifest creates two Deployment objects, one for the old version of the application and one for the new version. The Service object exposes both Deployment objects on port 80. The load balancer will automatically start sending traffic to the new Deployment object once it is deployed.

Nextly, as per the plan...

#kubectl edit deployment Green-new-version

And, change the replicas field to 1.

#kubectl edit deployment Blue-old-version

Later, change the replicas field to 0.

That's all you needed to do,

Now before we end this, I would like to say something on a personal note, I hope I was able to break the concepts down for you. I am always open to suggestions and corrections, you can also message me personally on my LinkedIn in case you face any issues with the concepts and implementation in general.

0
Subscribe to my newsletter

Read articles from Naman Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Naman Sharma
Naman Sharma