AWS CLI를 사용하여 Fargate 시작 유형에 대한 Amazon ECS Windows 작업 생성 - Amazon Elastic Container Service

AWS CLI를 사용하여 Fargate 시작 유형에 대한 Amazon ECS Windows 작업 생성

다음 단계는 AWS CLI를 사용하여 Amazon ECS에서 클러스터를 설정하고, 태스크 정의를 등록하고, Windows 태스크를 실행하고, 기타 일반적인 시나리오를 수행하는 데 도움이 됩니다. 최신 버전의 AWS CLI를 사용하고 있는지 확인합니다. 최신 버전으로 업그레이드하는 방법에 대한 자세한 정보는 AWS Command Line Interface 설치를 참조하세요.

필수 조건

이 자습서에서는 다음 사전 조건이 충족되었다고 가정합니다.

  • 최신 버전의 AWS CLI가 설치 및 구성됩니다. AWS CLI 설치 또는 업그레이드에 관한 자세한 정보는 AWS Command Line Interface 설치를 참조하세요.

  • Amazon ECS 사용 설정의 단계가 완료되었습니다.

  • AWS 사용자는 AmazonECS_FullAccess IAM 정책 예제에 지정된 필수 권한을 가집니다.

  • 사용할 VPC 및 보안 그룹이 생성되었습니다. 이 자습서에서는 Docker Hub에서 호스팅되는 컨테이너 이미지를 사용하므로 작업에서 인터넷에 액세스할 수 있어야 합니다. 작업에 인터넷 경로를 제공하려면 다음 옵션 중 하나를 사용합니다.

    • 탄력적 IP 주소가 있는 NAT 게이트웨이와 함께 프라이빗 서브넷을 사용합니다.

    • 퍼블릭 서브넷을 사용하고 퍼블릭 IP 주소를 작업에 할당합니다.

    자세한 내용은 Virtual Private Cloud 생성 단원을 참조하십시오.

    보안 그룹 및 규칙에 대한 자세한 내용은 Amazon Virtual Private Cloud 사용 설명서의 VPC에 대한 기본 보안 그룹규칙 예를 참조하세요.

  • (선택 사항) AWS CloudShell은 고객에게 자체 EC2 인스턴스를 생성할 필요 없이 명령줄을 제공하는 도구입니다. 자세한 내용은 AWS CloudShell 사용 설명서AWS CloudShell이란 무엇입니까? 섹션을 참조하십시오.

1단계: 클러스터 생성

계정에는 기본적으로 default 클러스터가 할당됩니다.

참고

제공된 default 클러스터를 사용하는 이점은 후속 명령에서 --cluster cluster_name 옵션을 지정할 필요가 없다는 것입니다. 기본 클러스터가 아닌 자체 클러스터를 생성하는 경우, 해당 클러스터에 사용할 각 명령에 --cluster cluster_name을 지정해야 합니다.

다음 명령을 사용하여 고유한 이름의 자체 클러스터를 생성합니다.

aws ecs create-cluster --cluster-name fargate-cluster

출력:

{ "cluster": { "status": "ACTIVE", "statistics": [], "clusterName": "fargate-cluster", "registeredContainerInstancesCount": 0, "pendingTasksCount": 0, "runningTasksCount": 0, "activeServicesCount": 0, "clusterArn": "arn:aws:ecs:region:aws_account_id:cluster/fargate-cluster" } }

2단계: Windows 태스크 정의 등록

Amazon ECS 클러스터에서 Windows 태스크를 실행하려면 먼저 태스크 정의를 등록해야 합니다. 태스크 정의는 그룹화된 컨테이너의 목록입니다. 다음은 웹 앱을 생성하는 단순 태스크 정의의 예입니다. 사용 가능한 태스크 정의 파라미터에 대한 자세한 정보는 Amazon ECS 작업 정의 섹션을 참조하세요.

