πŸ”§ DevOps

Blue-Green

Blue-Green Deployment

A zero-downtime deployment strategy that alternates between two environments. Immediate rollback is possible.

Detailed Explanation

Blue-Green deployment is a deployment strategy that runs two identical production environments (Blue and Green) to enable zero-downtime deployment and instant rollback. Popularized by Martin Fowler, this approach is a classic yet effective way to minimize deployment risk.

There is an environment that currently receives production traffic (e.g., Blue), and the new version is deployed to the standby environment (Green). After testing is complete, the load balancer or router is switched to send traffic to Green. If a problem occurs, switching back to Blue rolls back immediately.

In Kubernetes, it is implemented by changing the Service's selector or switching the Ingress backend. On AWS, it can be implemented with Elastic Beanstalk, ECS Blue/Green deployment, and Route 53 weighted routing.

In practice, care is needed when changing the database schema. A schema compatible with both Blue and Green must be maintained. The schema is changed gradually using the Expand-Contract pattern, or the approach is combined with feature flags.

Code Example

#!/bin/bash
# Kubernetes Blue-Green 배포 슀크립트

NAMESPACE="production"
SERVICE="my-app"
NEW_VERSION="v2.0.0"

# ν˜„μž¬ ν™œμ„± 배포 확인 (blue λ˜λŠ” green)
CURRENT=$(kubectl get svc ${SERVICE} -n ${NAMESPACE} \
    -o jsonpath='{.spec.selector.version}')

if [ "$CURRENT" == "blue" ]; then
    TARGET="green"
else
    TARGET="blue"
fi

echo "ν˜„μž¬: $CURRENT -> μ „ν™˜ λŒ€μƒ: $TARGET"

# μƒˆ 버전을 λŒ€κΈ° ν™˜κ²½μ— 배포
kubectl set image deployment/${SERVICE}-${TARGET} \
    app=myregistry/${SERVICE}:${NEW_VERSION} -n ${NAMESPACE}

# 배포 μ™„λ£Œ λŒ€κΈ°
kubectl rollout status deployment/${SERVICE}-${TARGET} \
    -n ${NAMESPACE} --timeout=300s

# ν—¬μŠ€μ²΄ν¬ 톡과 확인
echo "ν—¬μŠ€μ²΄ν¬ μˆ˜ν–‰ 쀑..."
sleep 10

# νŠΈλž˜ν”½ μ „ν™˜ (Service selector λ³€κ²½)
kubectl patch svc ${SERVICE} -n ${NAMESPACE} -p \
    "{\"spec\":{\"selector\":{\"version\":\"${TARGET}\"}}}"

echo "νŠΈλž˜ν”½μ΄ ${TARGET}으둜 μ „ν™˜λ˜μ—ˆμŠ΅λ‹ˆλ‹€."
echo "λ‘€λ°±: kubectl patch svc ${SERVICE} -n ${NAMESPACE} \
    -p '{\"spec\":{\"selector\":{\"version\":\"${CURRENT}\"}}}'"

How It's Said in Practice

Senior: "The last deployment rollback took 5 minutes, but if we switch to Blue-Green, we can roll back in 10 seconds. We only need to switch the traffic."

Junior: "Wouldn't the infrastructure cost double?"

Senior: "That's right, so some teams use Spot instances or scale up Green only during deployment."

Interviewer: "What is the difference between Blue-Green and Canary deployment?"

Candidate: "Blue-Green switches 100% of the traffic at once, while Canary gradually increases the traffic percentage. Blue-Green offers fast and simple rollback, but Canary can validate with a small group of users first, which spreads the risk further."

Take a mock interview on this topic β†’ WIA SOOM AI Interview Β· Start for free

Reviewer: "There's a health check on the Green environment before the traffic switch. Instead of relying only on readinessProbe, how about also adding an actual API call test?"

Developer: "Good idea. In addition to the /health endpoint, I'll add a smoke test that calls a few key APIs."

Cautions

Learn More