Introduction
In today’s fast-paced DevOps world, zero-downtime deployments are a necessity. Traditional deployments often result in downtime, failed updates, or rollback complexities. Enter Blue-Green Deployments, a strategy that eliminates these risks by maintaining two production environments—one active (Blue) and one staged (Green).
In this post, we’ll walk through how to implement a Blue-Green Deployment in Kubernetes while ensuring:
Zero-downtime application updates
Instant rollback in case of failure
Seamless traffic switching between versions
Since you don’t have access to Docker Hub or GitHub for image pulls, we’ll build and use a local image with Minikube instead.
How Blue-Green Deployment Works
Blue (Active Version): The currently running stable application version.
Green (New Version): The newly deployed application version, tested before switching live traffic.
Traffic Switching: Once the Green version is verified, traffic is routed to it, making it the new Blue version.
Rollback Option: If issues arise, traffic can be switched back to the previous version instantly.
Setting Up Blue-Green Deployment in Kubernetes
Step 1: Build the Application Image Locally
Since you don’t have access to external image registries, we’ll use Minikube’s local image repository for this setup.
First, create a simple Flask-based web app to simulate different versions:
Create app.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Hello from the Blue Version!"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
Create Dockerfile
FROM python:3.8-slim
WORKDIR /app
COPY app.py .
RUN pip install flask
CMD ["python", "app.py"]
Build and Load the Image into Minikube
eval $(minikube docker-env
docker build -t myapp:blue .
Step 2: Deploy the Blue Version
Create blue-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: blue-app
labels:
app: myapp
version: blue
spec:
replicas: 2
selector:
matchLabels:
app: myapp
version: blue
template:
metadata:
labels:
app: myapp
version: blue
spec:
containers:
- name: app
image: myapp:blue
ports:
- containerPort: 5000
Apply the Deployment
kubectl apply -f blue-deployment.yaml
Create service.yaml to expose Blue version
apiVersion: v1
kind: Service
metadata:
name: myapp-service
spec:
selector:
app: myapp
ports:
- protocol: TCP
port: 80
targetPort: 5000
type: LoadBalancer
Apply the Service
kubectl apply -f service.yaml
minikube service myapp-service
Now, you can access http:// and see:
“Hello from the Blue Version!”
Step 3: Deploy the Green Version
Modify app.py to change the response message:
Modify app.py
@app.route("/")
def home():
return "Hello from the Green Version!"
Rebuild and Load the Green Version
docker build -t myapp:green .
Create green-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: green-app
labels:
app: myapp
version: green
spec:
replicas: 2
selector:
matchLabels:
app: myapp
version: green
template:
metadata:
labels:
app: myapp
version: green
spec:
containers:
- name: app
image: myapp:green
ports:
- containerPort: 5000
Apply Green Deployment
kubectl apply -f green-deployment.yaml
At this point, both Blue (old version) and Green (new version) exist, but traffic still flows to Blue.
Step 4: Switch Traffic to the Green Version
To shift traffic, modify the Service Selector to point to the Green version instead of Blue.
Update service.yaml
spec:
selector:
app: myapp
version: green
Apply the Service Update
kubectl apply -f service.yaml
Now, accessing http:// will show:
“Hello from the Green Version!”
Traffic has successfully switched from Blue to Green!
Step 5: Rollback to Blue
If issues arise in the Green version, simply change the Service selector back:
Modify service.yaml again
spec:
selector:
app: myapp
version: blue
Apply the Rollback
kubectl apply -f service.yaml
Now, traffic is back to the Blue Version instantly—without redeploying anything!
Conclusion
Blue-Green Deployments in Kubernetes offer a seamless way to update applications with zero downtime. The ability to instantly switch traffic between versions ensures quick rollbacks, reducing deployment risks.
No downtime during deployments
Easy rollback if issues arise
No impact on end-users
How do you handle deployments in Kubernetes? Let me know in the comments!![]()