Chuyển đến nội dung chính

Bài 3: Rolling Updates, Rollbacks & Deployment Strategies

Deployment strategies: RollingUpdate vs Recreate. Kubectl rollout commands, maxUnavailable/maxSurge. Revision history và rollback kỹ thuật cho CKAD.

Rolling Update và Rollback — maxUnavailable, maxSurge, ReplicaSet history

1. Deployment Strategies

StrategyCách hoạt độngDowntime?Khi dùng
RollingUpdateReplace pods dần dần, maintain availabilityKhôngDefault, production
RecreateKill tất cả pods cũ, tạo mớiCóDev/test, breaking changes
spec:
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1   # OR "25%"  — Max pods unavailable during update
      maxSurge: 1         # OR "25%"  — Max extra pods above desired count

      ┌─────────────────────────────────────────────────┐
      │ Desired: 4 pods                                  │
      │                                                  │
      │ maxUnavailable: 1 → min 3 pods must be running  │
      │ maxSurge: 1       → max 5 pods total at once    │
      │                                                  │
      │ Step 1: Create 1 new pod (5 total = desired+surge)│
      │ Step 2: Terminate 1 old pod (4 total)           │
      │ Step 3: Repeat until all replaced               │
      └─────────────────────────────────────────────────┘

Exam tip: maxUnavailable và maxSurge KHÔNG thể cùng lúc là 0. Nếu cần zero-downtime update: set maxUnavailable: 0 và maxSurge: 1 (hoặc cao hơn).

2. kubectl rollout Commands

# Xem trạng thái rollout
kubectl rollout status deployment/myapp

# Xem revision history
kubectl rollout history deployment/myapp
kubectl rollout history deployment/myapp --revision=2

# Rollback về version trước
kubectl rollout undo deployment/myapp

# Rollback về revision cụ thể
kubectl rollout undo deployment/myapp --to-revision=2

# Tạm dừng rollout
kubectl rollout pause deployment/myapp

# Resume rollout
kubectl rollout resume deployment/myapp
CommandTác dụng
rollout statusWait/show current rollout progress
rollout historyList revision history
rollout undoRollback to previous (or specific) revision
rollout pause/resumePause để canary test, rồi resume
rollout restartForce restart tất cả pods (rolling)

Exam tip: Để lưu CHANGE-CAUSE trong revision history, thêm annotation: kubectl annotate deployment/myapp kubernetes.io/change-cause="Updated image to v2" TRƯỚC khi update. Hoặc dùng --record flag (deprecated nhưng vẫn hoạt động trong exam).

3. Trigger & Monitor Update

# Update image (trigger rolling update)
kubectl set image deployment/myapp container-name=nginx:1.25

# Xem ReplicaSet history (mỗi update tạo mới 1 RS)
kubectl get rs
# NAME              DESIRED   CURRENT   READY
# myapp-7d9b8c      4         4         4     ← current
# myapp-6f5a2b      0         0         0     ← old (kept for rollback)

# Scale deployment
kubectl scale deployment/myapp --replicas=6

# Edit deployment trực tiếp
kubectl edit deployment/myapp

4. Revision History Limit

spec:
  revisionHistoryLimit: 10  # Default: 10 old RS kept for rollback
                             # Set to 0 to disable rollback capability

5. Cheat Sheet

Tình huống examCommand
Update imagekubectl set image deploy/app c=image:v2
Check rolloutkubectl rollout status deploy/app
Rollback nhanhkubectl rollout undo deploy/app
Rollback về rev 3kubectl rollout undo deploy/app --to-revision=3
Zero-downtime configmaxUnavailable: 0, maxSurge: 1

6. Practice Questions

Q1: A Deployment with 10 replicas is configured with maxUnavailable: 2 and maxSurge: 3. During a rolling update, what is the maximum number of pods that can exist at any given time?

  • A) 10
  • B) 12
  • C) 13 ✓
  • D) 15

Explanation: maxSurge=3 means up to 3 extra pods above the desired count (10) can exist simultaneously. So maximum = 10 + 3 = 13 pods. Meanwhile, maxUnavailable=2 means at least 8 pods must be available.

Q2: You updated a Deployment and then realized the new version has a bug. Which command quickly reverts to the previous working version?

  • A) kubectl delete deployment myapp && kubectl apply -f old.yaml
  • B) kubectl rollout undo deployment/myapp ✓
  • C) kubectl rollout history deployment/myapp
  • D) kubectl set image deployment/myapp container=old-image

Explanation: rollout undo is the fastest way to revert to the previous revision. It creates a new rolling update back to the previous ReplicaSet. Option D also works but requires knowing the exact old image name.

Q3: A Deployment uses Recreate strategy. What is the expected behavior during an update?

  • A) Pods are replaced one at a time with no downtime
  • B) All existing pods are terminated before new pods are created, causing downtime ✓
  • C) Half the pods are updated at once while the other half serve traffic
  • D) New pods are created first, then old pods are terminated

Explanation: Recreate strategy terminates ALL existing pods at once (scale to 0), then creates the new pods. This causes downtime but ensures no two versions run simultaneously — suitable when old and new versions cannot coexist.