Blue‑Green Deployment with Host and Path‑Based Routing in Kubernetes
This guide explains how to implement a blue‑green deployment on Kubernetes with host‑ and path‑based routing, covering prerequisites, namespace creation, deployment manifests, service and ingress configuration, traffic switching, updates, verification, and rollback procedures.
Kubernetes is an open‑source container orchestration system that simplifies application deployment, scaling, and management. This article provides a step‑by‑step tutorial for performing a blue‑green deployment with host‑ and path‑based routing.
Prerequisites
Kubernetes cluster
kubectl command‑line tool
Docker image of the application to be deployed
Step 1: Create Namespace
kubectl create namespace blue-green-deploymentStep 2: Create Deployments
Create the blue deployment manifest (blue-deployment.yaml):
# blue-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: demoapp-blue
labels:
app: demoapp
env: blue
spec:
replicas: 3
selector:
matchLabels:
app: demoapp
env: blue
template:
metadata:
labels:
app: demoapp
env: blue
spec:
containers:
- name: demo
image: demoapp:v1.0
ports:
- containerPort: 80Apply the blue deployment:
kubectl apply -f blue-deployment.yaml -n blue-green-deploymentCreate a similar green deployment manifest (green-deployment.yaml) and apply it.
Step 3: Create Service
Create the service manifest (service.yaml):
# service.yaml
apiVersion: v1
kind: Service
metadata:
name: demoapp-service
spec:
selector:
app: demoapp
ports:
- name: http
port: 80
targetPort: 80
type: LoadBalancerApply the service:
kubectl apply -f service.yaml -n blue-green-deploymentGet the external IP address of the service:
kubectl get service demoapp-service -n blue-green-deploymentStep 4: Define Routing Rules
Update the service manifest to include session affinity and traffic policy, then apply it:
# service.yaml (updated)
apiVersion: v1
kind: Service
metadata:
name: demoapp-service
spec:
selector:
app: demoapp
ports:
- name: http
port: 80
targetPort: 80
type: LoadBalancer
sessionAffinity: ClientIP
externalTrafficPolicy: Local
loadBalancerSourceRanges:
- 0.0.0.0/0 kubectl apply -f service.yaml -n blue-green-deploymentStep 5: Host and Path‑Based Routing
Extend the service manifest with an Ingress resource that routes traffic based on host and path:
# service.yaml (with Ingress)
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: demoapp-ingress
spec:
rules:
- host: "demoapp.example.com"
http:
paths:
- path: /blue
backend:
service:
name: demoapp-blue
port:
number: 80
- path: /green
backend:
service:
name: demoapp-green
port:
number: 80 kubectl apply -f service.yaml -n blue-green-deploymentStep 6: Execute Blue‑Green Switch
With both blue and green deployments running, switch traffic from the blue deployment to the green deployment by updating the Ingress rules or service selectors.
Step 7: Update Green Deployment
Create a new green deployment manifest (green-deployment-v2.yaml) with the updated application image and apply it:
# green-deployment-v2.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: demoapp-green
labels:
app: demoapp
env: green
spec:
replicas: 3
selector:
matchLabels:
app: demoapp
env: green
template:
metadata:
labels:
app: demoapp
env: green
spec:
containers:
- name: myapp
image: myapp:v2.0
ports:
- containerPort: 80 kubectl apply -f green-deployment-v2.yaml -n blue-green-deploymentStep 8: Update Service to Route to Green
Modify the service selector to point to the green deployment and re‑apply:
# service.yaml (selector updated to env: green)
apiVersion: v1
kind: Service
metadata:
name: demoapp-service
spec:
selector:
app: demoapp
env: green
ports:
- name: http
port: 80
targetPort: 80
type: LoadBalancer kubectl apply -f service.yaml -n blue-green-deploymentStep 9: Verify Deployment
Access the application using the external IP address in a web browser to confirm that the new version is serving traffic.
Step 10: Rollback (If Needed)
If problems arise, roll back by changing the service selector back to the blue deployment and re‑applying the service manifest:
# service.yaml (selector set to env: blue)
apiVersion: v1
kind: Service
metadata:
name: demoapp-service
spec:
selector:
app: demoapp
env: blue
ports:
- name: http
port: 80
targetPort: 80
type: LoadBalancer kubectl apply -f service.yaml -n blue-green-deploymentThese steps provide a complete workflow for performing a blue‑green deployment on Kubernetes with host‑ and path‑based routing, enabling zero‑downtime releases and easy rollback.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.