🔧 DevOps

Rollback

Rollback

Restores the previous version when a deployment fails. An essential CI/CD feature.

Detailed Explanation

Rollback is the operation of quickly reverting a deployed application to a previous stable version when a problem occurs. In modern CI/CD pipelines, the rollback capability is a core safety mechanism that ensures service continuity.

A rollback goes beyond simply reverting code; it must also take into account database migrations, environment configuration, and infrastructure state. Implementing an automated rollback policy can reduce MTTR (Mean Time To Recovery) to within a few minutes when an incident occurs. Kubernetes supports instant rollback by retaining the previous 3 revisions of the ReplicaSet by default.

Code Example

Kubernetes Rollback Configuration

# deployment.yaml - 롤백 가능한 배포 설정
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-server
  annotations:
    kubernetes.io/change-cause: "v2.1.0 - 결제 API 개선"
spec:
  replicas: 3
  revisionHistoryLimit: 5  # 롤백 가능한 버전 수
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
      maxSurge: 1
  selector:
    matchLabels:
      app: api-server
  template:
    metadata:
      labels:
        app: api-server
        version: "v2.1.0"
    spec:
      containers:
      - name: api
        image: myapp/api-server:v2.1.0
        ports:
        - containerPort: 8080
        readinessProbe:
          httpGet:
            path: /health
            port: 8080
          initialDelaySeconds: 5
          periodSeconds: 10
        livenessProbe:
          httpGet:
            path: /health
            port: 8080
          initialDelaySeconds: 15
          periodSeconds: 20

Automated Rollback Script

#!/bin/bash
# auto-rollback.sh - 헬스체크 기반 자동 롤백

DEPLOYMENT="api-server"
NAMESPACE="production"
MAX_RETRIES=5
RETRY_COUNT=0

echo "배포 상태 모니터링 시작..."

while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
  # 배포 상태 확인
  STATUS=$(kubectl rollout status deployment/$DEPLOYMENT -n $NAMESPACE --timeout=60s 2>&1)

  if echo "$STATUS" | grep -q "successfully rolled out"; then
    echo "배포 성공!"
    exit 0
  fi

  RETRY_COUNT=$((RETRY_COUNT + 1))
  echo "헬스체크 실패 ($RETRY_COUNT/$MAX_RETRIES)"
  sleep 10
done

echo "배포 실패 감지 - 롤백 시작"

# 이전 버전으로 롤백
kubectl rollout undo deployment/$DEPLOYMENT -n $NAMESPACE

# 롤백 완료 대기
kubectl rollout status deployment/$DEPLOYMENT -n $NAMESPACE

# 알림 전송
curl -X POST $SLACK_WEBHOOK \
  -H 'Content-type: application/json' \
  -d "{\"text\":\"$DEPLOYMENT 자동 롤백 완료\"}"

Practical Conversation

On-Call Engineer

"500 errors are spiking in production. It looks like the v2.1.0 we just deployed is the cause."

DevOps engineer

"I checked the deployment history. I'll roll back to v2.0.9 with kubectl rollout undo. It'll be done in 30 seconds."

On-Call Engineer

"After the rollback, the error rate is back to normal. But is the DB migration okay?"

DevOps engineer

"Luckily, this release has a backward compatible schema change, so there's no problem. That's thanks to our policy of only allowing migrations that can always be rolled back."

Dev team lead

"Good. After the root cause analysis, let's proceed with a hotfix, and next time let's lower the canary deployment ratio and validate first."

Cautions

  • DB migration compatibility: You must always verify that schema changes are backward compatible when rolling back. Dropping columns or changing types makes rollback difficult.
  • Consider stateful data: Verify that data created in the new version is compatible with the previous version. If the format has changed, data loss may occur.
  • revisionHistoryLimit setting: Setting it too low limits your rollback options. We recommend keeping at least 5.
  • External service dependencies: When rolling back, you must also consider the compatibility of external API versions and message formats.
  • Using Feature Flags: Instead of rolling back code, disabling only the feature with a Feature Flag is often safer.

Learn More