如何搭配 CloudFormation 使用 Amazon ECS 服務探索?

3 分的閱讀內容
0

我想搭配 AWS CloudFormation 使用 Amazon Elastic Container Service (Amazon ECS) 服務探索。

簡短說明

Amazon ECS 主控台可讓您在服務建立期間,建立服務探索資源。不過,CloudFormation 的 AWS::ECS::Service 資源類型不允許您在服務建立期間,建立服務探索資源。

**注意事項:**Amazon ECS 主控台使用整合 API CreatePrivateDnsNamespaceCreateService (AWS Cloud Map) 和 CreateService (Amazon ECS)。在 CloudFormation 中,您可以使用資源類型 AWS::ServiceDiscovery::PrivateDnsNamespaceAWS::ServiceDiscovery::ServiceAWS::ECS::Service,在單一的 CloudFormation 範本中實作整合。

解決方案

1.    在 CloudFormation 範本的 Resources 區段中,建立私有服務探索命名空間,例如 example.com。在現有的 Amazon Virtual Private Cloud (Amazon VPC) 中建立命名空間。請參閱下列 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,建立服務探索服務。

您可以使用 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 Route 53 中自動建立的 DNS 記錄。當您的 Amazon ECS 任務向服務探索服務註冊時,就會建立這些 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 Resource Name (ARN)。使用內部函數 Fn::GetAtt,從 CloudFormation 範本取得此值。

如果您使用 awsvpc 模式,請將值設為 PortContainerPortContainerName 的組合 (如任務定義中所指定)。如果您使用 hostbridge 模式,請將值設為 ContainerPortContainerName (如任務定義中所指定)。

4.    開啟 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

AWS 官方
AWS 官方已更新 2 年前