[AWS ECS] Fargate ์‹œ์ž‘ ์œ ํ˜•์˜ Amazon ECS์— ์„œ๋ฒ„๋ฆฌ์Šค ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ฐฐํฌํ•˜๊ธฐ

2023. 1. 26. 19:00ใ†AWS

โ˜„๏ธ To-Be Architecture

To-Be Architecture

Fargate Type์˜ Amazon ECS๋ฅผ ์ด์šฉํ•˜์—ฌ ์„œ๋ฒ„๋ฆฌ์Šค ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋ฐฐํฌํ•ด ๋ณด์ž.

์•„๋ž˜์™€ ๊ฐ™์€ ์ˆœ์„œ๋กœ ์ง„ํ–‰ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

ECR์— Docker Image ์—…๋กœ๋“œ → ECS Cluster ์ƒ์„ฑ → ECS Task Definition ์ƒ์„ฑ → ALB ์ƒ์„ฑ → ECS Service ์ƒ์„ฑ(Task ๋ฐฐํฌ)

 

 

 

0. Amazon ECS(Elastic Container Service)๋ž€?

์ปจํ…Œ์ด๋„ˆํ™”๋œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์™„์ „๊ด€๋ฆฌํ˜• ์ปจํ…Œ์ด๋„ˆ ์˜ค์ผ€์ŠคํŠธ๋ ˆ์ด์…˜ ์„œ๋น„์Šค.

ECS ๊ตฌ์„ฑ ์š”์†Œ

  • ECS Task Definition: Docker ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ์„ค์ •๊ฐ’.
  • ECS Task: Task Definition์„ ์ด์šฉํ•˜์—ฌ ๋ฐฐํฌ๋œ Container Set.
  • ECS Service: ECS Cluster์—์„œ ์ง€์ •๋œ ์ˆ˜์˜ Task๋ฅผ ๋™์‹œ์— ์‹คํ–‰ํ•˜๊ณ  ์œ ์ง€ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•˜๋Š” ๊ตฌ์„ฑ.
  • ECS Container: ECS Service ๋‚ด ๊ฐœ๋ณ„ task๋‚˜ ์—ฌ๋Ÿฌ Task๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•˜๋Š” Task Definition์— ์ •์˜๋œ๋‹ค.

ECS Launch Type (์‹œ์ž‘ ์œ ํ˜•)

1. Fargate

  • ์„œ๋ฒ„๋ฆฌ์Šค ์ข…๋Ÿ‰์ œ ์˜ต์…˜.
  • ์ธํ”„๋ผ๋ฅผ ๊ด€๋ฆฌํ•  ํ•„์š” ์—†์ด ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

2. EC2

  • ํด๋Ÿฌ์Šคํ„ฐ์—์„œ EC2 ์ธ์Šคํ„ด์Šค๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  ๋ฐฐํฌํ•˜์—ฌ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.
  • ์ธํ”„๋ผ์— ๋Œ€ํ•œ ๋” ์„ธ๋ถ€์ ์ธ ์ œ์–ด ๊ฐ€๋Šฅ.

 

 

 

1. Amazon ECR(Elastic Container Registry) ์ƒ์„ฑ

ECS์—์„œ ์‚ฌ์šฉํ•  ์ปจํ…Œ์ด๋„ˆ ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค๊ณ  ECR์˜ Repository์— ์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•œ๋‹ค.

1-1. (์ฐธ๊ณ ) Dockerfile ์ž‘์„ฑ

FROM ubuntu:18.04

# Install dependencies
RUN apt-get update && \\
 apt-get -y install apache2

# Install apache and write hello world message
RUN echo 'Hello World!' > /var/www/html/index.html

# Configure apache
RUN echo '. /etc/apache2/envvars' > /root/run_apache.sh && \\
 echo 'mkdir -p /var/run/apache2' >> /root/run_apache.sh && \\
 echo 'mkdir -p /var/lock/apache2' >> /root/run_apache.sh && \\ 
 echo '/usr/sbin/apache2 -D FOREGROUND' >> /root/run_apache.sh && \\ 
 chmod 755 /root/run_apache.sh

