Blue Green Deployments using Kubernetes

jaffar shaik
4 min readMar 1, 2023

Source code can be viewed at :

Jaffarterraform786/bluegreendeploy (github.com)

Blue Deployment

Lets say in our cluster we have deployment called bluedeploy. Inside the deployment object we have replica set inside the replica set we have pods.so this deployment which is currently running is considered as blue deployment or production deployment.

Lets say this Blue Deployment running Docker image with tag blue.

Green Deployment

Lets say if i need to upgrade the application . or if i need to deploy a new version of application in this case we will start another similar deployment,
lets say Greendeploy .

In this case we will start another parallel deployment and run it under some non production environment just say test environment.we consider it as a Green deployment.

Lets say this is running another Docker image with tag Green.

  1. The old deployment — blue deployment or production deployment which is in production is attached with BlueService .
  2. The new deployment-test deployment or non production deployment which is in non prod is attached with Greenservice.

3. we will start these two deployments in parallel.

4. once the new deployment- Greendeploy is doing well we will flip the service of old deployment=blueDeploy to new deployment=Greendeploy.
then we will get rid of new service and old deployment.

step1:

create blue deployment which is a active deployment

this deployment has match Labels: app: blue

step2:

lets create a parallel green deployment which is a new deployment

this deployment has match labels matchLabels app: green

step3:

lets create blue service:

we can now access our active application using above external url:

lets describe service1

Home Page — canary_demo_kubernetes (a1c8bfb47d6ec44b7a2ea4296b912352–2003649121.us-east-1.elb.amazonaws.com)

step4:

lets create green service

step5:

lets describe green service2

lets access green service:

Home Page — canary_demo_kubernetes (a8af45b5fa7d74a15bf39078398e77c0–1679801909.us-east-1.elb.amazonaws.com)

now two parallel services are running.

Lets describe all pods

Figure Two services blue/green pods running.
Figure both App version's are running currently.

once the test environment is running fine.

inital app label:blue

we will flip the blue service object app label:blue to app label:green in blueservice.yml as shown below.

in this case it searches for pod with label label:green.

save and apply the change .

in this case we can refresh the blue deployment url and we can see traffic flowing through green app.

In order to save cost on resources lets delete the new service.

In order to save cost on resources lets delete the old deployment

Finally we have only one deployment :

Finally we have a new deployment with old service object serving the traffic.

Advantages:

The advantage of this “there is no downtime”
2. Shifting to the previous version of app is simple.
3. End user service is not intruppted.

--

--