Amazon Elastic Container Service
开发人员指南 (API 版本 2014-11-13)
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 Amazon AWS 入门

教程:使用 AWS CLI 创建包含 Fargate 任务的群集

以下步骤将帮助您在 Amazon ECS 中使用 AWS CLI 设置集群、注册任务定义、运行任务和执行其他常见方案。请确保您使用的是最新版本的 AWS CLI。有关如何升级到最新版本的更多信息,请参阅安装 AWS 命令行界面

先决条件

第 1 步:(可选)创建集群

第 2 步:注册任务定义

第 3 部:列出任务定义

步骤 4:创建服务

步骤 5:列出服务

步骤 6:描述正在运行的服务

先决条件

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

第 1 步:(可选)创建集群

默认情况下,您的账户会收到一个 default 群集。

注意

使用为您提供的 default 群集的好处是您不必在后续命令中指定 --cluster cluster_name 选项。如果您自行创建非默认群集,您需要为您打算用于该群集的每个命令指定 --cluster cluster_name

使用以下命令自行创建具有唯一名称的群集:

aws ecs create-cluster --cluster-name fargate-cluster

输出:

{ "cluster": { "status": "ACTIVE", "statistics": [], "clusterName": "fargate-cluster", "registeredContainerInstancesCount": 0, "pendingTasksCount": 0, "runningTasksCount": 0, "activeServicesCount": 0, "clusterArn": "arn:aws:ecs:region:aws_account_id:cluster/fargate-cluster" } }

第 2 步:注册任务定义

您必须先注册任务定义才能在 ECS 集群上运行任务。任务定义是分组在一起的一系列容器。下面的示例是创建 PHP Web 应用程序的一个简单任务定义。有关可用任务定义参数的更多信息,请参阅 Amazon ECS 任务定义

{ "family": "sample-fargate", "networkMode": "awsvpc", "containerDefinitions": [ { "name": "fargate-app", "image": "httpd:2.4", "portMappings": [ { "containerPort": 80, "hostPort": 80, "protocol": "tcp" } ], "essential": true, "entryPoint": [ "sh", "-c" ], "command": [ "/bin/sh -c \"echo '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p> </div></body></html>' > /usr/local/apache2/htdocs/index.html && httpd-foreground\"" ] } ], "requiresCompatibilities": [ "FARGATE" ], "cpu": "256", "memory": "512" }

上述示例 JSON 可通过两种方式传递到 AWS CLI:您可以将任务定义 JSON 保存为文件并使用 --cli-input-json file://path_to_file.json 选项传递它,也可以对 JSON 中的引号进行转义并在命令行上传递 JSON 容器定义,如以下示例中所示。如果您选择在命令行上传递容器定义,您的命令还需要一个 --family 参数,该参数用于使任务定义的多个版本保持互相关联。

将 JSON 文件用于容器定义:

aws ecs register-task-definition --cli-input-json file://$HOME/tasks/fargate-task.json

register-task-definition 将在其完成注册后返回任务定义的说明。

{ "taskDefinition": { "status": "ACTIVE", "networkMode": "awsvpc", "family": "sample-fargate", "placementConstraints": [], "requiresAttributes": [ { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.18" }, { "name": "ecs.capability.task-eni" } ], "cpu": "256", "compatibilities": [ "EC2", "FARGATE" ], "volumes": [], "memory": "512", "requiresCompatibilities": [ "FARGATE" ], "taskDefinitionArn": "arn:aws:ecs:region:aws_account_id:task-definition/sample-fargate:2", "containerDefinitions": [ { "environment": [], "name": "fargate-app", "mountPoints": [], "image": "httpd:2.4", "cpu": 0, "portMappings": [ { "protocol": "tcp", "containerPort": 80, "hostPort": 80 } ], "entryPoint": [ "sh", "-c" ], "command": [ "/bin/sh -c \"echo '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p> </div></body></html>' > /usr/local/apache2/htdocs/index.html && httpd-foreground\"" ], "essential": true, "volumesFrom": [] } ], "revision": 2 } }

第 3 部:列出任务定义

您可以随时使用 list-task-definitions 命令列出您的账户的任务定义。此命令的输出将显示 familyrevision 值,您可以在调用 run-taskstart-task 时将这些值一起使用。

aws ecs list-task-definitions

输出:

{ "taskDefinitionArns": [ "arn:aws:ecs:region:aws_account_id:task-definition/sample-fargate:1", "arn:aws:ecs:region:aws_account_id:task-definition/sample-fargate:2" ] }

步骤 4:创建服务

为账户注册任务后,您可以为群集中已注册的任务创建服务。在本示例中,我们将创建一个服务,其中至少有 2 个 sample-fargate:1 任务定义实例将在您的群集中运行。

aws ecs create-service --cluster fargate-cluster --service-name fargate-service --task-definition sample-fargate:1 --desired-count 2 --launch-type "FARGATE" --network-configuration "awsvpcConfiguration={subnets=[subnet-abcd1234],securityGroups=[sg-abcd1234]}"