EXPOSE 80

CMD /root/run_apache.sh

1-2. ECR Repository ์ƒ์„ฑ

๋ฐฉ๋ฒ• 1) AWS Console

AWS ECS Console > Repositories > Create Repository

๋ฐฉ๋ฒ• 2) AWS CLI

aws ecr create-repository --repository-name { hyeonju-ecr }

1-3. ECR์— ๋„์ปค ์ด๋ฏธ์ง€ ํ‘ธ์‹œ

ECR > Repository ์ฝ˜์†”์—์„œ [View Push Commands] ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ์•„๋ž˜์™€ ๊ฐ™์€ ์ด๋ฏธ์ง€ ํ‘ธ์‹œ ๋ฐฉ๋ฒ• ๊ฐ€์ด๋“œ๊ฐ€ ์ œ๊ณต๋œ๋‹ค.

# macOS/Linux ์—์„œ push commands

# AWS ECR ๋กœ๊ทธ์ธ
aws ecr get-login-password --region { ap-northeast-2 } | docker login --username AWS --password-stdin { account-number.dkr.ecr.ap-northeast-2.amazonaws.com }

# Docker ์ด๋ฏธ์ง€ ๋นŒ๋“œ
docker build -t { hyeonju-ecr } .

# ์ด๋ฏธ์ง€ ํƒœ๊น…
docker tag { hyeonju-ecr:latest } { account-number.dkr.ecr.ap-northeast-2.amazonaws.com/hyeonju-ecr:latest }

# AWS ECR์— ์ด๋ฏธ์ง€ ํ‘ธ์‹œ
docker push { account-number.dkr.ecr.ap-northeast-2.amazonaws.com/hyeonju-ecr:latest }

 

 

 

 

2. ECS Cluster ์ƒ์„ฑ

Networking

ECS Cluster๋ฅผ ๊ตฌ์„ฑํ•  VPC, Subnet์„ ์„ ํƒํ•œ๋‹ค.

Infrastructure

  • Cluster ์ธํ”„๋ผ๋กœ ์›ํ•˜๋Š” ํƒ€์ž… ์„ ํƒ.
  • Fargate๋กœ ๊ตฌ์„ฑํ•œ๋‹ค๋ฉด ๋ณ„๋„๋กœ ์ง€์ •ํ•  ํ•„์š”๋Š” ์—†๋‹ค.

Monitoring > Container Insight ํ™œ์„ฑํ™”(์„ ํƒ)

[Ref] AWS Docs: ํด๋Ÿฌ์Šคํ„ฐ ๋ฐ ์„œ๋น„์Šค ์ˆ˜์ค€ ์ง€ํ‘œ๋ฅผ ์œ„ํ•ด Amazon ECS์—์„œ Container Insights ์„ค์ •

  • ์ปจํ…Œ์ด๋„ˆํ™”๋œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ฐ ๋งˆ์ดํฌ๋กœ ์„œ๋น„์Šค์˜ ์ง€ํ‘œ ๋ฐ ๋กœ๊ทธ๋ฅผ ์ˆ˜์ง‘ํ•˜๊ณ  ์ง‘๊ณ„ํ•˜๋ฉฐ ์š”์•ฝํ•œ๋‹ค.
  • ECS Cluster, ECS Task ๋ฐ ECS Service ๋ ˆ๋ฒจ์—์„œ ์ง€ํ‘œ๋ฅผ ์ˆ˜์ง‘ํ•œ๋‹ค.
  • CloudWatch automatically collects metrics for many resources, such as CPU, memory, disk, and network. Container Insights also provides diagnostic information, such as container restart failures, that you use to isolate issues and resolve them quickly. You can also set CloudWatch alarms on metrics that Container Insights collects.

 

 

 

