Sub topic 5
Blue-green deployments: two environments and one switch
This chapter assumes you know Kubernetes Deployments and Services, what a load balancer does, and why a failed release hurts. Earlier chapters covered rolling updates, readiness probes, and traffic routing. Here you will see how blue-green uses two full environments and one traffic switch.
The idea
A blue-green deployment runs two complete copies of your service. The live copy is called blue. The new version is deployed to a second copy, called green, which receives no user traffic yet. You test green, then move all traffic from blue to green in one step.
Blue stays running after the cutover. If green misbehaves, you point traffic back at blue. That switch back is the rollback, and it takes seconds because blue is still deployed and warm.
This is the main difference from a rolling update. A rolling update mixes old and new pods behind the same endpoint for several minutes. Blue-green keeps them apart, and users see one version or the other, never both at once.
Step 1: run blue behind a Service
In Kubernetes, the switch is usually a Service selector. The Service below includes a version label, so it sends traffic only to pods labeled version: blue.
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
version: blue
ports:
- port: 80
targetPort: 8080Each environment is its own Deployment. The blue Deployment labels its pods version: blue. You then create a second Deployment with version: green in its pod template and the new image. Both Deployments exist at the same time, and only blue receives traffic.
Step 2: verify green before the switch
Green is running but idle, so you can check it without risk. Useful checks:
- Wait until all green pods pass their readiness probes.
- Send smoke-test requests directly to green. You can do this through a separate internal Service that selects
version: green, or withkubectl port-forward. - Run a short set of critical-path tests, such as login and checkout.
Passing here shows that green starts, connects to its dependencies, and answers requests. It does not show how green behaves under real traffic. That gap is why teams sometimes choose a canary instead.
Step 3: switch the selector
The cutover changes the Service selector from blue to green.
kubectl patch service web -p '{"spec":{"selector":{"app":"web","version":"green"}}}'The Service's endpoint list updates, and new connections go to green pods. Connections already open to blue pods may continue until they close, so let blue drain rather than deleting it at once.
To roll back, run the same command with blue. Nothing needs to be rebuilt or rescheduled.
Costs and failure modes
Blue-green has real costs, and they decide whether it fits.
| Concern | What happens |
|---|---|
| Capacity | You run roughly double the pods during the release, so you pay for both environments. |
| All-at-once exposure | Every user moves to green together. A bug that tests missed hits 100% of traffic until you roll back. |
| Shared database | Both environments usually use the same database, so a schema change must work for both versions. |
| Warm-up | Green's caches and connection pools start cold, so the first minutes after cutover can be slower. |
| In-flight work | Long requests, sessions, and background jobs on blue need a plan during the switch. |
The shared database is the most common reason a "one-second rollback" fails. If green ran a migration that blue cannot read, switching back does not restore service. The fix is to make schema changes backward compatible, so that both versions work with the same schema. A later chapter covers this.
After the cutover
Watch error rate and latency for a defined period, such as 15 to 30 minutes. Keep blue running during that time. Once you trust green, scale blue down or keep it as the target for the next release. Many teams alternate colors: the idle environment becomes the staging area for the next version.
Exercises
Exercise
You switched the Service selector to green, and error rates rose. Blue pods are still running. What do you do, and what could stop it from working?
Show answer
Patch the Service selector back to version: blue. It could fail if green applied a database migration that blue's code cannot handle, or if blue was already scaled down or deleted. Backward-compatible schema changes and keeping blue running through the observation period prevent both problems.
Exercise
Why does a passing smoke test on green not guarantee a safe cutover?
Show answer
Smoke tests send little, synthetic traffic. They do not reveal problems that appear only under real load or with real data. At cutover, all users reach green at once, so a missed bug affects everyone.