</>Học Dev
Bài học

Tuần 5 - Ngày 2: Containers (ECS, EKS, Fargate)

Tuần 5 – Ngày 2

Tuần 5 - Ngày 2: Containers (ECS, EKS, Fargate)

Mục tiêu học tập

  • Hiểu architecture của ECS và EKS
  • Nắm vững Fargate vs EC2 launch types
  • Biết cách design container architectures

1. Container Services Overview

AWSCONTAINERSERVICESORCHESTRATION:ECS(AWSnative)EKS(Kubernetes)COMPUTE:EC2(self-managed)Fargate(serverless)Outposts(on-premises)REGISTRY:ECR(ElasticContainerRegistry)DEPLOYMENT:CodeDeploy(ECS)AppMesh(servicemesh)CloudMap(servicediscovery)

2. ECS Architecture

Components

ECSCLUSTERECSSERVICETASKDEFINITION-Containerimages-CPU/Memory-Portmappings-Environmentvariables-IAMTaskRoleTASKTASKTASK(running)(running)(running)CAPACITYPROVIDERS:FargateFargateSpotEC2AutoScalingGroup

EC2 vs Fargate Launch Type

FeatureEC2Fargate
Server managementYou manageAWS manages
PricingPer EC2 instancePer vCPU/memory/second
ScalingASG + Capacity ProviderAutomatic
GPU supportYesNo
Spot pricingVia EC2 SpotFargate Spot
Persistent storageEBSEFS only

3. EKS Architecture

Components

EKSCLUSTERCONTROLPLANE(AWSManaged)-APIServer-etcd-ControllerManager-SchedulerDATAPLANE(Youmanage)NodeGroups:ManagedNodeGroups(recommended)Self-ManagedNodesFargateProfiles

EKS Add-ons

  • CoreDNS
  • kube-proxy
  • Amazon VPC CNI
  • AWS Load Balancer Controller
  • EBS CSI Driver
  • EFS CSI Driver

4. Service Discovery & Mesh

AWS Cloud Map

AWSCLOUDMAPNamespace:company.localServices:api.company.localInstance1:10.0.1.50:8080Instance2:10.0.2.50:8080database.company.localInstance1:10.0.3.100:5432Discovery:DNSorAPI

AWS App Mesh

Service Mesh for:
- Traffic routing
- Observability (X-Ray integration)
- mTLS between services
- Circuit breakers
- Retries

Components:
- Virtual Nodes
- Virtual Routers
- Virtual Services
- Envoy proxy (sidecar)

Lưu ý 2026: AWS đã announce App Mesh end-of-support cuối 2026. Workload mới nên dùng VPC Lattice (managed) hoặc Istio/Linkerd self-managed.

Amazon VPC Lattice (GA 2023)

Managed application networking cho service-to-service traffic — thay thế use case của App Mesh cho microservices cross-account/cross-VPC.

VPCLATTICEServicelogicalservice(consumer-facing)Networkscope:VPC,account,hocOrganizationassociateVPCs(cross-account,cross-regionOK)ProviderServices:-HTTP/HTTPS,gRPC,HTTP/2-Targettypes:EC2,ALB,Lambda,IP,k8spods(viaEKS)-Weightedrouting,healthchecks,retries-IAMauth-basedaccesscontrol(resource-levelpolicies)-VPCLatticeCloudWatch/Firehoseaccesslogs

Khi nào dùng VPC Lattice (vs alternatives):

Use caseLatticeApp MeshALB/NLB + PrivateLink
Cross-account service mesh, IAM-based auth✓ bestPhức tạpKhả thi nhưng tốn cấu hình
Sidecar-free (no Envoy injection)✗ (Envoy)
Layer 7 routing với weighted targetALB only
EKS service-to-service trong clusterOK✓ (legacy)
Cần circuit breaker, mTLS pinnedHạn chế
Single-VPC traffic internalALB tốt hơnOKALB tốt hơn

Exam keyword:

  • "Cross-account service communication với least operational overhead" → VPC Lattice
  • "Centralized auth + observability cho microservices spread across multiple VPCs" → VPC Lattice
  • "Existing App Mesh in production" → vẫn dùng (Lattice migration path), nhưng workload mới chọn Lattice.

5. Container Security

IAM Roles

Task Role:
- Permissions for containers in task
- Access AWS services (S3, DynamoDB, etc.)

Task Execution Role:
- Permissions for ECS agent
- Pull images from ECR
- Send logs to CloudWatch
- Retrieve secrets

Secrets Management

Options:
1. Secrets Manager
   - Automatic rotation
   - Cross-account sharing

2. Parameter Store
   - Simple secrets
   - Lower cost

3. Environment variables (not recommended for secrets)

Integration:
{
  "secrets": [{
    "name": "DB_PASSWORD",
    "valueFrom": "arn:aws:secretsmanager:region:account:secret:name"
  }]
}

6. Câu hỏi ôn tập

  1. ECS Task Definition chứa những gì?

    Xem đáp án

    Task Definition là blueprint cho container(s): (1) Container definitions (image URI, CPU/memory, port mappings, env vars, secrets, log config), (2) Launch type (Fargate/EC2) và network mode (awsvpc cho Fargate), (3) CPU và Memory allocation cho task, (4) Task Execution Role (ECS agent permissions), (5) Task Role (app code permissions), (6) Volumes, health check, stop timeout. Versioned — tạo revision mới khi thay đổi.

  2. Fargate vs EC2 launch type khác nhau như thế nào?

    Xem đáp án

    Fargate: serverless compute — không quản lý EC2, pay per task vCPU+GB, tự scale, không cần ECS agent, mỗi task có ENI riêng (awsvpc only). EC2 launch type: bạn quản lý EC2 cluster (patch OS, scale cluster, ECS Agent), pay for EC2 even when idle, có thể dùng Spot, GPU instances. Fargate tốt cho variable workloads, new services; EC2 cho steady workload, cost optimization với large clusters, hoặc GPU needs.

  3. EKS Managed Node Groups là gì?

    Xem đáp án

    EKS Managed Node GroupsAWS-managed EC2 nodes cho EKS cluster — AWS tự động provision, configure, và upgrade worker nodes. Tự động apply Kubernetes version upgrades khi cluster upgrade. Giảm operational overhead so với self-managed nodes. Hỗ trợ Spot Instances và On-Demand. Khác EKS Fargate Profiles (fully serverless, không manage nodes). Managed Node Groups là middle ground: có node visibility nhưng AWS handle lifecycle.

  4. AWS Cloud Map dùng để làm gì?

    Xem đáp án

    Service Discovery cho microservices — register services với name và attributes, clients discover qua DNS hoặc API calls. ECS tích hợp sẵn với Cloud Map: khi task start → tự register, khi task stop → tự deregister. Clients lookup service.namespace để get healthy endpoints. Hỗ trợ health check integration. Thay thế cho Consul, Eureka trong AWS-native environments. Phù hợp cho ECS, EKS, Lambda, EC2 microservices.

  5. Task Role khác Task Execution Role như thế nào?

    Xem đáp án

    Task Execution Role: permissions cho ECS agent (infrastructure side) — pull image từ ECR, write logs đến CloudWatch, get secrets từ Secrets Manager/Parameter Store khi task start. Task Role: permissions cho application code trong container — AWS API calls mà app cần lúc runtime (S3 read, DynamoDB write, SQS send...). Least privilege: Task Role chỉ có quyền app cần, không có quyền infrastructure. Cả hai là IAM Roles riêng biệt.


Tài liệu tham khảo chính thức


Ngày tiếp theo: Lambda & Serverless