Rollback
Rollback
Restores the previous version when a deployment fails. An essential CI/CD feature.
Rollback
Restores the previous version when a deployment fails. An essential CI/CD feature.
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.
# 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
#!/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 자동 롤백 완료\"}"