本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
教程:使用 Amazon ECS CLI 创建使用服务发现的 Amazon ECS 服务
本教程介绍创建配置为使用服务发现的 Amazon ECS 服务的简单演练。许多服务发现配置值可使用 ECS 参数文件或标志进行指定。在使用标志时,它们优先于 ECS 参数文件(如果同时存在)。在使用 Amazon ECS CLI 时,复合项目名称用作 ECS 服务的名称。
先决条件
在继续本教程前,您应当已经满足下列先决条件:
-
设置 AWS 账户。
-
安装 Amazon ECS CLI。有关更多信息,请参阅安装 Amazon ECS CLI。
配置 Amazon ECS CLI
您必须先安装并配置 Amazon ECS CLI,然后才能启动此教程。有关更多信息,请参阅安装 Amazon ECS CLI。
Amazon ECS CLI 需要获得凭证才能代表您发出 API 请求。它可以从环境变量、AWS 配置文件或 Amazon ECS 配置文件中提取凭证。有关更多信息,请参阅 配置 Amazon ECS CLI。
创建 Amazon ECS CLI 配置
-
创建集群配置:
ecs-cli configure --cluster
ec2-tutorial
--regionus-east-1
--default-launch-type EC2 --config-nameec2-tutorial
-
使用您的访问密钥和私有密钥创建配置文件:
ecs-cli configure profile --access-key
AWS_ACCESS_KEY_ID
--secret-keyAWS_SECRET_ACCESS_KEY
--profile-nameec2-tutorial
注意 如果这是您首次配置 Amazon ECS CLI,则这些配置将被标记为默认值。如果这不是您首次配置 Amazon ECS CLI,请参阅 Amazon Elastic Container Service Developer Guide 中的 Amazon ECS 命令行参考,以将其设置为默认配置和配置文件。
创建一个配置为使用服务发现的 Amazon ECS 服务
按照以下步骤,使用 Amazon ECS CLI 创建配置为使用服务发现的 Amazon ECS 服务。
创建一个配置为使用服务发现的 Amazon ECS 服务
-
创建一个名为
backend
的 Amazon ECS 服务并在 VPC 中创建一个名为tutorial
的私有 DNS 命名空间。在此示例中,任务使用awsvpc
网络模式,因此无需container_name
和container_port
值。ecs-cli compose --project-name
backend
service up --private-dns-namespacetutorial
--vpcvpc-04deee8176dce7d7d
--enable-service-discovery输出:
INFO[0001] Using ECS task definition TaskDefinition="backend:1" INFO[0002] Waiting for the private DNS namespace to be created... INFO[0002] Cloudformation stack status stackStatus=CREATE_IN_PROGRESS WARN[0033] Defaulting DNS Type to A because network mode was awsvpc INFO[0033] Waiting for the Service Discovery Service to be created... INFO[0034] Cloudformation stack status stackStatus=CREATE_IN_PROGRESS INFO[0065] Created an ECS service service=backend taskDefinition="backend:1" INFO[0066] Updated ECS service successfully desiredCount=1 serviceName=backend INFO[0081] (service backend) has started 1 tasks: (task 824b5a76-8f9c-4beb-a64b-6904e320630e). timestamp="2018-09-12 00:00:26 +0000 UTC" INFO[0157] Service status desiredCount=1 runningCount=1 serviceName=backend INFO[0157] ECS Service has reached a stable state desiredCount=1 runningCount=1 serviceName=backend
-
在同一私有 DNS 命名空间中创建另一个名为
frontend
的服务。由于此命名空间已存在,Amazon ECS CLI 会使用它而不是创建一个新的。ecs-cli compose --project-name
frontend
service up --private-dns-namespacetutorial
--vpcvpc-04deee8176dce7d7d
--enable-service-discovery输出:
INFO[0001] Using ECS task definition TaskDefinition="frontend:1" INFO[0002] Using existing namespace ns-kvhnzhb5vxplfmls WARN[0033] Defaulting DNS Type to A because network mode was awsvpc INFO[0033] Waiting for the Service Discovery Service to be created... INFO[0034] Cloudformation stack status stackStatus=CREATE_IN_PROGRESS INFO[0065] Created an ECS service service=frontend taskDefinition="frontend:1" INFO[0066] Updated ECS service successfully desiredCount=1 serviceName=frontend INFO[0081] (service frontend) has started 1 tasks: (task 824b5a76-8f9c-4beb-a64b-6904e320630e). timestamp="2018-09-12 00:00:26 +0000 UTC" INFO[0157] Service status desiredCount=1 runningCount=1 serviceName=frontend INFO[0157] ECS Service has reached a stable state desiredCount=1 runningCount=1 serviceName=frontend
-
验证这两个服务是否能够在 VPC 中使用 DNS 发现对方。DNS 主机名使用以下格式:
<service_discovery_service_name>.<service_discovery_namespace>
。在此示例中,frontend
服务可在frontend.tutorial
处发现,backend
服务可在backend.tutorial
处发现。由于这些是私有 DNS 命名空间,这些 DNS 名称仅在指定的 VPC 中时可解析。 -
要更新服务发现设置,请更新
frontend
服务的设置。可更新的值是 DNS TTL 和运行状况检查自定义配置失败阈值的值。ecs-cli compose --project-name
frontend
service up --update-service-discovery --dns-typeSRV
--dns-ttl120
--healthcheck-custom-config-failure-threshold2
输出:
INFO[0001] Using ECS task definition TaskDefinition="frontend:1" INFO[0001] Updated ECS service successfully desiredCount=1 serviceName=frontend INFO[0001] Service status desiredCount=1 runningCount=1 serviceName=frontend INFO[0001] ECS Service has reached a stable state desiredCount=1 runningCount=1 serviceName=frontend INFO[0002] Waiting for your Service Discovery resources to be updated... INFO[0002] Cloudformation stack status stackStatus=UPDATE_IN_PROGRESS
-
要清除,请删除 Amazon ECS 服务和服务发现资源。在删除
frontend
服务后,Amazon ECS CLI 会自动删除关联的服务发现服务。ecs-cli compose --project-name
frontend
service rmINFO[0000] Updated ECS service successfully desiredCount=0 serviceName=frontend INFO[0001] Service status desiredCount=0 runningCount=1 serviceName=frontend INFO[0016] Service status desiredCount=0 runningCount=0 serviceName=frontend INFO[0016] (service frontend) has stopped 1 running tasks: (task 824b5a76-8f9c-4beb-a64b-6904e320630e). timestamp="2018-09-12 00:37:25 +0000 UTC" INFO[0016] ECS Service has reached a stable state desiredCount=0 runningCount=0 serviceName=frontend INFO[0016] Deleted ECS service service=frontend INFO[0016] ECS Service has reached a stable state desiredCount=0 runningCount=0 serviceName=frontend INFO[0027] Waiting for your Service Discovery Service resource to be deleted... INFO[0027] Cloudformation stack status stackStatus=DELETE_IN_PROGRESS
-
要完成清除,请删除
backend
服务以及使用它创建的私有 DNS 命名空间。Amazon ECS CLI 会将私有 DNS 命名空间的 AWS CloudFormation 堆栈与为其创建该堆栈的 Amazon ECS 服务相关联。在删除服务时,也会删除命名空间。ecs-cli compose --project-name
backend
service rm --delete-namespace