M03.03 - Containers and Orchestration on AWS
Cho đến bây giờ, chúng ta đã nói về EC2 và Lambda, và có một compute service khác mà chúng ta chưa nói đến. Hãy nói về containers!
Hãy tưởng tượng bạn là một developer đang cố gắng deploy một application hoạt động hoàn hảo trên máy tính của bạn nhưng thất bại ở mọi nơi khác. Điều đó thật frustating, phải không? Bạn thậm chí có thể đã nghe các developers cố gắng debug vấn đề này và nói với nhau, "It works on my machine."
Mục tiêu học tập
Sau khi hoàn thành bài học này, bạn sẽ có thể:
- ✅ Mô tả cách containers tạo một runtime environment nhất quán và portable trên các systems khác nhau
- ✅ Giải thích cách Amazon Elastic Container Registry (Amazon ECR) được sử dụng để lưu trữ, quản lý, và version container images
- ✅ Xác định cách Amazon ECS và Amazon EKS orchestrate containers để deploy, scale, và quản lý applications
- ✅ Mô tả cách AWS Fargate chạy containers mà không cần provision hoặc quản lý servers
Containers là gì?
Containers giải quyết vấn đề portability này bằng cách cung cấp một environment nhất quán có thể được replicate ở bất cứ đâu. Đó là những gì containers làm. Chúng package mọi thứ application của bạn cần để chạy—code, runtime, dependencies, configuration—thành một single, portable unit. Điều này tạo ra một environment nhất quán, isolate application của bạn khỏi underlying system và làm cho nó thuận tiện để deploy và scale application của bạn ở bất cứ đâu.
Lợi ích của Containers:
- ✅ Consistency: Môi trường nhất quán ở mọi nơi
- ✅ Portability: Chạy trên bất kỳ system nào
- ✅ Isolation: Ứng dụng được cách ly khỏi hệ thống
- ✅ Faster start times: Khởi động nhanh hơn VMs
- ✅ Improved resource efficiency: Sử dụng tài nguyên hiệu quả hơn
Key Takeaways: Solving Deployment Challenges
Containers cung cấp một cách đáng tin cậy để package code và dependencies của application thành một single, portable unit, làm cho chúng lý tưởng cho workflows yêu cầu high security, reliability, và scalability.
Containers và VMs
Một container package application của bạn với mọi thứ nó cần để chạy, vì vậy nó hoạt động giống nhau trên bất kỳ computer nào. Điều này giúp di chuyển, cập nhật, và quản lý. Containers nhanh hơn và nhẹ hơn virtual machines (VMs) vì chúng chia sẻ operating system của host computer. VMs sử dụng hypervisor để chạy full, separate operating systems, điều này làm cho chúng ít resource-efficient hơn và có startup times dài hơn.

Sự khác biệt chính:
- VMs: Mỗi VM có Guest OS riêng, chạy trên Hypervisor
- Containers: Chia sẻ Host OS, chỉ chứa application và dependencies
Deployment Consistency với Containers
Khi environment của developer khác với staging hoặc production, deployments có thể thất bại và trở nên khó debug. Containers giải quyết điều này bằng cách giữ environment của application nhất quán ở mọi nơi, làm cho deployments mượt mà hơn và hỗ trợ troubleshooting.

Container image được build một lần và có thể chạy nhất quán trên:
- Development environment
- QA environment
- Staging environment
- Production environment
Scaling Containers với Orchestration
Khi containerized applications scale, việc quản lý chúng trở nên phức tạp hơn. Một setup bắt đầu với một vài containers trên một single host có thể nhanh chóng phát triển thành hàng trăm hoặc hàng nghìn containers trên multiple hosts. Ở quy mô đó, manually xử lý container lifecycle, monitoring, và general operations trở nên không bền vững. Đây là nơi orchestration tools xuất hiện. Chúng tự động hóa deployment, scaling, và management để giữ mọi thứ chạy trơn tru.

