In this lesson, you will learn how to manage two identical production environments to switch traffic instantly between versions with zero downtime.
The Blue-Green Concept
The Blue-Green deployment strategy minimizes risk by maintaining two parallel, identical environments. We call the current production environment Blue and the new, pending version Green.
Because both environments exist simultaneously, you can fully test your new code in a live-production environment without exposing it to your actual users. Once the Green environment is verified, the "switch" occurs at the networking layer, redirecting all incoming traffic from Blue to Green.
Implementing the Switch in Kubernetes
In Kubernetes, you orchestrate this by using two separate Deployments—one for each version—and a single Service to manage the traffic.
- Deploy Green: Create a new deployment for your updated application. At this stage, your
Serviceselector is still pointing to theversion: bluelabels on your existing pods. - Verify: Use
kubectl port-forwardto access the Green pods directly. Since the Service hasn't been updated, your external users remain on the Blue environment. - The Cutover: Update the
selectorin yourServicedefinition to match the labels of the Green pods. Kubernetes performs an atomic update to the Service, instantly shifting all traffic:
apiVersion: v1
kind: Service
metadata:
name: my-app-service
spec:
selector:
app: my-app
version: green # Updated from 'blue' to 'green' to flip traffic
ports:
- protocol: TCP
port: 80
targetPort: 8080
Managing the Transition
The beauty of this strategy is that if a critical bug is discovered after the switch, you can roll back instantly by updating the Service selector back to version: blue. Once you are confident in the Green deployment, you can safely decommission the Blue pods to free up cluster resources. Since both environments are identical in capacity, you ensure that the Green environment can handle the full production load the moment it goes live.
Key Takeaway: Blue-Green deployment achieves zero downtime by keeping a fully validated "Green" environment ready to receive traffic and performing an instantaneous switch via the Kubernetes Service selector.