Kuberneteskubernetesblue-greendeploymentzero-downtimeproduction-ready

Blue-Green Deployment

Kubernetes blue-green deployment strategy with service switching, automated health validation, and instant rollback capability.


title: "Blue-Green Deployment" description: "Kubernetes blue-green deployment strategy with service switching, automated health validation, and instant rollback capability." category: "Kubernetes" language: "yaml" tags: ["kubernetes", "blue-green", "deployment", "zero-downtime", "production-ready"] createdAt: "2025-03-15" updatedAt: "2025-04-01"

Overview

Blue-green deployment maintains two identical environments. Traffic is switched atomically by updating the Service selector, enabling instant rollback by reverting the selector. The inactive slot serves as a pre-production test environment.

Code

# Promote green to production (instant switch)
kubectl patch service app-production -n production \
  -p '{"spec":{"selector":{"slot":"green"}}}'
 
# Rollback to blue (instant)
kubectl patch service app-production -n production \
  -p '{"spec":{"selector":{"slot":"blue"}}}'

Notes

  • Service selector change is atomic, providing zero-downtime deployment
  • The preview service enables testing the new version in production infrastructure before switching
  • successThreshold: 3 requires three consecutive health checks before marking ready
  • Rollback is a single kubectl patch command with no pod restarts needed
  • After successful promotion, update the old slot with the next version for the next release
  • Doubles resource requirements (both slots running simultaneously)