AWS CloudFormation에서 Amazon ECS 서비스 검색을 사용하려면 어떻게 해야 합니까?
최종 업데이트 날짜: 2020년 4월 8일
AWS CloudFormation에서 Amazon Elastic Container Service(Amazon ECS) 서비스 검색을 사용하려고 합니다.
간략한 설명
Amazon ECS 콘솔과 달리, AWS CloudFormation용 AWS::ECS::Service 리소스 유형은 서비스 생성 도중에 서비스 검색 리소스를 생성하지 않습니다. 이 점은 Amazon ECS 콘솔과 다른데, 이 덕분에 서비스 생성 도중에 서비스 검색 리소스를 생성할 수 있습니다.
참고: Amazon ECS 콘솔은 CreatePrivateDnsNamespace, CreateService(AWS Cloud Map) 및 CreateService(Amazon ECS) 통합 API를 사용합니다. AWS CloudFormation에서는 AWS:: ServiceDiscovery:: PrivateDnsNamespace, AWS:: ServiceDiscovery:: Service, 및 AWS:: ECS:: Service 리소스 유형을 이용하여 하나의 AWS CloudFormation 템플릿에서 통합을 구현할 수 있습니다.
해결 방법
1. AWS CloudFormation 템플릿의 리소스 섹션에서 기존 Amazon Virtual Private Cloud(Amazon VPC)에 프라이빗 서비스 검색 네임스페이스(예: example.com)를 생성하십시오. 다음의 JSON 및 YAML 예제를 참조하십시오.
JSON:
{
"PrivateNamespace": {
"Type": "AWS::ServiceDiscovery::PrivateDnsNamespace",
"Properties": {
"Name": "example.com",
"Vpc": "vpc-xxxxxxx"
}
}
}
YAML:
PrivateNamespace:
Type: AWS::ServiceDiscovery::PrivateDnsNamespace
Properties:
Name: example.com
Vpc: vpc-xxxxxxx
참고: example.com을 네임스페이스의 이름으로 바꾸십시오. vpc-xxxxxxx를 계정의 VPC ID로 바꾸십시오.
2. 1단계에서 생성한 네임스페이스의 네임스페이스 ID를 사용하여 서비스 검색 서비스를 생성하십시오.
AWS CloudFormation 템플릿의 Ref 내장 함수를 사용하여 네임스페이스 ID 값을 가져올 수 있습니다. 다음의 JSON 및 YAML 예제를 참조하십시오.
JSON:
{
"DiscoveryService": {
"Type": "AWS::ServiceDiscovery::Service",
"Properties": {
"Description": "Discovery Service for the Demo Application",
"DnsConfig": {
"RoutingPolicy": "MULTIVALUE",
"DnsRecords": [
{
"TTL": 60,
"Type": "A"
},
{
"TTL": 60,
"Type": "SRV"
}
]
},
"HealthCheckCustomConfig": {
"FailureThreshold": 1
},
"Name": "example-service-name",
"NamespaceId": {
"Ref": "PrivateNamespace"
}
}
}
}
YAML:
DiscoveryService:
Type: AWS::ServiceDiscovery::Service
Properties:
Description: Discovery Service for the Demo Application
DnsConfig:
RoutingPolicy: MULTIVALUE
DnsRecords:
- TTL: 60
Type: A
- TTL: 60
Type: SRV
HealthCheckCustomConfig:
FailureThreshold: 1
Name: example-service-name
NamespaceId: !Ref PrivateNamespace
참고: example-service-name은 서비스 이름으로 바꾸십시오.
DnsConfig 속성은 Amazon ECS 태스크가 서비스 검색 서비스에 등록될 때 Amazon Route 53에서 자동으로 생성되는 DNS 레코드를 지정합니다.
참고: A 및 SRV 유형의 DNS 레코드가 지원됩니다.
팁: HealthCheckCustomConfig 속성을 사용하여 Amazon ECS가 태스크 상태를 서비스 검색에 보고할 수 있도록 하는 것이 가장 좋습니다. Amazon ECS는 컨테이너 확인, 상태 확인 및 태스크 상태의 정보를 사용하여 AWS Cloud Map과 함께 서비스 검색 상태를 업데이트합니다.
3. 이전 단계에서 생성한 서비스 검색 네임스페이스 및 서비스와 통합되는 Amazon ECS 서비스를 생성하려면, AWS:ECS::Service 리소스의 ServiceRegistries 속성을 사용하여 통합을 지정하십시오. 다음의 JSON 및 YAML 예제를 참조하십시오.
JSON:
{
"EcsService": {
"Type": "AWS::ECS::Service",
"Properties": {
"Cluster": "awsExampleCluster",
"DesiredCount": 4,
"LaunchType": "FARGATE",
"NetworkConfiguration": {
"AwsvpcConfiguration": {
"SecurityGroups": [
"sg-xxxxxxx"
],
"Subnets": [
"subnet-xxxxxxx"
]
}
},
"ServiceName": "awsExampleService",
"TaskDefinition": "awsExampleTaskDefinition",
"ServiceRegistries": [
{
"RegistryArn": {
"Fn::GetAtt": [
"DiscoveryService",
"Arn"
]
},
"Port": 80
}
]
}
}
}
YAML:
EcsService:
Type: AWS::ECS::Service
Properties:
Cluster: awsExampleCluster
DesiredCount: 4
LaunchType: FARGATE
NetworkConfiguration:
AwsvpcConfiguration:
SecurityGroups:
- sg-xxxxxxx
Subnets:
- subnet-xxxxxxx
ServiceName: awsExampleService
TaskDefinition: awsExampleTaskDefinition
ServiceRegistries:
- RegistryArn: !GetAtt DiscoveryService.Arn
Port: 80
참고: awsExampleCluster는 클러스터로, awsExampleService는 서비스로, awsExampleTaskDefinition은 태스크 정의로, sg-xxxxxxx 는 보안 그룹 ID로, subnet-xxxxxxx는 서브넷 ID로 바꾸십시오.
RegistryArn 속성의 경우, 해당 값을 Amazon ECS 서비스와 통합하려는 서비스 검색 서비스의 Amazon 리소스 이름(ARN)으로 설정하십시오. 내장 함수 Fn::GetAtt를 사용하여 AWS CloudFormation 템플릿에서 이 값을 가져오십시오.
awsvpc 모드를 사용하는 경우, 그 값을 포트 또는 ContainerPort 및 ContainerName의 조합 중 하나로 설정하십시오(태스크 정의에 지정된 대로). 호스트 또는 브리지 모드를 사용하는 경우, 그 값을 ContainerPort 또는 ContainerName으로 설정하십시오(태스크 정의에 지정된 대로).
4. AWS CloudFormation 콘솔을 연 다음, 스택 생성을 선택하여 업데이트된 템플릿을 기반으로 리소스를 배포하십시오.
5. VPC 내에서 태스크를 해결할 수 있는지 확인하려면 다음 명령을 실행하십시오.
$ dig awsExampleService.awsExampleNamespace. +short
$ dig srv awsExampleService.awsExampleNamespace. +short
$ curl awsExampleService.awsExampleNamespace. -I
참고: awsExampleService의 값은 AWS::ECS::Service 리소스의 이름입니다. awsExampleNamespace의 값은 AWS::ServiceDiscovery::PrivateDnsNamespace 리소스의 이름입니다.
이 명령은 다음과 비슷한 출력을 반환합니다.
$ dig awsExampleService.awsExampleNamespace. +short의 경우:
172.31.182.0
172.31.160.124
172.31.137.81
172.31.149.244
$ dig srv awsExampleService.awsExampleNamespace. +short의 경우:
1 1 80 ffe95d27ea8d4f7aba0dfed87297fc5a.awsExampleService.awsExampleNamespace.
1 1 80 44a17fa781974a93bb563bc1826a8697.awsExampleService.awsExampleNamespace.
1 1 80 d640ecb3d283421bb2d1318caf4b0d66.awsExampleService.awsExampleNamespace.
1 1 80 65aff6fff33144b2ad79d283ab52cfe9.awsExampleService.awsExampleNamespace.
$ curl awsExampleService.awsExampleNamespace. -I의 경우:
HTTP/1.1 200 OK
Server: nginx/1.15.12
Date: Wed, 15 May 2019 02:25:19 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 16 Apr 2019 13:08:19 GMT
Connection: keep-alive
ETag: "5cb5d3c3-264"
Accept-Ranges: bytes