在 Fargate 中使用 Service Connect AWS CLI - Amazon Elastic Container Service

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

在 Fargate 中使用 Service Connect AWS CLI

以下教程展示了如何使用 Service Connect 和 Fargate 任务创建 Amazon ECS 服务。 AWS CLI

Amazon ECS 支持中 AWS 区域 列出的服务连接功能Regions with Service Connect

先决条件

本教程假设以下先决条件已完成:

步骤 1:创建 Amazon ECS 集群

请按照以下步骤创建 Amazon ECS 集群和命名空间。

创建 Amazon ECS 集群和 AWS Cloud Map 命名空间
  1. 创建要使用的名为 tutorial 的 Amazon ECS 集群。参数 --service-connect-defaults 设置集群的默认命名空间。在示例输出中,此账户中service-connect不存在该名称的 AWS Cloud Map 命名空间 AWS 区域,因此该命名空间由 Amazon ECS 创建。命名空间是在账户中的 AWS Cloud Map 中创建的,所有其他命名空间都可见,因此请使用表明目的的名称。

    aws ecs create-cluster --cluster-name tutorial --service-connect-defaults namespace=service-connect

    输出:

    {
        "cluster": {
            "clusterArn": "arn:aws:ecs:us-west-2:123456789012:cluster/tutorial",
            "clusterName": "tutorial",
            "serviceConnectDefaults": {
                "namespace": "arn:aws:servicediscovery:us-west-2:123456789012:namespace/ns-EXAMPLE"
            },
            "status": "PROVISIONING",
            "registeredContainerInstancesCount": 0,
            "runningTasksCount": 0,
            "pendingTasksCount": 0,
            "activeServicesCount": 0,
            "statistics": [],
            "tags": [],
            "settings": [
                {
                    "name": "containerInsights",
                    "value": "disabled"
                }
            ],
            "capacityProviders": [],
            "defaultCapacityProviderStrategy": [],
            "attachments": [
                {
                    "id": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111",
                    "type": "sc",
                    "status": "ATTACHING",
                    "details": []
                }
            ],
            "attachmentsStatus": "UPDATE_IN_PROGRESS"
        }
    }
    }
  2. 验证集群是否已创建:

    aws ecs describe-clusters --clusters tutorial

    输出:

    {
        "clusters": [
            {
                "clusterArn": "arn:aws:ecs:us-west-2:123456789012:cluster/tutorial",
                "clusterName": "tutorial",
                "serviceConnectDefaults": {
                    "namespace": "arn:aws:servicediscovery:us-west-2:123456789012:namespace/ns-EXAMPLE"
                },
                "status": "ACTIVE",
                "registeredContainerInstancesCount": 0,
                "runningTasksCount": 0,
                "pendingTasksCount": 0,
                "activeServicesCount": 0,
                "statistics": [],
                "tags": [],
                "settings": [],
                "capacityProviders": [],
                "defaultCapacityProviderStrategy": []
            }
        ],
        "failures": []
    }
                    
  3. (可选)确认命名空间是在中创建的 AWS Cloud Map。您可以使用中创建的 AWS Management Console 或普通 AWS CLI 配置 AWS Cloud Map。

    例如,使用 AWS CLI:

    aws servicediscovery --region us-west-2 get-namespace --id ns-EXAMPLE

    输出:

    {
        "Namespace": {
            "Id": "ns-EXAMPLE",
            "Arn": "arn:aws:servicediscovery:us-west-2:123456789012:namespace/ns-EXAMPLE",
            "Name": "service-connect",
            "Type": "HTTP",
            "Properties": {
                "DnsProperties": {
                    "SOA": {}
                },
                "HttpProperties": {
                    "HttpName": "service-connect"
                }
            },
            "CreateDate": 1661749852.422,
            "CreatorRequestId": "service-connect"
        }
    }
                    

步骤 2:为服务器创建 Amazon ECS 服务

