Compute
D2 · Resilient
D3 · Performance
~1 phút đọcASG advanced — lifecycle hooks, termination, warm pool
Tinh chỉnh ASG: lifecycle hooks chèn logic, termination policy, instance refresh, warm pool, predictive scaling.
asg
auto-scaling
lifecycle-hook
warm-pool
Sơ đồ tổng quan
Đang tải sơ đồ…
Lifecycle hooks
- Hook `EC2_INSTANCE_LAUNCHING`: instance ở state `Pending:Wait` cho tới khi complete hoặc timeout → chèn bootstrap, warm-up, register service discovery.
- Hook `EC2_INSTANCE_TERMINATING`: instance ở `Terminating:Wait` → drain connection, backup log, deregister DNS.
- Heartbeat timeout mặc định 1h, tối đa 48h.
- Trigger: EventBridge → Lambda / SSM Automation để hành động.
Termination policies
- Default: rải đều AZ, chọn oldest launch config/template, closest to billing hour.
- `OldestInstance`: thay dần instance cũ.
- `NewestInstance`: test xong rollback instance mới.
- `OldestLaunchTemplate` / `AllocationStrategy` cho mixed instances.
- Scale-in protection cho instance quan trọng (stateful).
Instance Refresh
Replace dần instance theo config mới (Launch Template version mới). Có min healthy %, có thể skip matching để tránh replace không cần thiết. Rollback được nếu fail.
Warm Pool
- Pre-initialized instance ở trạng thái Stopped/Running/Hibernated.
- Khi scale-out, kéo từ warm pool → nhanh hơn tạo mới (bootstrap lâu).
- Phù hợp app có AMI nặng, bootstrap > 5 phút.
Scaling policies nâng cao
- Target Tracking (khuyến nghị): đơn giản, AWS quản CloudWatch alarm.
- Step Scaling: điều chỉnh theo mức severity của alarm.
- Predictive Scaling: ML forecast load, scale TRƯỚC khi spike. Phù hợp pattern lặp (daily/weekly).
- Scheduled Action: cron fix (thứ 2 đầu tuần tăng capacity).
Tip
Combo thường dùng: Target Tracking (CPU 60%) + Predictive Scaling → vừa phản ứng, vừa chủ động. Scheduled cho event lớn đoán trước.