Zero-downtime blue-green deploys on Kubernetes
Safe deploys with no dropped requests.
run blue and green deployments, switch a Service or ingress selector to the new color after readiness probes pass, drain old pods gracefully, and handle backward-compatible DB migrations.
WHAT THIS TESTS Whether you can translate the blue-green concept into concrete Kubernetes primitives and, crucially, handle the database and connection-draining concerns that actually break naive deploys.
A GOOD ANSWER COVERS Run two Deployments, blue (current) and green (new), each with its own pods but selectable by labels. A Service or ingress routes traffic to whichever color is live via its label selector. To deploy, roll out green, and define a readiness probe hitting a health endpoint that returns ready only once the app, its DB pool, and dependencies are up; Kubernetes withholds traffic from not-ready pods. Once green is healthy, flip the Service selector (or shift ingress weight) to green. Then retire blue: a preStop hook plus terminationGracePeriodSeconds lets in-flight requests complete, and Uvicorn handles SIGTERM by stopping new connections while finishing current ones, so no request is dropped. The often-missed component is the database: migrations must be backward and forward compatible (expand-then-contract) because both colors may run at once, so you add columns nullable first, deploy code, then remove old columns in a later release. Configure liveness probes too, plus resource requests and a rollback path by flipping the selector back.
COMMON WRONG ANSWERS Switching the Service before readiness probes confirm green is healthy. Deleting blue pods immediately, dropping in-flight requests. Running a destructive migration that the still-live old version cannot tolerate. Forgetting graceful shutdown handling.
LIKELY FOLLOW-UPS How does this compare to a rolling update or canary? What is the expand-contract migration pattern? How do you roll back instantly?
ONE CONCRETE EXAMPLE Green pods come up, readiness probe on /health passes after the DB pool warms, you patch the Service selector from color=blue to color=green, traffic shifts instantly, and blue pods receive SIGTERM, finish their open requests within a 30-second grace period, then terminate. The release added a nullable column only, so blue ran fine right up to cutover.
Read the original → fastapi.tiangolo.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.