Container Orchestration Services
Bây giờ bạn đã hiểu containers là gì, hãy nói về việc host chúng trên AWS. Bạn có thể quản lý containers trên của riêng mình, nơi bạn đặt containers trên top của một cluster EC2 instances, nhưng đó là rất nhiều công việc. Bạn sẽ phải xử lý monitoring health của containers, starting và stopping chúng khi cần, updating chúng, và managing networking cho chúng, và nhiều hơn nữa. Bạn có thể quản lý nó, tất nhiên, nhưng nó sẽ phức tạp và dễ mắc lỗi. Đó là nơi container orchestration services xuất hiện.
Container orchestration services quản lý lifecycle của containers, bao gồm starting, stopping, và running chúng trên một cluster. Các orchestration services này tự động scale containers out khi traffic tăng lên—và scale back in khi mọi thứ lắng xuống. Bằng cách này, application của bạn có thể xử lý spikes trong demand mà không cần đổ mồ hôi. Chúng cũng xử lý recovery từ failure, monitoring, và updates, tiết kiệm cho bạn rất nhiều thời gian và công sức.
AWS Container Services
AWS có một bộ công cụ để quản lý containers phù hợp với ba categories: orchestration, registry, và compute.
Amazon ECS (Elastic Container Service)
Amazon Elastic Container Service (Amazon ECS) là một scalable container orchestration service để chạy và quản lý containers trên AWS, như Docker containers. Docker là một software platform để build, test, và deploy applications nhanh chóng.
Amazon ECS lý tưởng nếu bạn muốn một cái gì đó streamlined và integrated, nhưng bạn vẫn có thể định nghĩa container images và resources của application, chẳng hạn như EC2 instance types và load balancers. ECS tự động quản lý containers và infrastructure của chúng dựa trên parameters mà bạn đặt.
Amazon ECS Launch Types
Amazon ECS với Amazon EC2:
- Lý tưởng cho small-to-medium businesses cần full control over infrastructure
- Phù hợp cho custom applications yêu cầu specific hardware hoặc networking configurations
- Flexibility của Amazon EC2 và simplicity của Amazon ECS
Amazon ECS với AWS Fargate:
- Hoàn hảo cho startups hoặc small teams xây dựng web applications với variable traffic
- Serverless option—không cần server management
- Teams có thể focus vào development trong khi Amazon ECS xử lý scaling và orchestration
Amazon EKS (Elastic Kubernetes Service)
Amazon Elastic Kubernetes Service (Amazon EKS) là một fully managed service để chạy Kubernetes trên AWS. Kubernetes là một open source platform tự động hóa containerized application deployment, scaling, và management. EKS làm cho nó thuận tiện để chạy Kubernetes clusters trên AWS. Nó cung cấp nhiều control và flexibility, đặc biệt cho large-scale hoặc hybrid deployments.
Amazon EKS Launch Types
Amazon EKS với Amazon EC2:
- Tốt nhất cho enterprises cần full control over infrastructure
- Cung cấp deep customization của EC2 instances cùng với Kubernetes scalability
- Lý tưởng cho complex, large-scale workloads
Amazon EKS với AWS Fargate:
- Tuyệt vời cho teams muốn Kubernetes flexibility mà không cần quản lý servers
- Kết hợp Kubernetes power với serverless simplicity
- Giúp scale applications nhanh chóng trên various use cases
Amazon ECR (Elastic Container Registry)
Amazon Elastic Container Registry (Amazon ECR) là nơi bạn có thể lưu trữ, quản lý, và deploy container images. Nó hỗ trợ container images tuân theo Open Container Initiative (OCI) standards. Bạn có thể push, pull, và quản lý images trong Amazon ECR repositories của bạn sử dụng standard container tooling và command line interfaces (CLIs).
ECR Features:
- ✅ Fully managed container registry
- ✅ OCI-compliant images
- ✅ Integration với ECS và EKS
- ✅ Image scanning và security
- ✅ Version control cho container images
Orchestration services cần nơi nào đó để lấy containers của chúng. Đó là nơi Amazon Elastic Container Registry, hoặc Amazon ECR, xuất hiện. ECR là một fully managed container registry lưu trữ container images của bạn. Bạn build containers của bạn có application và tất cả dependencies của nó được bundle cùng nhau. Từ đó, một container orchestration tool có thể pull container image và deploy nó.
AWS Fargate
AWS Fargate là một serverless compute engine cho containers. Nó hoạt động với cả Amazon ECS và Amazon EKS. Fargate là một container hosting platform, không giống như Amazon ECS và Amazon EKS, cả hai đều là container orchestration services.
Fargate Benefits:
- ✅ Serverless: Không cần provision hoặc quản lý servers
- ✅ Managed Infrastructure: AWS quản lý server infrastructure cho bạn
- ✅ Focus on Innovation: Bạn có thể focus nhiều hơn vào innovating và developing applications
- ✅ Pay-per-use: Bạn chỉ trả cho resources cần thiết để chạy containers của bạn
Khi sử dụng Fargate, bạn không cần provision hoặc quản lý servers. Fargate quản lý server infrastructure cho bạn. Bạn có thể focus nhiều hơn vào innovating và developing applications, và bạn chỉ trả cho resources cần thiết để chạy containers của bạn.
So sánh EC2 và Fargate
Bạn đã có container image của mình, và bạn đã chọn một orchestration service. Bây giờ, hãy focus vào nơi containers của bạn sẽ thực sự chạy. AWS cung cấp hai tùy chọn chính cho điều này: Amazon EC2 và AWS Fargate.
Amazon EC2
Với EC2, bạn quản lý virtual machines chạy containers của bạn. Với tùy chọn này, bạn có full control, nhưng bạn cần quản lý underlying infrastructure.
Use Cases:
- Cần full control over infrastructure
- Custom configurations
- Specific hardware requirements
AWS Fargate
Mặt khác, Fargate là serverless và cung cấp efficiency và convenience. Với Fargate, AWS quản lý servers, và bạn chỉ cần lo lắng về containers của mình. Không cần quản lý fleet cho các containers này chạy trên đó.
Use Cases:
- Muốn focus vào application, không phải infrastructure
- Variable traffic patterns
- Serverless preference
Workflow: Cách tất cả các phần kết hợp với nhau
Hãy đi qua một ví dụ về cách tất cả các phần này kết hợp với nhau:
Bước 1: Upload Container Images lên ECR
Đầu tiên, bạn sẽ upload container images của mình lên ECR, đây là nơi images của bạn được lưu trữ securely và sẵn sàng để sử dụng.
- Build container image với application và dependencies
- Push image lên Amazon ECR repository
- Image được lưu trữ securely và versioned
Bước 2: Chọn Orchestration Service
Sau đó, bạn sẽ chọn một orchestration service dựa trên những gì bạn cần, hoặc ECS hoặc EKS.
- Amazon ECS: Streamlined và integrated với AWS
- Amazon EKS: Kubernetes flexibility và control
Bước 3: Chọn Compute Option
Cuối cùng, bạn sẽ chọn compute option của mình, hoặc EC2 hoặc Fargate.
- Amazon EC2: Full control, manage infrastructure
- AWS Fargate: Serverless, no infrastructure management
Kết quả
Với AWS, deploying và managing containers là convenient, efficient, và scalable—hoàn hảo để giữ focus của bạn ở nơi quan trọng nhất: trên application của bạn.
So sánh các AWS Container Services
| Service | Type | Mô tả | Use Case |
|---|---|---|---|
| Amazon ECS | Orchestration | Scalable container orchestration service cho AWS | Streamlined, integrated với AWS |
| Amazon EKS | Orchestration | Fully managed Kubernetes service | Kubernetes flexibility, large-scale |
| Amazon ECR | Registry | Container registry để lưu trữ và quản lý images | Store, manage, version container images |
| AWS Fargate | Compute | Serverless compute engine cho containers | No server management, focus on code |
Tóm tắt
Trong bài học này, bạn đã học về:
- ✅ Containers: Package application và dependencies thành portable units
- ✅ Containers vs VMs: Containers nhẹ hơn và nhanh hơn vì chia sẻ OS
- ✅ Deployment Consistency: Containers đảm bảo environment nhất quán
- ✅ Container Orchestration: Tự động hóa deployment, scaling, và management
- ✅ Amazon ECS: Container orchestration service của AWS
- ✅ Amazon EKS: Fully managed Kubernetes service
- ✅ Amazon ECR: Container registry để lưu trữ images
- ✅ AWS Fargate: Serverless compute engine cho containers
- ✅ EC2 vs Fargate: So sánh compute options cho containers
Test your skills
Câu hỏi 1
A developer just finished building an application that runs flawlessly on their laptop. However, when their team tries to deploy it in a different environment, it crashes because of missing dependencies and configuration mismatches.
How do containers help solve this problem?
- By providing access to multiple operating systems for compatibility testing
- By packaging the application and everything it needs to run into one consistent environment ← Correct
- By automatically scaling the application across multiple cloud providers
- By eliminating the need to write configuration files
Giải thích: Mặc dù testing trên các environments khác nhau có thể giúp, containers không được thiết kế để cung cấp access đến multiple operating systems. Thay vào đó, chúng tạo ra một environment nhất quán, portable bất kể underlying system.
Câu hỏi 2
Match each AWS container service with its definition:
Services:
- Amazon Elastic Kubernetes Service (Amazon EKS)
- AWS Fargate
- Amazon Elastic Container Registry (Amazon ECR)
- Amazon Elastic Container Service (Amazon ECS)
Definitions:
- Scalable container orchestration service for running containers on AWS
- Fully managed Kubernetes service for deploying and scaling containers
- Stores, manages, and deploys Open Container Initiative (OCI)-compliant container images
- Serverless compute engine for containers—removes the need to manage servers
Đáp án:
- 1 → Fully managed Kubernetes service for deploying and scaling containers: Amazon EKS giúp đơn giản hóa Kubernetes deployment.
- 2 → Serverless compute engine for containers—removes the need to manage servers: Fargate là serverless compute engine, loại bỏ nhu cầu quản lý infrastructure.
- 3 → Stores, manages, and deploys Open Container Initiative (OCI)-compliant container images: Amazon ECR lưu trữ và quản lý OCI-compliant container images.
- 4 → Scalable container orchestration service for running containers on AWS: Amazon ECS là scalable service để orchestrate containers trên AWS.
Giải thích: Amazon ECS là scalable service để orchestrate containers trên AWS, trong khi Amazon EKS giúp đơn giản hóa Kubernetes deployment. Amazon ECR lưu trữ và quản lý OCI-compliant container images, tích hợp với Amazon ECS, Amazon EKS, và Fargate. Fargate là serverless compute engine cho containers, loại bỏ nhu cầu quản lý infrastructure và hoạt động với Amazon ECS và Amazon EKS.