输出:

{ "service": { "status": "ACTIVE", "taskDefinition": "arn:aws:ecs:region:aws_account_id:task-definition/sample-fargate:1", "pendingCount": 0, "launchType": "FARGATE", "loadBalancers": [], "roleArn": "arn:aws:iam::aws_account_id:role/aws-service-role/ecs.amazonaws.com/AWSServiceRoleForECS", "placementConstraints": [], "createdAt": 1510811361.128, "desiredCount": 2, "networkConfiguration": { "awsvpcConfiguration": { "subnets": [ "subnet-abcd1234" ], "securityGroups": [ "sg-abcd1234" ], "assignPublicIp": "DISABLED" } }, "platformVersion": "LATEST", "serviceName": "fargate-service", "clusterArn": "arn:aws:ecs:region:aws_account_id:cluster/fargate-cluster", "serviceArn": "arn:aws:ecs:region:aws_account_id:service/fargate-service", "deploymentConfiguration": { "maximumPercent": 200, "minimumHealthyPercent": 100 }, "deployments": [ { "status": "PRIMARY", "networkConfiguration": { "awsvpcConfiguration": { "subnets": [ "subnet-abcd1234" ], "securityGroups": [ "sg-abcd1234" ], "assignPublicIp": "DISABLED" } }, "pendingCount": 0, "launchType": "FARGATE", "createdAt": 1510811361.128, "desiredCount": 2, "taskDefinition": "arn:aws:ecs:region:aws_account_id:task-definition/sample-fargate:1", "updatedAt": 1510811361.128, "platformVersion": "0.0.1", "id": "ecs-svc/9223370526043414679", "runningCount": 0 } ], "events": [], "runningCount": 0, "placementStrategy": [] } }

步骤 5:列出服务

列出您的群集的服务。您应看到您在上一部分中创建的服务。您可以选取从此命令返回的服务名称或完整 ARN 并在稍后将其用于描述服务。

aws ecs list-services --cluster fargate-cluster

输出:

{ "serviceArns": [ "arn:aws:ecs:region:aws_account_id:service/fargate-service" ] }

步骤 6:描述正在运行的服务

使用之前检索到的服务名称描述服务,以获取有关任务的更多信息。

aws ecs describe-services --cluster fargate-cluster --services fargate-service

输出:

{ "services": [ { "status": "ACTIVE", "taskDefinition": "arn:aws:ecs:region:aws_account_id:task-definition/sample-fargate:1", "pendingCount": 2, "launchType": "FARGATE", "loadBalancers": [], "roleArn": "arn:aws:iam::aws_account_id:role/aws-service-role/ecs.amazonaws.com/AWSServiceRoleForECS", "placementConstraints": [], "createdAt": 1510811361.128, "desiredCount": 2, "networkConfiguration": { "awsvpcConfiguration": { "subnets": [ "subnet-abcd1234" ], "securityGroups": [ "sg-abcd1234" ], "assignPublicIp": "DISABLED" } }, "platformVersion": "LATEST", "serviceName": "fargate-service", "clusterArn": "arn:aws:ecs:region:aws_account_id:cluster/fargate-cluster", "serviceArn": "arn:aws:ecs:region:aws_account_id:service/fargate-service", "deploymentConfiguration": { "maximumPercent": 200, "minimumHealthyPercent": 100 }, "deployments": [ { "status": "PRIMARY", "networkConfiguration": { "awsvpcConfiguration": { "subnets": [ "subnet-abcd1234" ], "securityGroups": [ "sg-abcd1234" ], "assignPublicIp": "DISABLED" } }, "pendingCount": 2, "launchType": "FARGATE", "createdAt": 1510811361.128, "desiredCount": 2, "taskDefinition": "arn:aws:ecs:region:aws_account_id:task-definition/sample-fargate:1", "updatedAt": 1510811361.128, "platformVersion": "0.0.1", "id": "ecs-svc/9223370526043414679", "runningCount": 0 } ], "events": [ { "message": "(service fargate-service) has started 2 tasks: (task 53c0de40-ea3b-489f-a352-623bf1235f08) (task d0aec985-901b-488f-9fb4-61b991b332a3).", "id": "92b8443e-67fb-4886-880c-07e73383ea83", "createdAt": 1510811841.408 }, { "message": "(service fargate-service) has started 2 tasks: (task b4911bee-7203-4113-99d4-e89ba457c626) (task cc5853e3-6e2d-4678-8312-74f8a7d76474).", "id": "d85c6ec6-a693-43b3-904a-a997e1fc844d", "createdAt": 1510811601.938 }, { "message": "(service fargate-service) has started 2 tasks: (task cba86182-52bf-42d7-9df8-b744699e6cfc) (task f4c1ad74-a5c6-4620-90cf-2aff118df5fc).", "id": "095703e1-0ca3-4379-a7c8-c0f1b8b95ace", "createdAt": 1510811364.691 } ], "runningCount": 0, "placementStrategy": [] } ], "failures": [] }