๐ŸŒฑ Infra/Container_AWS_ECS

[CodePipeline] ECR์„ ์†Œ์Šค๋กœ ํ•ด์„œ CodeDeploy๋กœ ECS ๋ฐฐํฌํ•˜๊ธฐ

mini_world 2022. 7. 16. 02:24
๋ชฉ์ฐจ ์ ‘๊ธฐ

์•ˆ๋…•ํ•˜์„ธ์š” :) 
์˜ค๋ž˜๊ฐ„๋งŒ์˜ ํฌ์ŠคํŒ…์ž…๋‹ˆ๋‹ค.

์˜ค๋Š˜์€ ECR์— ์ด๋ฏธ์ง€๊ฐ€ Push ๋˜๋ฉด, ECS์„œ๋น„์Šค ๋ฐฐํฌ ๋ฐฉ์‹(blue/green, rolling)์— ๋”ฐ๋ผ ECS์— ๋ฐฐํฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.
๊ทธ๋ƒฅ, ECR ๋ ˆํฌ์ง€ํ† ๋ฆฌ์— latest ํƒœ๊ทธ๋ฅผ ๊ฐ€์ง„ ๋„์ปค์ด๋ฏธ์ง€๊ฐ€ push ๋˜๋ฉด, ๋ฐ”๋กœ ๋ฐฐํฌ๋˜๊ฒŒ๋”... ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค.

AWS ๊ณต์‹ ๋ฌธ์„œ๋Š” ์—ฌ๊ธฐ๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ECS ์„œ๋น„์Šค๊ฐ€ ์ด๋ฏธ ๊ตฌ์„ฑ ๋˜์–ด์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค.

 


๐Ÿ“Œ ์•„ํ‚คํ…์ณ

 

 


๐Ÿ“Œ Step 1) CodeCommit ๊ตฌ์„ฑ

codecommit์ด ์•„๋‹ˆ์–ด๋„ ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค. Github, Bitbucket๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

  • ์ฝ”๋“œ์ปค๋ฐ‹ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  • ๋ธ”๋ฃจ/๊ทธ๋ฆฐ ๋ฐฐํฌ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ appspec.yaml, taskdef.json ๋‘๊ฐœ์˜ ํŒŒ์ผ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.
  • ๋กค๋ง ๋ฐฐํฌ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ imagedefinitions.json ํ•˜๋‚˜์˜ ํŒŒ์ผ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.
  • ๊ฐ ๋ฐฐํฌ ๋ฐฉ์‹์— ๋งž๋Š” ํŒŒ์ผ์„ CodeCommit์— ์—…๋กœ๋“œ ํ•˜์—ฌ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค.

 

๐Ÿ‘‡ appspec.yaml ์ƒ˜ํ”Œ

๋”๋ณด๊ธฐ

์ปจํ…Œ์ด๋„ˆ ์„ค์ •์— ๋งž๊ฒŒ ContainerName, ContainerPort ๋‘๊ฐœ๋งŒ ์ˆ˜์ •ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

version: 0.0
Resources:
  - TargetService:
      Type: AWS::ECS::Service
      Properties:
        TaskDefinition: <TASK_DEFINITION>
        LoadBalancerInfo:
          ContainerName: "์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„"
          ContainerPort: 80
        CapacityProviderStrategy:
          - Base: 0
            CapacityProvider: "์šฉ๋Ÿ‰๊ณต๊ธ‰์ž ์ด๋ฆ„"
            Weight: 1
# # Optional properties
#         PlatformVersion: "LATEST"
#         NetworkConfiguration:
#             AwsvpcConfiguration:
#               Subnets: ["subnet-name-1", "subnet-name-2"]
#               SecurityGroups: ["security-group"]
#               AssignPublicIp: "ENABLED"
# Hooks:
# - BeforeInstall: "BeforeInstallHookFunctionName"
# - AfterInstall: "AfterInstallHookFunctionName"
# - AfterAllowTestTraffic: "AfterAllowTestTrafficHookFunctionName"
# - BeforeAllowTraffic: "BeforeAllowTrafficHookFunctionName"
# - AfterAllowTraffic: "AfterAllowTrafficHookFunctionName"

 

๐Ÿ‘‡ taskdef.json ์ƒ˜ํ”Œ

๋”๋ณด๊ธฐ

"aws ecs describe-task-definition --task-definition <์ž‘์—…์ •์˜:๋ฒ„์ „>"์„ ํ„ฐ๋ฏธ๋„์—์„œ ์‹คํ–‰ํ•˜์—ฌ ๊ฐ’์„ ํ™•์ธํ•˜๊ณ , 
"taskDefinition" ํ‚ค ์•„๋ž˜์˜ ๊ฐ’์„ ๋ชจ๋‘ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. 
taskdef.jon์— ์ •์˜๋œ ๋ฐ๋กœ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์‹คํ–‰๋˜๊ธฐ๋•Œ๋ฌธ์— ์ฃผ์˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

{
    "executionRoleArn": "arn:aws:iam::account_ID:role/ecsTaskExecutionRole",
    "containerDefinitions": [
        {
            "name": "sample-website",
            "image": "<IMAGE1_NAME>",
            "essential": true,
            "portMappings": [
                {
                    "hostPort": 80,
                    "protocol": "tcp",
                    "containerPort": 80
                }
            ]
        }
    ],
    "requiresCompatibilities": [
        "FARGATE"
    ],
    "networkMode": "awsvpc",
    "cpu": "256",
    "memory": "512",
    "family": "ecs-demo"
}

 

