Amazon Elastic Container Service
Điều phối container
Dịch vụ của AWS để chạy và quản lý nhiều container (task/service), tự khởi động lại khi lỗi và gắn với load balancer.
- Chi phí$$ Vừa phải
- Độ khó●●●●●
- Để không có tốn tiền?Không
- AWS quản lý
- Có trong đềSAA · DVA · SOA
Hình dungNhư điều phối viên kho vận xếp các thùng hàng (container) lên xe phù hợp.
ECS đứng ở đâu trong hệ thống?
Gọi tới ECS
Một ví dụ thật, từng bước
- Đội của bạn đóng gói API thành Docker image và đẩy lên ECR.
- Bạn viết task definition: dùng image nào, bao nhiêu CPU và RAM, biến môi trường gì, ghi log vào đâu.
- Bạn tạo ECS service yêu cầu luôn có 3 task chạy, gắn sau Application Load Balancer và chọn chạy trên Fargate.
- Một task bị lỗi thì ECS tự chạy task mới; khi deploy bản mới, ECS thay dần từng task để web không bị gián đoạn.
- Lượng truy cập tăng, service auto scaling tự tăng số task theo CPU.
Khi nào nên dùng
✓ Nên dùng khi
Chạy ứng dụng container mà không muốn học Kubernetes.
✗ Không nên dùng khi
Đội đã quen Kubernetes hoặc cần chạy đa cloud → EKS.
Ví dụ hay gặp
- Microservices dạng container phía sau ALB
- Worker xử lý nền đọc từ SQS
- Batch job chạy bằng container
- Đưa ứng dụng Docker lên AWS mà không cần học Kubernetes
ECS, EKS hay Fargate?
| Là gì | Trình điều phối container của AWS | Kubernetes chuẩn do AWS quản lý | Nơi chạy container không cần máy chủ |
|---|---|---|---|
| Độ khó học | Thấp, ít khái niệm | Cao, phải hiểu Kubernetes | Thấp, dùng cùng ECS hoặc EKS |
| Phí riêng | Không | Phí control plane theo giờ | Theo vCPU và RAM của task |
| Hợp nhất với | Đội chỉ dùng AWS, muốn đơn giản | Đội đã quen Kubernetes, cần chạy nhiều nơi | Không muốn quản lý cụm máy |
Tốn bao nhiêu tiền?
- Cách tính: ECS không tính phí. Bạn trả cho nơi task chạy: EC2 (theo giờ máy) hoặc Fargate (theo vCPU và GB bộ nhớ).
- Miễn phí: Không có phí ECS.
Task kéo image qua NAT Gateway (dùng VPC endpoint cho ECR và S3 để tránh); mỗi service một ALB riêng; log container không đặt thời hạn lưu.
Task role cấp quyền cho ứng dụng trong container (đọc S3, ghi DynamoDB); task execution role cấp quyền cho ECS kéo image từ ECR và ghi log. Đề nói “không muốn quản lý máy chủ” → chọn Fargate làm launch type.
Thử sức với câu hỏi thi thật
1An ecommerce website is deploying its web application as Amazon Elastic Container Service (Amazon ECS) container instances behind an Application Load Balancer (ALB). During periods of high activity, the website slows down and availability is reduced. A solutions architect uses Amazon CloudWatch alarms to receive notifications whenever there is an availability issue so they can scale out resources. Company management wants a solution that automatically responds to such events. Which solution meets these requirements?
- ASet up AWS Auto Scaling to scale out the ECS service when there are timeouts on the ALB. Set up AWS Auto Scaling to scale out the ECS cluster when the CPU or memory reservation is too high.
- BSet up AWS Auto Scaling to scale out the ECS service when the ALB CPU utilization is too high. Set up AWS Auto Scaling to scale out the ECS cluster when the CPU or memory reservation is too high.
- CSet up AWS Auto Scaling to scale out the ECS service when the service's CPU utilization is too high. Set up AWS Auto Scaling to scale out the ECS cluster when the CPU or memory reservation is too high.
- DSet up AWS Auto Scaling to scale out the ECS service when the ALB target group CPU utilization is too high. Set up AWS Auto Scaling to scale out the ECS cluster when the CPU or memory reservation is too high.
Xem đáp án
Đáp án đúng: C
Scale số task của ECS service theo CPU của chính service, và scale số máy trong cluster theo mức CPU/bộ nhớ đã đặt chỗ. ALB không có chỉ số CPU để dựa vào.
2A company is running a multi-tier web application on premises. The web application is containerized and runs on a number of Linux hosts connected to a PostgreSQL database that contains user records. The operational overhead of maintaining the infrastructure and capacity planning is limiting the company's growth. A solutions architect must improve the application's infrastructure. Which combination of actions should the solutions architect take to accomplish this? (Choose two.)
- AMigrate the PostgreSQL database to Amazon Aurora.
- BMigrate the web application to be hosted on Amazon EC2 instances.
- CSet up an Amazon CloudFront distribution for the web application content.
- DSet up Amazon ElastiCache between the web application and the PostgreSQL database.
- EMigrate the web application to be hosted on AWS Fargate with Amazon Elastic Container Service (Amazon ECS).
Xem đáp án
Đáp án đúng: A, E
Chuyển database sang Aurora (dịch vụ được quản lý) và chạy container trên Fargate với ECS: không còn máy chủ để vận hành hay phải tính trước công suất. Chuyển sang EC2 vẫn phải tự quản lý máy.
Câu hỏi thường gặp
ECS chạy trên Fargate hay EC2 thì tốt hơn?
Fargate không phải quản lý máy chủ, trả tiền theo CPU và bộ nhớ của từng task. Chạy trên EC2 phải tự quản lý cụm máy nhưng chủ động chọn loại máy (kể cả GPU) và thường rẻ hơn khi tải lớn, đều.
ECS có tính phí không?
ECS không tính phí riêng. Bạn trả cho tài nguyên chạy container: Fargate (vCPU và bộ nhớ theo giây) hoặc các instance EC2, cùng load balancer, log và lưu trữ đi kèm.
Đề thi AWS hay hỏi gì về ECS?
Phân biệt task definition, task và service; task role (quyền cho ứng dụng) khác execution role (quyền kéo image, ghi log); chọn Fargate hay EC2 launch type; và kết nối với ALB, ECR, CloudWatch Logs.