Implement Blue-Green Deployment
What you'll build
Two Kubernetes Deployments (blue and green) with a Service that can be switched between them for zero-downtime deployment and instant rollback.
Goals
- Understand the blue-green deployment strategy
- Deploy two versions of an application (blue and green)
- Switch traffic between versions by updating a Service selector
- Perform an instant rollback
Prerequisites
- A Kubernetes cluster with kubectl
- Basic understanding of Deployments and Services
- Two container image versions (e.g., app:v1 and app:v2)
Steps
- 1
Deploy the blue version
Create a Deployment for the blue (current) version with a label
version: blue. This is the version currently serving traffic.CodeapiVersion: apps/v1 kind: Deployment metadata: name: app-blue spec: replicas: 3 selector: matchLabels: app: my-app version: blue template: metadata: labels: app: my-app version: blue spec: containers: - name: app image: myapp:v1Expected result
The blue Deployment is running with 3 Pods.
- 2
Create the Service pointing to blue
Create a Service that selects the blue version. This Service routes traffic to the blue Pods.
CodeapiVersion: v1 kind: Service metadata: name: my-app-svc spec: selector: app: my-app version: blue ports: - port: 80 targetPort: 8080Expected result
The Service routes all traffic to the blue version.
- 3
Deploy the green version
Create a Deployment for the green (new) version with label
version: green. This does NOT receive traffic yet.CodeapiVersion: apps/v1 kind: Deployment metadata: name: app-green spec: replicas: 3 selector: matchLabels: app: my-app version: green template: metadata: labels: app: my-app version: green spec: containers: - name: app image: myapp:v2Expected result
The green Deployment is running but not receiving traffic.
- 4
Switch traffic to green (and rollback)
Update the Service selector to
version: greento switch all traffic to the new version. If something goes wrong, switch back to blue instantly.Code# Switch to green kubectl patch service my-app-svc -p '{"spec":{"selector":{"version":"green"}}}' # Rollback to blue instantly if needed kubectl patch service my-app-svc -p '{"spec":{"selector":{"version":"blue"}}}'Expected result
Traffic switches to green. If there are issues, the rollback command instantly switches back to blue.
- 5
Add a health check endpoint
Add a /health endpoint returning 200 OK for orchestration systems (Kubernetes, load balancers) to probe liveness. Health checks are foundational for production deployment.
Code# app.py (Flask example) from flask import Flask, jsonify app = Flask(__name__) @app.route("/health") def health(): return jsonify({"status": "ok"}), 200 @app.route("/ready") def ready(): # Check DB connection etc. return jsonify({"ready": True}), 200Expected result
curl localhost:5000/health returns {"status":"ok"} with a 200 status code.
Try it yourself
What you learned
You implemented a blue-green deployment strategy that allows zero-downtime deployments and instant rollback. This is a powerful technique for safe production releases.
Related Resources
Related Glossary
- Blue-Green Deployment
Blue-green deployment strategy
- Rolling Update
Rolling update strategy
Related Glossary
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
- Blue-Green Deployment
Blue-green deployment strategy
- Rolling Update
Rolling update strategy
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.