3. ECS Task Definition ์ƒ์„ฑ

3-1. IAM > ECS Task Execution Role ์ƒ์„ฑ

AWS managed Policy- AmazonECSTaskExecutionRolePolicy๋ฅผ ์—ฐ๊ฒฐํ•œ๋‹ค.

// arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ecr:GetAuthorizationToken",
                "ecr:BatchCheckLayerAvailability",
                "ecr:GetDownloadUrlForLayer",
                "ecr:BatchGetImage",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": "*"
        }
    ]
}
// Trust relationships
{
    "Version": "2008-10-17",
    "Statement": [
        {
            "Sid": "",
            "Effect": "Allow",
            "Principal": {
                "Service": "ecs-tasks.amazonaws.com"
            },
            "Action": "sts:AssumeRole"
        }
    ]
}

3-2. Container ์ •๋ณด ์ž…๋ ฅ

  • Image URI : 1๋ฒˆ ๋‹จ๊ณ„์—์„œ ECR์— ์—…๋กœ๋“œํ•œ Image URI ์ž…๋ ฅ
  • App Environment : AWS Fargate (serverless) ์„ ํƒ
  • Operating system/Architecture : Linux/X86_64
  • Task size(CPU, Memeory) ์„ ํƒ
  • Task role > b์—์„œ ์ƒ์„ฑํ•œ ecsTaskExecutionRole ์„ ํƒ
    • A task execution IAM role is used by the container agent to make AWS API requests on your behalf. If you don't already have a task execution IAM role created, we can create one for you.
  • Network mode : Fargate์˜ ๊ฒฝ์šฐ ์ž๋™ awsvpc ์„ ํƒ.
    • The network mode that's used for your tasks. By default, when the AWS Fargate (serverless) app environment is selected, the awsvpc network mode is used. If you select Amazon EC2 instances app environment, you can use the awsvpc or bridge network mode.

 

 

 

4. ALB ์ƒ์„ฑ

ECS Service์— ์—ฐ๊ฒฐํ•  Load Balancer๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

4-1. Target Group ์ƒ์„ฑ

  • Target Type : IP
    • Services with tasks that use the awsvpc network mode (for example, those with the Fargate launch type) only support Application Load Balancers and Network Load Balancers. Classic Load Balancers aren't supported. Also, when you create any target groups for these services, you must choose ip as the target type, not instance. This is because tasks that use the awsvpc network mode are associated with an elastic network interface, not an Amazon EC2 instance.
  • Port : Container Port
  • Health Check ๊ฒฝ๋กœ๋„ ๋ฏธ๋ฆฌ ์„ค์ •ํ•œ๋‹ค.

4-2. ALB ์ƒ์„ฑ

Application Load Balancer ์ƒ์„ฑ ๋ฐ 4-1์—์„œ ๋งŒ๋“  Target Group์œผ๋กœ ํŠธ๋ž˜ํ”ฝ์„ ์ „๋‹ฌํ•˜๋Š” ๋ฆฌ์Šค๋„ˆ ์—ฐ๊ฒฐ

 

 

 

5. ECS Service ์ƒ์„ฑ

๋””๋ฒ„๊น… ๋ชฉ์ ์œผ๋กœ ๋ช…๋ น์–ด ์‹คํ–‰์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ(—enable-execute-command) ํ•˜๊ธฐ ์œ„ํ•ด AWS CLI๋ฅผ ์ด์šฉํ•˜์—ฌ ECS Service๋ฅผ ์ƒ์„ฑํ•˜์˜€๋‹ค. ์›ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ AWS Console์„ ํ†ตํ•ด ์ƒ์„ฑํ•˜์—ฌ๋„ ๋ฌด๋ฐฉํ•˜๋‹ค.

Auto Scaling์€ ๋ณ„๋„ ์ ์šฉํ•˜์ง€ ์•Š์•˜๋‹ค.