{ "containerDefinitions": [ { "command": ["New-Item -Path C:\\inetpub\\wwwroot\\index.html -Type file -Value '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p>'; C:\\ServiceMonitor.exe w3svc"], "entryPoint": [ "powershell", "-Command" ], "essential": true, "cpu": 2048, "memory": 4096, "image": "mcr.microsoft.com/windows/servercore/iis:windowsservercore-ltsc2019", "name": "sample_windows_app", "portMappings": [ { "hostPort": 80, "containerPort": 80, "protocol": "tcp" } ] } ], "memory": "4096", "cpu": "2048", "networkMode": "awsvpc", "family": "windows-simple-iis-2019-core", "executionRoleArn": "arn:aws:iam::012345678910:role/ecsTaskExecutionRole", "runtimePlatform": {"operatingSystemFamily": "WINDOWS_SERVER_2019_CORE"}, "requiresCompatibilities": ["FARGATE"] }

위의 예시 JSON은 두 가지 방법으로 AWS CLI에 전달할 수 있습니다. 작업 정의 JSON을 파일로 저장하고 --cli-input-json file://path_to_file.json 옵션을 사용하여 전달할 수 있습니다.

컨테이너 정의에 JSON 파일을 사용하려면

aws ecs register-task-definition --cli-input-json file://$HOME/tasks/fargate-task.json

register-task-definition 명령은 등록을 완료한 후 작업 정의의 설명을 반환합니다.

3단계: 태스크 정의 나열

언제라도 list-task-definitions 명령을 사용하여 계정의 작업 정의를 나열할 수 있습니다. 이 명령은 run-task 또는 start-task를 호출할 때 함께 사용할 수 있는 familyrevision 값을 출력합니다.

aws ecs list-task-definitions

출력:

{ "taskDefinitionArns": [ "arn:aws:ecs:region:aws_account_id:task-definition/sample-fargate-windows:1" ] }

4단계: 서비스 생성

계정에 대한 작업을 등록한 후 클러스터에서 등록된 작업에 대해 서비스를 생성할 수 있습니다. 이 예제에서는 클러스터에서 실행 중인 sample-fargate:1 작업 정의 인스턴스 하나를 사용하여 서비스를 생성합니다. 이 작업에는 인터넷 경로가 필요하므로 이 작업을 수행할 수 있는 두 가지 방법이 있습니다. 한 가지 방법은 퍼블릭 서브넷에서 탄력적 IP 주소가 있는 NAT 게이트웨이로 구성된 프라이빗 서브넷을 사용하는 것입니다. 또 다른 방법은 퍼블릭 서브넷을 사용하고 작업에 퍼블릭 IP 주소를 할당하는 것입니다. 아래 두 가지 예를 모두 제공합니다.

프라이빗 서브넷을 사용하는 예.

aws ecs create-service --cluster fargate-cluster --service-name fargate-service --task-definition sample-fargate-windows:1 --desired-count 1 --launch-type "FARGATE" --network-configuration "awsvpcConfiguration={subnets=[subnet-abcd1234],securityGroups=[sg-abcd1234]}"

퍼블릭 서브넷을 사용하는 예.

aws ecs create-service --cluster fargate-cluster --service-name fargate-service --task-definition sample-fargate-windows:1 --desired-count 1 --launch-type "FARGATE" --network-configuration "awsvpcConfiguration={subnets=[subnet-abcd1234],securityGroups=[sg-abcd1234],assignPublicIp=ENABLED}"

create-service 명령은 등록을 완료한 후 작업 정의의 설명을 반환합니다.

5단계: 서비스 나열

클러스터의 서비스를 나열합니다. 이전 섹션에서 생성한 서비스가 보일 것입니다. 이 명령에서 반환된 서비스 이름 또는 전체 ARN을 기록해 두었다가 나중에 서비스를 설명하는 데 사용할 수 있습니다.

aws ecs list-services --cluster fargate-cluster

출력:

{ "serviceArns": [ "arn:aws:ecs:region:aws_account_id:service/fargate-service" ] }

6단계: 실행 서비스 설명