๐Ÿ‘‡  imagedefinitions.json ์ƒ˜ํ”Œ

๋”๋ณด๊ธฐ

 ์ œ์ผ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„๊ณผ ์ด๋ฏธ์ง€ ํƒœ๊ทธ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

[
  {
    "name": "์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„"
    "imageUri": "์ด๋ฏธ์ง€URL:latest"
  }
]

 

 

๐Ÿ“Œ Step 2) CodePipeline ๊ตฌ์„ฑ

๊ฒฐ๊ณผ ๋จผ์ € ๋ณด์ž๋ฉด, ์•„๋ž˜์ฒ˜๋Ÿผ ๊ตฌ์„ฑ๋˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. 
์ฃผ์˜ํ•ด์•ผํ•  ๋ถ€๋ถ„์€ CodeCommit/Github/bitbucket ๋จผ์ € ์†Œ์Šค์— ๋“ฑ๋กํ•œ ํ›„ ECR์„ ๋“ฑ๋กํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

1) Source์— ๋ ˆํฌ์ง€ํ† ๋ฆฌ๋ฅผ ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค.

2) ๋นŒ๋“œ ๋‹จ๊ณ„๋ฅผ ๊ฑด๋„ˆ๋œ๋‹ˆ๋‹ค.

3) ๋ฐฐํฌ ์Šคํ…Œ์ด์ง€์—์„œ ๋ฐฐํฌํ•  ECS ์„œ๋น„์Šค ๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ์ง๊ด€์ ์ด๊ธฐ๋•Œ๋ฌธ์—.. ์บก์ณ ์—†์ด ์ง€๋‚˜๊ฐ€๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
     ์™„๋ฃŒ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฆ…๋‹ˆ๋‹ค.

4) ์ฝ”๋“œํŒŒ์ดํ”„๋ผ์ธ ์ˆ˜์ • -> ์†Œ์Šค ์Šคํ…Œ์ด์ง€ ํŽธ์ง‘์„ ๋ˆ„๋ฅด๊ณ  ECR์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
     ์ด๋•Œ ์ถœ๋ ฅ ์•„ํ‹ฐํŽ™ํŠธ ์ด๋ฆ„์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. AWS ๊ณต์‹์ž์Šต์„œ ๋ฌธ์„œ์— ๋”ฐ๋ผ "MyImage"๋กœ ์ง€์ •ํ–ˆ์ง€๋งŒ ์ด๋ฆ„์€ ๋‹ค๋ฅธ๊ฒƒ์œผ๋กœ ํ•ด๋„ ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค.

5) ์œ„ ๋‹จ๊ณ„์—์„œ ์ถ”๊ฐ€ํ•œ ECR ์†Œ์Šค๋ฅผ ํŠธ๋ฆฌ๊ฑฐ ํ•  ์ˆ˜ ์žˆ๋„๋ก Deploy๋„ ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค.
     ์ž…๋ ฅ ์•„ํ‹ฐํŒฉํŠธ์— MyImage๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

      ์•„๋ž˜๋กœ ์Šคํฌ๋กค ๋‹ค์šดํ•˜์—ฌ "์ด๋ฏธ์ง€ ์„ธ๋ถ€ ์ •๋ณด๊ฐ€ ์žˆ๋Š” ์ž…๋ ฅ ์•„ํ‹ฐํŒฉํŠธ" - "MyImage"๋ฅผ ์„ ํƒํ•˜๊ณ ,
       "์ž‘์—… ์ •์˜์˜ ์ž๋ฆฌ ํ‘œ์‹œ์ž ํ…์ŠคํŠธ"- "IMAGE1_NAME"์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.
       ์ž‘์—…์ •์˜์˜ ์ž๋ฆฌ ํ‘œ์‹œ์ž ํ…์ŠคํŠธ๋Š” taskdef.json์—์„œ ์ •์˜ํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ“Œ Step 3) ํ…Œ์ŠคํŠธ

 

์ด์ œ ์ž˜ ๋™์ž‘ํ•˜๋Š”์ง€ ํ…Œ์ŠคํŠธ ํ•ด๋ด…์‹œ๋‹ค.
ECR ๋ ˆํฌ์— ์ด๋ฏธ์ง€๋ฅผ push ํ•ด๋ณด์„ธ์š”! ์ž˜ ๋ ๊ฒ๋‹ˆ๋‹ค.

 


https://docs.aws.amazon.com/ko_kr/codepipeline/latest/userguide/tutorials-ecs-ecr-codedeploy.html

 

์ž์Šต์„œ: Amazon ECR ์†Œ์Šค์™€ ECS-to-CodeDeploy ๋ฐฐํฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ์ดํ”„๋ผ์ธ ์ƒ์„ฑ - AWS CodePipeline

์ด๋Ÿฌํ•œ ๋‹จ๊ณ„๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Amazon ECS ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ๋„คํŠธ์›Œํ‚น๋งŒ ๊ฐ€๋Šฅํด๋Ÿฌ์Šคํ„ฐ ํ…œํ”Œ๋ฆฟ, ํ”„๋กœ๋น„์ €๋‹AWSFargate ์ปจํ…Œ์ด๋„ˆ.AWS Fargate๋Š” ์ปจํ…Œ์ด๋„ˆ ์ธ์Šคํ„ด์Šค ์ธํ”„๋ผ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๊ธฐ์ˆ ์ž…๋‹ˆ๋‹ค. Amazon E

docs.aws.amazon.com

 

 

728x90