Blue/green deployments in Kubernetes: the one-field switch
Run the new version beside the old one and move every user across by changing one field: the version in a Service's selector. Why it beats a rolling update for risky releases, and what it costs.
ON THIS PAGE
A blue/green deployment runs the new version of an app beside the old one and moves every user across in one step. In plain Kubernetes that step is a single field: the version label in a Service's selector. Change it from blue to green and the next connection lands on the new Pods; change it back and you have rolled back. Nothing is rebuilt, nothing restarts.
Based on the article from Gazeek, our sister site for hands-on CKA and CKAD labs: Blue/green deployments in Kubernetes, step by step. The full YAML, every command and a real cluster to try it on are there.
Why not just let a rolling update do it?#
A Deployment's rolling update is the default for good reason: it needs no spare capacity and no ceremony. But while it runs, some users get the old version and some the new, and if the new one is broken the way back is another rolling update. Blue/green buys two things with double capacity during a release: you test green before anyone uses it, and both the switch and the rollback are instant.
The mechanism, in three moving parts#
- Two Deployments,
shop-blueandshop-green. Their Pods shareapp: shopand differ only inversion: blueorversion: green. - One Service,
shop, whose selector names both labels:app: shop, version: blue. Users only ever talk to this. - One patch to release: set the selector's
versiontogreenwithkubectl patch service shop. The same patch withblueis your rollback.
Test green before the switch with kubectl port-forward or a second Service that selects only green. Users never see it until you decide they should.
The switch, in two fragments#
The Service selects on version as well as app, so it reaches only one of the two Deployments:
spec:
selector:
app: shop
version: blueReleasing is changing that one value. Rolling back is changing it again:
kubectl patch service shop -p '{"spec":{"selector":{"app":"shop","version":"green"}}}'What it costs you#
- Double capacity while both versions run in full. On a small cluster, check there is room first.
- Long-lived connections (WebSockets, keep-alive) stay on blue until they reconnect.
- The database has to work with both versions at once: add columns before you use them, remove them after.
- The selector is the release. Put
versioninto the Deployment's selector from day one; a Deployment's selector cannot change once it exists.
Where else the switch can live#
The Service selector is the version every Kubernetes exam asks for, and the one that works everywhere. The same idea moves outwards as your platform grows:
- A Service selector: this article.
- An Ingress: switch the backend at the edge of the cluster.
- The Gateway API: weights built into the standard.
- Istio: the switch inside every client, with mirroring.
The diagram#
The picture at the top was drawn in archhy, an architecture diagram tool. Open it in archhy and press Make a copy to redraw it for your own app.
Go deeper#
Gazeek's step-by-step version has the YAML for both Deployments and the Service, the exact patch, and how blue/green differs from a canary. Its CKAD course practises both on a real cluster.
