Ngày 5: EC2 và Auto Scaling Review
Mục tiêu học tập
- Ôn tập EC2 instance types và pricing models
- Hiểu sâu về Auto Scaling Groups
- Nắm vững các scaling strategies
1. EC2 Instance Types
Naming Convention
Instance Families
| Family | Optimized for | Use Cases |
|---|---|---|
| M (General) | Balanced | Web servers, app servers |
| C (Compute) | CPU | Batch processing, gaming |
| R (Memory) | RAM | In-memory databases, caching |
| X (Memory) | Extreme RAM | SAP HANA, large databases |
| I (Storage) | High I/O | NoSQL, data warehousing |
| D (Dense) | HDD storage | Distributed file systems |
| G/P (Accelerated) | GPU | ML, graphics rendering |
| T (Burstable) | Burst CPU | Variable workloads |
T Instance và CPU Credits
2. EC2 Pricing Models
So sánh các pricing models
| Model | Discount | Commitment | Best For |
|---|---|---|---|
| On-Demand | 0% | None | Short-term, unpredictable |
| Reserved | 30-72% | 1 or 3 years | Steady-state workloads |
| Savings Plans | 30-72% | 1 or 3 years | Flexible compute usage |
| Spot | 60-90% | None | Fault-tolerant, flexible |
| Dedicated Host | Varies | Per host | Licensing, compliance |
Reserved Instances Types
Standard RI:
- Highest discount (up to 72%)
- Có thể bán trên Marketplace
- Không thể đổi instance family
Convertible RI:
- Lower discount (up to 66%)
- Có thể đổi instance family, OS, tenancy
- Không thể bán trên Marketplace
Savings Plans
Compute Savings Plans:
- Áp dụng cho EC2, Lambda, Fargate
- Flexible across regions, instance families
- Up to 66% discount
EC2 Instance Savings Plans:
- Specific to instance family in a Region
- Up to 72% discount
- Less flexible
Spot Instances
Spot Request Types:
1. One-time Request
- Launched → Running → Terminated when interrupted
2. Persistent Request
- Automatically re-requests if interrupted
- Stays valid until canceled
Spot Fleet:
- Collection of Spot + On-Demand instances
- Automatically maintains target capacity
- Diversified allocation strategy
3. EC2 Placement Groups
Ba loại Placement Groups
4. Auto Scaling Groups (ASG)
Components
Launch Template vs Launch Configuration
| Feature | Launch Template | Launch Config |
|---|---|---|
| Versioning | Yes | No |
| Multiple instance types | Yes | No |
| Spot + On-Demand mix | Yes | No |
| T2/T3 Unlimited | Yes | No |
| Recommended | Yes | Legacy |
5. Scaling Policies
Target Tracking (Khuyến nghị)
Policy: Keep Average CPU at 50%
CPU = 70% → Scale OUT (add instances)
CPU = 30% → Scale IN (remove instances)
CPU = 50% → No action
Step Scaling
CPU Usage Action
0-40% Remove 2 instances
40-60% No change
60-80% Add 1 instance
80-100% Add 3 instances
Scheduled Scaling
{
"ScheduledActionName": "scale-out-morning",
"Recurrence": "0 9 * * MON-FRI",
"MinSize": 5,
"MaxSize": 20,
"DesiredCapacity": 10
}
Predictive Scaling
- Sử dụng ML để dự đoán traffic patterns
- Proactive scaling trước khi load tăng
- Kết hợp với dynamic scaling
6. Scaling Cooldowns
7. Health Checks
Types
| Type | Default | Checks |
|---|---|---|
| EC2 | Yes | Instance status |
| ELB | Optional | HTTP health check |
| Custom | Optional | External health system |
Grace Period
- Thời gian chờ sau khi instance launch
- Không terminate instance mới trong grace period
- Default: 300 seconds
8. Câu hỏi ôn tập
-
Sự khác biệt giữa Standard và Convertible Reserved Instances?
Xem đáp án
Standard RI: discount cao hơn (~72%), không đổi instance family/OS/tenancy trong thời hạn, có thể bán trên RI Marketplace. Convertible RI: discount thấp hơn (~54%), có thể exchange sang RI khác cùng hoặc cao hơn giá trị (đổi family, OS, tenancy, Region), không bán được. Chọn Standard khi workload ổn định rõ ràng; Convertible khi cần flexibility vì technology roadmap chưa chắc.
-
Khi nào nên dùng Spot Instances?
Xem đáp án
Spot phù hợp cho fault-tolerant, stateless workloads: batch processing, data analytics (Spark/Hadoop), ML training, CI/CD workers, image/video rendering, genomics. Key requirements: (1) App có thể checkpoint và resume, (2) Không cần guaranteed uptime, (3) Có thể handle 2-phút interruption warning. Không dùng cho: production databases, stateful web servers (không có fallback), real-time customer-facing APIs.
-
Ba loại Placement Groups là gì và use cases của chúng?
Xem đáp án
(1) Cluster: cùng rack trong 1 AZ, low-latency 10+ Gbps network — cho HPC, ML training, big data (Hadoop). (2) Spread: mỗi instance trên hardware riêng biệt, cross-AZ, max 7 instances/AZ — cho critical apps cần max HA (tránh correlated hardware failures). (3) Partition: groups of instances, mỗi partition cách nhau về hardware — cho distributed databases (Cassandra, HDFS, HBase) cần topology-aware replication.
-
Tại sao nên dùng Launch Template thay vì Launch Configuration?
Xem đáp án
Launch Template hỗ trợ: versioning (rollback về version cũ), multiple instance types (Mixed Instances Policy), Spot + On-Demand trong cùng ASG, T2/T3 unlimited, tất cả EC2 features mới. Launch Configuration là legacy (deprecated 2022) — không có versioning, chỉ 1 instance type, không tương thích nhiều features mới. AWS không cho phép tạo Launch Configuration mới.
-
Target Tracking Scaling policy hoạt động như thế nào?
Xem đáp án
Target Tracking maintain một metric tại target value bạn set — ASG tự động scale out/in để giữ metric gần target. Ví dụ: target CPU = 50% → khi CPU > 50%, ASG thêm instances; khi CPU < 50%, ASG giảm instances (sau cooldown). Đơn giản nhất trong 4 scaling policies — không cần define thresholds phức tạp. AWS recommend dùng CPU, ALB Request Count per Target, hoặc custom metric từ CloudWatch.
9. Bài tập thực hành
- Tạo Launch Template với User Data
- Tạo Auto Scaling Group với Target Tracking policy
- Test scaling bằng cách tạo CPU load
- Xem scaling activities trong console
Tài liệu tham khảo chính thức
- Amazon EC2 User Guide
- EC2 Instance Types
- Reserved Instances
- Spot Instances
- Savings Plans
- EC2 Auto Scaling User Guide
- Launch Templates
- Scaling Policies
Ngày tiếp theo: S3 và Storage Review