aws ecs create-service \
--service-name hyeonju-ecs-service \
--cluster hyeonju-ecs-cluster \
--task-definition hyeonju-ecs-td \
--desired-count 2 \
--launch-type "FARGATE" \
--load-balancers targetGroupArn=arn:aws:elasticloadbalancing:ap-northeast-2:account-number:targetgroup/security-group-name/3###############,containerName=hyeonju-container,containerPort=80 \
--network-configuration "awsvpcConfiguration={subnets=[subnet-0###############,subnet-0###############],securityGroups=[sg-0###############]}" \
--enable-execute-command
  • enable-execute-command
    • enables execute command functionality on all containers in the service tasks.
  • LoadBalancer
    • Configure a load balancer to distribute incoming traffic across the tasks running in your service.
    • 3์—์„œ ์ƒ์„ฑํ•œ ALB์˜ Target Group ARN์„ ์ž…๋ ฅํ•œ๋‹ค.

๐Ÿ’ก ์ฝ˜์†”์—์„œ Service ์ƒ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ, ์•„๋ž˜ ์ฐธ๊ณ .

๋”๋ณด๊ธฐ

์ƒ์„ฑํ•œ cluster > Services ํƒญ > Create ๋ฒ„ํŠผ์œผ๋กœ ์ƒ์„ฑ ๊ฐ€๋Šฅ.

  • Launch Type : Fargate
  • Application Type
    • Service : ECS Cluster์—์„œ ์ง€์ •๋œ ์ˆ˜์˜ ์ž‘์—… ์ •์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋™์‹œ์— ์‹คํ–‰ํ•˜๊ณ  ์œ ์ง€, ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์žฅ๊ธฐ๊ฐ„ ์‹คํ–‰๋˜๋Š” ์ƒํƒœ ๋น„์ €์žฅ ์„œ๋น„์Šค ๋ฐ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜๊ณผ ์ ํ•ฉํ•˜๋‹ค. ์„œ๋น„์Šค ์ž‘์—…์ด ์‹คํŒจํ•˜๊ฑฐ๋‚˜ ์ค‘์ง€๋˜๋ฉด ์„œ๋น„์Šค ์Šค์ผ€์ค„๋Ÿฌ๋Š” ์ž‘์—… ์ •์˜์˜ ๋‹ค๋ฅธ ์ธ์Šคํ„ด์Šค๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ํ•ด๋‹น ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ”๊พธ์–ด ์„œ๋น„์Šค์—์„œ ์›ํ•˜๋Š” ์ž‘์—… ์ˆ˜๋ฅผ ์œ ์ง€ํ•œ๋‹ค.
    • (Standalone) Task : ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•œ ํ›„ ์ค‘์ง€ํ•˜๋Š” ๋ฐฐ์น˜ ์ž‘์—…๊ณผ ๊ฐ™์€ ํ”„๋กœ์„ธ์Šค์— ๊ฐ€์žฅ ์ ํ•ฉํ•˜๋‹ค.

๐Ÿ’ก Service๊ฐ€ ์ž˜ ์ƒ์„ฑ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ณด์ž = Task๊ฐ€ ์ž˜ ๋ฐฐํฌ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ณด์ž.

  1. ALB์˜ Target Group์— Target์ด ์ •์ƒ์ ์œผ๋กœ ๋“ฑ๋ก๋˜์—ˆ๋Š”์ง€ ๋ฐ healthy์ธ์ง€ ํ™•์ธ.
  2. ECS์˜ Task์— ์›ํ•˜๋Š” ์ˆ˜์˜ task๊ฐ€ ์ •์ƒ์ ์œผ๋กœ running ์ค‘์ธ์ง€ ํ™•์ธ. ALB์˜ Target Group์—์„œ health check๊ฐ€ ์ œ๋Œ€๋กœ ๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ Task๊ฐ€ ๋“ฑ๋ก๊ณผ drain์„ ๋ฐ˜๋ณตํ•œ๋‹ค.
  3. ALB์˜ Endpoint๋กœ ์ ‘๊ทผ ์‹œ, ์ •์ƒ์ ์œผ๋กœ ๋™์ž‘ํ•˜๋Š”์ง€ ํ™•์ธ.