Service Connect 功能旨在互连 Amazon ECS 上的多个应用程序。这些应用程序中至少有一个需要提供 Web 服务才能连接。在此步骤中,您将创建:

  • 使用未经修改的官方 NGINX 容器映像并包括 Service Connect 配置的任务定义。

  • Amazon ECS 服务定义,用于配置 Service Connect,为该服务的流量提供服务发现和服务网格代理。该配置重复使用集群配置中的默认命名空间,以减少您为每项服务所做的服务配置量。

  • Amazon ECS 服务。它使用任务定义运行一项任务,并为 Service Connect 代理插入一个额外的容器。代理侦听任务定义的容器端口映射中的端口。在 Amazon ECS 中运行的客户端应用程序中,客户端任务中的代理侦听与任务定义端口名、服务发现名称或服务客户端别名以及来自客户端别名的端口号的出站连接。

使用 Amazon ECS Service Connect 创建 Web 服务
  1. 注册与 Fargate 兼容的任务定义并使用 awsvpc 网络模式。按照以下步骤进行操作:

    1. 使用以下任务定义的内容创建名为 service-connect-nginx.json 的文件。

      此任务定义通过向端口映射添加 nameappProtocol 参数来配置 Service Connect。使用多个端口时,端口名称使该端口在服务配置中更易于识别。默认情况下,端口名也用作命名空间中其他应用程序使用的可发现名称。

      任务定义包含任务 IAM 角色,因为该服务已启用 ECS Exec。

      重要

      此任务定义使用来发送 Amaz logConfiguration on Logs 的 nginx 输出stdout和发送stderr到 Amazon Logs。 CloudWatch 此任务执行角色没有创建 CloudWatch 日志组所需的额外权限。使用 AWS Management Console 或在 Lo CloudWatch gs 中创建日志组 AWS CLI。如果您不想将 nginx 日志发送 CloudWatch 到日志,则可以删除。logConfiguration

      将执行角色中的 AWS 账户 ID 替换为您的 AWS 账户 ID。

      { "family": "service-connect-nginx", "executionRoleArn": "arn:aws:iam::123456789012:role/ecsTaskExecutionRole", "taskRoleArn": "arn:aws:iam::123456789012:role/ecsTaskRole", "networkMode": "awsvpc", "containerDefinitions": [ { "name": "webserver", "image": "public.ecr.aws/docker/library/nginx:latest", "cpu": 100, "portMappings": [ { "name": "nginx", "containerPort": 80, "protocol": "tcp", "appProtocol": "http" } ], "essential": true, "logConfiguration": { "logDriver": "awslogs", "options": { "awslogs-group": "/ecs/service-connect-nginx", "awslogs-region": "region", "awslogs-stream-prefix": "nginx" } } } ], "cpu": "256", "memory": "512" }
    2. 使用 service-connect-nginx.json 文件注册任务定义:

      aws ecs register-task-definition --cli-input-json file://service-connect-nginx.json
  2. 按照以下步骤创建 ECS 服务:

    1. 使用您将要创建的 Amazon ECS 服务的内容,创建名为 service-connect-nginx-service.json 的文件。此示例会使用在上一步中创建的任务定义。由于示例任务定义使用 awsvpc 网络模式,awsvpcConfiguration 是必需的。

      创建 ECS 服务时,请指定 Fargate 启动类型和支持 Service Connect 的 LATEST 平台版本。securityGroupssubnets 必须属于符合使用 Amazon ECS 要求的 VPC。您可以从 Amazon VPC 控制台获取安全组和子网 ID。

      此服务通过添加 serviceConnectConfiguration 参数来配置 Service Connect。不需要命名空间,因为集群配置了默认命名空间。在命名空间中的 ECS 中运行的客户端应用程序通过使用 portNameclientAliases 中的端口连接到此服务。例如,可使用 http://nginx:80/ 访问此服务,因为 nginx 在根位置 / 提供了欢迎页面。不在 Amazon ECS 中运行或不在同一命名空间中的外部应用程序可以使用任务的 IP 地址和任务定义中的端口号,通过 Service Connect 代理访问此应用程序。对于您的tls配置,请arn为您awsPcaAuthorityArn、您和roleArn您的 kmsKey IAM 角色添加证书。

      该服务使用将服务连接代理输出从 stdout A logConfiguration mazon Logs 发送或发送stderr到 Amazon CloudWatch Logs。此任务执行角色没有创建 CloudWatch 日志组所需的额外权限。使用 AWS Management Console 或在 Lo CloudWatch gs 中创建日志组 AWS CLI。我们建议您创建此日志组并将代理日志存储在 CloudWatch 日志中。如果您不想将代理日志发送到日 CloudWatch 志,则可以删除logConfiguration

      { "cluster": "tutorial", "deploymentConfiguration": { "maximumPercent": 200, "minimumHealthyPercent": 0 }, "deploymentController": { "type": "ECS" }, "desiredCount": 1, "enableECSManagedTags": true, "enableExecuteCommand": true, "launchType": "FARGATE", "networkConfiguration": { "awsvpcConfiguration": { "assignPublicIp": "ENABLED", "securityGroups": [ "sg-EXAMPLE" ], "subnets": [ "subnet-EXAMPLE", "subnet-EXAMPLE", "subnet-EXAMPLE" ] } }, "platformVersion": "LATEST", "propagateTags": "SERVICE", "serviceName": "service-connect-nginx-service", "serviceConnectConfiguration": { "enabled": true, "services": [ { "portName": "nginx", "clientAliases": [ { "port": 80 } ], "tls": { "issuerCertificateAuthority": { "awsPcaAuthorityArn": "certificateArn" }, "kmsKey": "kmsKey", "roleArn": "iamRoleArn" } } ], "logConfiguration": { "logDriver": "awslogs", "options": { "awslogs-group": "/ecs/service-connect-proxy", "awslogs-region": "region", "awslogs-stream-prefix": "service-connect-proxy" } } }, "taskDefinition": "service-connect-nginx" }
    2. 使用 service-connect-nginx-service.json 文件创建 ECS 服务。

      aws ecs create-service --cluster tutorial --cli-input-json file://service-connect-nginx-service.json

      输出:

      {
          "service": {
              "serviceArn": "arn:aws:ecs:us-west-2:123456789012:service/tutorial/service-connect-nginx-service",
              "serviceName": "service-connect-nginx-service",
              "clusterArn": "arn:aws:ecs:us-west-2:123456789012:cluster/tutorial",
              "loadBalancers": [],
              "serviceRegistries": [],
              "status": "ACTIVE",
              "desiredCount": 1,
              "runningCount": 0,
              "pendingCount": 0,
              "launchType": "FARGATE",
              "platformVersion": "LATEST",
              "platformFamily": "Linux",
              "taskDefinition": "arn:aws:ecs:us-west-2:123456789012:task-definition/service-connect-nginx:1",
              "deploymentConfiguration": {
                  "deploymentCircuitBreaker": {
                      "enable": false,
                      "rollback": false
                  },
                  "maximumPercent": 200,
                  "minimumHealthyPercent": 0
              },
              "deployments": [
                  {
                      "id": "ecs-svc/3763308422771520962",
                      "status": "PRIMARY",
                      "taskDefinition": "arn:aws:ecs:us-west-2:123456789012:task-definition/service-connect-nginx:1",
                      "desiredCount": 1,
                      "pendingCount": 0,
                      "runningCount": 0,
                      "failedTasks": 0,
                      "createdAt": 1661210032.602,
                      "updatedAt": 1661210032.602,
                      "launchType": "FARGATE",
                      "platformVersion": "1.4.0",
                      "platformFamily": "Linux",
                      "networkConfiguration": {
                          "awsvpcConfiguration": {
                              "assignPublicIp": "ENABLED",
                              "securityGroups": [
                                  "sg-EXAMPLE"
                              ],
                              "subnets": [
                                  "subnet-EXAMPLEf",
                                  "subnet-EXAMPLE",
                                  "subnet-EXAMPLE"
                              ]
                          }
                      },
                      "rolloutState": "IN_PROGRESS",
                      "rolloutStateReason": "ECS deployment ecs-svc/3763308422771520962 in progress.",
                      "failedLaunchTaskCount": 0,
                      "replacedTaskCount": 0,
                      "serviceConnectConfiguration": {
                          "enabled": true,
                          "namespace": "service-connect",
                          "services": [
                              {
                                  "portName": "nginx",
                                  "clientAliases": [
                                      {
                                          "port": 80
                                      }
                                  ]
                              }
                          ],
                          "logConfiguration": {
                              "logDriver": "awslogs",
                              "options": {
                                  "awslogs-group": "/ecs/service-connect-proxy",
                                  "awslogs-region": "us-west-2",
                                  "awslogs-stream-prefix": "service-connect-proxy"
                              },
                              "secretOptions": []
                          }
                      },
                      "serviceConnectResources": [
                          {
                              "discoveryName": "nginx",
                              "discoveryArn": "arn:aws:servicediscovery:us-west-2:123456789012:service/srv-EXAMPLE"
                          }
                      ]
                  }
              ],
              "roleArn": "arn:aws:iam::123456789012:role/aws-service-role/ecs.amazonaws.com/AWSServiceRoleForECS",
              "version": 0,
              "events": [],
              "createdAt": 1661210032.602,
              "placementConstraints": [],
              "placementStrategy": [],
              "networkConfiguration": {
                  "awsvpcConfiguration": {
                      "assignPublicIp": "ENABLED",
                      "securityGroups": [
                          "sg-EXAMPLE"
                      ],
                      "subnets": [
                          "subnet-EXAMPLE",
                          "subnet-EXAMPLE",
                          "subnet-EXAMPLE"
                      ]
                  }
              },
              "schedulingStrategy": "REPLICA",
              "enableECSManagedTags": true,
              "propagateTags": "SERVICE",
              "enableExecuteCommand": true
          }
      }
                              

      您提供的 serviceConnectConfiguration 出现在输出的第一个部署中。当您以需要更改任务的方式更改 ECS 服务时,Amazon ECS 会创建新的部署。

