Blue-Green Deployments on Kubernetes: A CKAD Exam Must-Know
Concept
Long_Story_Short
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
Here's the plan...
Firstly, we must create two Deployment objects, one for the old version of the application and one for the new version of the application.
Specify the replicas field to 0 for the new Deployment object, considering it is not available initially.
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 labelWe might also wanna use an Nginx(optional) or a cloud-based load balancer to distribute traffic between the two Deployment objects
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.
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.
Subscribe to my newsletter
Read articles from Naman Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by