Introduction
Blue/green deployment is a method of Continuous Deployment which reduces downtime and risk by running two identical production environments. Only one of the environments is live at any given time – the Green is active while the Blue is idle. This technique enables straightforward rollback to the previous version if anything goes wrong with the new deployment.
In the case of Kubernetes (K8s), which offers robust orchestration of containerized applications, blue/green deployment can be particularly effective. Let’s go through the implementation of this strategy in a K8s cluster. We’ll assume you already have a Kubernetes cluster running and are familiar with deploying applications on it.
Prerequisites
- A running Kubernetes cluster
- kubectl, the Kubernetes command-line tool, installed and configured
- A containerized application to deploy
Step-by-Step Instructions
Step 1: Define Your Blue and Green Environments
Initially, your blue and green environments will be identical. Each environment is defined by its own set of resources in Kubernetes—services, deployments, and ingress rules that determine how traffic is routed.
apiVersion: v1
kind: Service
metadata:
name: myapp-blue
spec:
selector:
app: myapp
version: blue
ports:
- protocol: TCP
port: 80
targetPort: 9376
This is a simple service definition for the blue version of an application. Similarly, you’d create another service for the green version.
apiVersion: v1
kind: Service
metadata:
name: myapp-green
spec:
selector:
app: myapp
version: green
ports:
- protocol: TCP
port: 80
targetPort: 9376
Step 2: Deploy the Blue Version
Deploy the blue version of your application, which will serve as the initial live environment. It’s integral to test and ensure that the blue environment is stable before proceeding.
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-blue
spec:
replicas: 3
selector:
matchLabels:
app: myapp
version: blue
template:
metadata:
labels:app: myapp
version: blue
spec:containers:
- name: myapp
image: myapp:1.0.0
ports:
- containerPort: 8080
Step 3: Routing Traffic
You will route traffic to the blue environment initially. An Ingress controller can help you manage the external access to the services, typically HTTP requests. The ingress resource below sends all traffic to the blue service.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: myapp-ingress
spec:
rules:
- http:
paths:
- path: /myapp
pathType: Prefix
backend:
service:
name: myapp-blue
port:
number: 80
Updating this Ingress resource is how you ultimately switch traffic from blue to green.
Step 4: Deploy the Green Version
With the blue environment live and stable, you can deploy the green version. This environment should replicate the blue one but with the new application version.
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-green
spec:
replicas: 3
selector:
matchLabels:
app: myapp
version: green
template:
metadata:
labels:app: myapp
version: green
spec:containers:
- name: myapp
image: myapp:1.1.0
ports:
- containerPort: 8080
Both environments will now be running in parallel, but only the blue version is receiving traffic.
Step 5: Smoke Testing the Green Deployment
Before routing traffic to the green version, it’s crucial to perform smoke testing to check basic functionalities. Temporarily modify the ingress to a canary release to test the green deployment without impacting all users.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: myapp-ingress
spec:
rules:
- http:
paths:
- path: /myapp
pathType: Prefix
backend:
service:
name: myapp-green
port
number: 80
Step 6: Full Traffic Switch to Green Deployment
If the green deployment is stable based on smoke testing and additional tests, you can proceed to reroute all traffic to it by updating the Ingress resource.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: myapp-ingress
spec:
rules:
- http:
paths:
- path: /myapp
pathType: Prefix
backend:
service:
name: myapp-green
port:
number: 80
Now the green deployment becomes the live environment. If any issues arise, roll back to the blue deployment by reversing the Ingress change.
Conclusion
Congratulations! You have now successfully implemented a blue/green deployment strategy in Kubernetes. Remember, the strength of this approach is the ability to quickly switch between two isolated but identical environments, increasing confidence in your deployments and reducing the impact of potential failures on the end users.