Amazon ECS 배포 회로 차단기를 구성하고 모니터링하려면 어떻게 해야 합니까?
Amazon Elastic Container Service(Amazon ECS) 배포가 실패할 경우 롤백을 자동화하고 알림을 받고 싶습니다.
간략한 설명
Amazon ECS 배포 회로 차단기를 사용하여 롤백을 자동화하고 배포를 모니터링하려면 다음 단계를 완료하십시오.
- 배포 회로 차단기를 구성합니다.
- Amazon EventBridge를 구성하여 배포 회로 차단기를 모니터링합니다.
- 배포 실패 시나리오를 테스트합니다.
해결 방법
참고: AWS Command Line Interface(AWS CLI) 명령을 실행할 때 오류가 발생하면 AWS CLI의 오류 해결을 참조하십시오. 또한 최신 AWS CLI 버전을 사용하고 있는지 확인하십시오.
배포 회로 차단기 구성
다음 단계를 완료합니다.
- 다음 예시와 비슷한 작업 정의로 JSON 파일을 생성합니다.
참고: 123456789876을 사용자의 AWS 계정 ID로 바꾸십시오. ecsTaskExecutionRole이 없는 경우 작업 실행 역할을 생성하십시오.{ "family": "my-task", "containerDefinitions": [ { "name": "sample-container", "image": "nginx:alpine", "essential": true } ], "executionRoleArn": "arn:aws:iam::123456789876:role/ecsTaskExecutionRole", "networkMode": "awsvpc", "requiresCompatibilities": [ "FARGATE" ], "cpu": "256", "memory": "512" } - 작업 정의를 등록하려면 register-task-definition 명령을 실행합니다.
참고: taskdef-success.json을 작업 정의 JSON 파일로 바꾸십시오.aws ecs register-task-definition \ --cli-input-json file://taskdef-success.json - 배포 회로 차단기 및 롤백이 활성화된 상태로 Amazon ECS 서비스를 생성하려면 create-service 명령을 실행합니다.
참고: subnet-12345를 서브넷으로 바꾸고 sg-12345를 보안 그룹으로 바꾸십시오. 배포 회로 차단기는 롤링 업데이트 배포에만 사용할 수 있으므로 deployment-controller를 type=ECS로 설정해야 합니다.aws ecs create-service \ --cluster default \ --service-name my-sample-service \ --deployment-controller type=ECS \ --desired-count 1 \ --deployment-configuration "deploymentCircuitBreaker={enable=true,rollback=true}" \ --task-definition my-task:1 \ --launch-type FARGATE \ --network-configuration "awsvpcConfiguration={subnets=[subnet-12345],securityGroups=[sg-12345],assignPublicIp=ENABLED}"
기본 클러스터가 없는 경우 create-cluster 명령을 실행하여 클러스터를 생성합니다.
참고: example-cluster를 클러스터 이름으로 바꿉니다.aws ecs create-cluster \ --cluster-name example-cluster - describe-services 명령을 실행하여 Amazon ECS 서비스가 정상 상태인지 확인합니다.
다음 예시와 유사한 출력이 표시됩니다.aws ecs describe-services \ --cluster default \ --services my-sample-service | jq '.services[0].events[] | {message}'{ "message": "(service my-sample-service) has reached a steady state." } { "message": "(service my-sample-service) (deployment ecs-svc/1234567890123456789) deployment completed." } { "message": "(service my-sample-service) has started 1 tasks: (task 2918eb15dd0f4d42affc2a3a07818abf)." }
EventBridge를 구성하여 배포 회로 차단기 모니터링
다음 단계를 완료합니다.
-
create-topic 명령을 실행하여 EventBridge 규칙 대상으로 사용할 Amazon Simple Notification Service(Amazon SNS) 주제를 생성합니다.
aws sns create-topic \ --name my-topic참고: my-topic을 SNS 주제 이름으로 바꾸십시오.
-
필료한 API에 대한 호출을 허용하도록 주제 속성을 업데이트하려면 set-topic-attributes 명령을 실행합니다.
aws sns set-topic-attributes \ --topic-arn arn:aws:sns:eu-west-1:123456789876:my-topic \ --attribute-name Policy \ --attribute-value '{ "Version": "2008-10-17", "Id": "my_topic_policy", "Statement": [ { "Sid": "my_topic_default", "Effect": "Allow", "Principal": { "AWS": "*" }, "Action": [ "SNS:GetTopicAttributes", "SNS:SetTopicAttributes", "SNS:AddPermission", "SNS:RemovePermission", "SNS:DeleteTopic", "SNS:Subscribe", "SNS:ListSubscriptionsByTopic", "SNS:Publish" ], "Resource": "arn:aws:sns:eu-west-1:123456789876:my-topic", "Condition": { "StringEquals": { "AWS:SourceOwner": "123456789876" } } }, { "Sid": "my_topic_for_sns_Publish", "Effect": "Allow", "Principal": { "Service": "events.amazonaws.com" }, "Action": "sns:Publish", "Resource": "arn:aws:sns:eu-west-1:123456789876:my-topic" } ] }'참고: eu-west-1을 AWS 리전으로, 123456789876을 계정 ID로, my-topic을 주제 이름으로 바꾸십시오.
-
이메일로 SNS 주제를 구독하려면 subscribe 명령을 실행합니다.
aws sns subscribe \ --topic-arn arn:aws:sns:eu-west-1:123456789876:my-topic \ --protocol email \ --notification-endpoint example@example.com참고: eu-west-1을 리전으로, 123456789876을 계정 ID로, my-topic을 주제 이름으로, example@example.com을 이메일 주소로 바꾸십시오.
-
수신한 구독 확인 이메일에서 구독 확인을 선택합니다.
-
서비스 배포 실패 이벤트에 대한 EventBridge 규칙을 생성하려면 put-rule 명령을 실행합니다.
aws events put-rule \ --name "EcsServiceDeploymentFailed" \ --event-pattern "{\"source\":[\"aws.ecs\"],\"detail-type\":[\"ECS Deployment State Change\"],\"detail\":{\"eventName\":[\"SERVICE_DEPLOYMENT_FAILED\"]}}" -
SNS 주제를 EventBridge 규칙 대상으로 추가하려면 put-targets 명령을 실행합니다.
aws events put-targets \ --rule EcsServiceDeploymentFailed --targets "Id"="1","Arn"="arn:aws:sns:eu-west-1:123456789876:my-topic"참고: eu-west-1을 리전으로, 123456789876을 계정 ID로, my-topic을 주제 이름으로 바꾸십시오.
배포 실패 시나리오 테스트
다음 단계를 완료합니다.
-
작업 정의에 다음과 비슷한 이미지 태그가 포함된 잘못된 JSON 파일을 생성합니다.
{ "family": "my-task", "containerDefinitions": [ { "name": "sample-container", "image": "nginx:wrong-image-tag", "essential": true } ], "executionRoleArn": "arn:aws:iam::123456789876:role/ecsTaskExecutionRole", "networkMode": "awsvpc", "requiresCompatibilities": [ "FARGATE" ], "cpu": "256", "memory": "512" }참고: sample-container를 컨테이너 인스턴스로, nginx:wrong-image-tag를 잘못된 이미지 태그로, 123456789876을 계정 ID로 바꾸십시오. 잘못된 이미지 태그로 인해 배포가 실패합니다.
-
작업 정의를 등록하려면 register-task-definition 명령을 실행합니다.
aws ecs register-task-definition --cli-input-json file://taskdef-failure.json참고: taskdef-failure.json을 작업 정의 JSON 파일의 제목으로 바꾸십시오.
-
새 작업 정의로 서비스를 업데이트하고 새 배포를 시작하려면 update-service 명령을 실행합니다.
aws ecs update-service --service my-sample-service --task-definition my-task:2참고: my-sample-service를 사용자의 서비스로, my-task:2를 사용자의 작업으로 바꾸십시오. 작업에서 이미지를 가져올 수 없기 때문에 새 배포가 실패합니다. 다음 예시와 유사한 출력이 표시됩니다.
{ "version": "0", "id": "12345abc-2f7c-f86a-e544-a69218eb1446", "detail-type": "ECS Deployment State Change", "source": "aws.ecs", "account": "123456789876", "time": "2024-11-19T17:42:41Z", "region": "eu-west-1", "resources": [ "arn:aws:ecs:eu-west-1:123456789876:service/default/my-sample-service" ], "detail": { "eventType": "ERROR", "eventName": "SERVICE_DEPLOYMENT_FAILED", "clusterArn": "arn:aws:ecs:eu-west-1:123456789876:cluster/default", "deploymentId": "ecs-svc/9876543210987654321", "updatedAt": "2024-11-19T17:42:40.73Z", "reason": "ECS deployment circuit breaker: tasks failed to start." } } -
Amazon ECS 서비스가 롤백되었는지 확인하려면 describe-services 명령을 실행합니다.
aws ecs describe-services \ --cluster default \ --services my-sample-service | jq '.services[0].events[] | {message}'다음 예시와 유사한 출력이 표시됩니다.
{ "message": "(service my-sample-service) has reached a steady state." } { "message": "(service my-sample-service) (deployment ecs-svc/1234567890123456789) deployment completed." } { "message": "(service my-sample-service) rolling back to deployment ecs-svc/1234567890123456789." } { "message": "(service my-sample-service) (deployment ecs-svc/9876543210987654321) deployment failed: tasks failed to start." } { "message": "(service my-sample-service) has started 1 tasks: (task b808c60616134ec1ac0c656a2bff1ef2)." } { "message": "(service my-sample-service) has started 1 tasks: (task 846c9aebd9224c2b832a38942cae5ea6)." } { "message": "(service my-sample-service) has started 1 tasks: (task 7143d03444574f2db2b567d75df3fe72)." } { "message": "(service my-sample-service) has started 1 tasks: (task 9a6a399770d940a2b442560c02a6a4c0)." } { "message": "(service my-sample-service) has reached a steady state." } { "message": "(service my-sample-service) (deployment ecs-svc/1234567890123456789) deployment completed." } { "message": "(service my-sample-service) has started 1 tasks: (task 2918eb15dd0f4d42affc2a3a07818abf)." }
관련 정보
- 언어
- 한국어