步骤 3:验证是否可以连接

要验证 Service Connect 是否已配置并正常运行,请按照以下步骤从外部应用程序连接到 Web 服务。然后,查看中由 Servic CloudWatch e Connect 代理创建的其他指标。

从外部应用程序连接到 Web 服务
  • 使用任务 IP 地址连接到任务 IP 地址和容器端口

    使用 AWS CLI 获取任务 ID,使用aws ecs list-tasks --cluster tutorial

    如果您的子网和安全组允许来自任务定义的端口上的公共互联网流量,则可以从计算机连接到公有 IP。但是,“describe-tasks” 中没有公有 IP,因此这些步骤包括前往 Amazon EC2 AWS Management Console 或 AWS CLI 获取弹性网络接口的详细信息。

    在此示例中,同一 VPC 中的 Amazon EC2 实例使用任务的私有 IP。应用程序是 nginx,但 server: envoy 标头显示使用了 Service Connect 代理。Service Connect 代理正在从任务定义侦听容器端口。

    $ curl -v 10.0.19.50:80/
    *   Trying 10.0.19.50:80...
    * Connected to 10.0.19.50 (10.0.19.50) port 80 (#0)
    > GET / HTTP/1.1
    > Host: 10.0.19.50
    > User-Agent: curl/7.79.1
    > Accept: */*
    >
    * Mark bundle as not supporting multiuse
    < HTTP/1.1 200 OK
    < server: envoy
    < date: Tue, 23 Aug 2022 03:53:06 GMT
    < content-type: text/html
    < content-length: 612
    < last-modified: Tue, 16 Apr 2019 13:08:19 GMT
    < etag: "5cb5d3c3-264"
    < accept-ranges: bytes
    < x-envoy-upstream-service-time: 0
    <
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
        body {
            width: 35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to nginx!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html> 
                    
查看 Service Connect 指标

Service Connect 代理在指标中创建应用程序(HTTP、HTTP2、gRPC 或 TCP 连接)指标。 CloudWatch 使用 CloudWatch 控制台时,可以查看 ECS 命名空间下的DiscoveryNameDiscoveryName ServiceName、 (、TargetDiscoveryName、 ClusterName) 和 (TargetDiscoveryName, ServiceName, ClusterName) 的其他指标维度。有关这些指标与维度的更多信息,请参阅 Amazon ECS 的可用指标和维度