앞서 검색한 서비스 이름으로 서비스를 설명하여 작업에 관한 정보를 더 많이 가져옵니다.

aws ecs describe-services --cluster fargate-cluster --services fargate-service

성공하면 서비스 실패 및 서비스에 대한 설명이 반환됩니다. 예를 들어 서비스 섹션에서 실행 중이거나 보류 중인 작업 상태와 같이 배포에 대한 정보를 찾을 수 있습니다. 작업 정의, 네트워크 구성 및 타임스탬프가 지정된 이벤트에 대한 정보도 찾을 수 있습니다. 실패 섹션에서는 호출과 관련된 실패(있는 경우)에 대한 정보를 찾을 수 있습니다. 문제 해결에 대한 자세한 정보는 서비스 이벤트 메시지를 참조하세요. 서비스 설명에 대한 자세한 정보는 서비스 설명을 참조하세요.

{ "services": [ { "status": "ACTIVE", "taskDefinition": "arn:aws:ecs:region:aws_account_id:task-definition/sample-fargate-windows:1", "pendingCount": 2, "launchType": "FARGATE", "loadBalancers": [], "roleArn": "arn:aws:iam::aws_account_id:role/aws-service-role/ecs.amazonaws.com/AWSServiceRoleForECS", "placementConstraints": [], "createdAt": 1510811361.128, "desiredCount": 2, "networkConfiguration": { "awsvpcConfiguration": { "subnets": [ "subnet-abcd1234" ], "securityGroups": [ "sg-abcd1234" ], "assignPublicIp": "DISABLED" } }, "platformVersion": "LATEST", "serviceName": "fargate-service", "clusterArn": "arn:aws:ecs:region:aws_account_id:cluster/fargate-cluster", "serviceArn": "arn:aws:ecs:region:aws_account_id:service/fargate-service", "deploymentConfiguration": { "maximumPercent": 200, "minimumHealthyPercent": 100 }, "deployments": [ { "status": "PRIMARY", "networkConfiguration": { "awsvpcConfiguration": { "subnets": [ "subnet-abcd1234" ], "securityGroups": [ "sg-abcd1234" ], "assignPublicIp": "DISABLED" } }, "pendingCount": 2, "launchType": "FARGATE", "createdAt": 1510811361.128, "desiredCount": 2, "taskDefinition": "arn:aws:ecs:region:aws_account_id:task-definition/sample-fargate-windows:1", "updatedAt": 1510811361.128, "platformVersion": "0.0.1", "id": "ecs-svc/9223370526043414679", "runningCount": 0 } ], "events": [ { "message": "(service fargate-service) has started 2 tasks: (task 53c0de40-ea3b-489f-a352-623bf1235f08) (task d0aec985-901b-488f-9fb4-61b991b332a3).", "id": "92b8443e-67fb-4886-880c-07e73383ea83", "createdAt": 1510811841.408 }, { "message": "(service fargate-service) has started 2 tasks: (task b4911bee-7203-4113-99d4-e89ba457c626) (task cc5853e3-6e2d-4678-8312-74f8a7d76474).", "id": "d85c6ec6-a693-43b3-904a-a997e1fc844d", "createdAt": 1510811601.938 }, { "message": "(service fargate-service) has started 2 tasks: (task cba86182-52bf-42d7-9df8-b744699e6cfc) (task f4c1ad74-a5c6-4620-90cf-2aff118df5fc).", "id": "095703e1-0ca3-4379-a7c8-c0f1b8b95ace", "createdAt": 1510811364.691 } ], "runningCount": 0, "placementStrategy": [] } ], "failures": [] }

7단계: 정리

이 자습서로 완료를 한 후에 사용하지 않는 리소스에 대해 요금이 발생하는 것을 방지하기 위해 연결된 리소스를 정리해야 합니다.

서비스를 삭제합니다.

aws ecs delete-service --cluster fargate-cluster --service fargate-service --force

클러스터를 삭제합니다.

aws ecs delete-cluster --cluster fargate-cluster