๐Ÿ’ก Task Definition ์ˆ˜์ • ์‹œ, Service Update๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

Task Definition ์ˆ˜์ • ๋“ฑ์œผ๋กœ ์ธํ•œ new revision ์ƒ์„ฑ ํ›„, ์ƒˆ๋กœ์šด ๋ฒ„์ „์œผ๋กœ ๋ฐฐํฌ๋ฅผ ์œ„ํ•ด์„œ๋Š” Service๋„ ๋ฐ˜๋“œ์‹œ update๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

 

 

 

6. ECS Exec ์‚ฌ์šฉํ•˜๊ธฐ (์„ ํƒ)

ECS Exec์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ช…๋ น์„ ์‹คํ–‰ํ•˜๊ฑฐ๋‚˜ Amazon EC2 ์ธ์Šคํ„ด์Šค ๋˜๋Š” AWS Fargate์—์„œ ์‹คํ–‰ํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ์— ์…ธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์ปจํ…Œ์ด๋„ˆ์— ๋Œ€ํ•œ ์ค‘๋‹จ ์—†์ด ์•ก์„ธ์Šค ํ•˜๊ณ  ๋ฌธ์ œ๋ฅผ ๋””๋ฒ„๊น…ํ•  ์ˆ˜ ์žˆ๋‹ค.

6-1. ์‚ฌ์ „ ์กฐ๊ฑด

AWS CLI์— ๋Œ€ํ•œ Session Manager ํ”Œ๋Ÿฌ๊ทธ์ธ์ด ์„ค์น˜๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

6-2. ECS Service์— ecs exec Enablement ์—ฌ๋ถ€ ํ™•์ธ

aws ecs describe-tasks --cluster hyeonju-ecs-cluster --tasks { task-id }

# ํ™œ์„ฑํ™” ์—ฌ๋ถ€๋งŒ ์ถœ๋ ฅํ•˜๊ธฐ
# aws ecs describe-tasks --cluster hyeonju-ecs-cluster --tasks { task-id } | grep enableExecuteCommand

false์ธ ๊ฒฝ์šฐ, ECS Service ๊ตฌ์„ฑ ์ •๋ณด ์—…๋ฐ์ดํŠธ ํ•„์š”.

์•„๋ž˜์˜ command๋ฅผ ํ†ตํ•ด ECS Service์˜ enableExecuteCommand ์˜ต์…˜์„ ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ์œผ๋‚˜, ECS Service๋ฅผ ํ†ตํ•ด Task๊ฐ€ ์ƒ์„ฑ๋œ ๊ฒฝ์šฐ Service ์—…๋ฐ์ดํŠธ ํ›„ Task๊ฐ€ ์ƒˆ๋กœ ์ƒ์„ฑ๋˜์–ด์•ผ๋งŒ ์ ‘์†์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

aws ecs update-service \
	--cluster hyeonju-ecs-cluster \
	--service { service-name } \
	--enable-execute-command

 

6-3. Task Role ์„ค์ •

Task Definition์— ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” Task Role์— ECS Exec์— ํ•„์š”ํ•œ ๊ถŒํ•œ์ด ํฌํ•จ๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ssmmessages:CreateControlChannel",
                "ssmmessages:CreateDataChannel",
                "ssmmessages:OpenControlChannel",
                "ssmmessages:OpenDataChannel"
            ],
            "Resource": "*"
        }
    ]
}

6-4. ECS Exec์ด ๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธํ•ด ๋ณด์ž.

aws ecs execute-command \
	--cluster hyeonju-ecs-cluster \
    --container { container-name }
    --task { task-id } \
    --interactive \
    --command "/bin/sh"

 

 

 

 

๐Ÿ”— ์ฐธ๊ณ  ๋ฌธ์„œ