教程:通过 AWS CLI 使用集群 Auto Scaling - Amazon Elastic Container Service
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

教程:通过 AWS CLI 使用集群 Auto Scaling

Amazon ECS 集群 Auto Scaling 可以使用 AWS 管理控制台、AWS CLI 或 Amazon ECS API 进行设置和配置。

本教程分步演示了使用 AWS CLI 创建用于集群 Auto Scaling 的资源。如果资源需要名称,我们将使用前缀 CLItutorial 来确保它们都具有唯一的名称,并使它们易于找到。

有关 AWS 管理控制台 教程,请参阅教程:通过 AWS 管理控制台 使用集群 Auto Scaling

先决条件

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

步骤 1:创建 Auto Scaling 资源

此步骤将引导您完成创建 Auto Scaling 启动配置和两个 Auto Scaling 组。此步骤要求您已创建 VPC 以及至少一个公有子网和一个安全组。有关更多信息,请参阅教程:为您的集群创建带有公有和私有子网的 VPC

创建 Auto Scaling 资源

  1. 按照以下步骤创建 Auto Scaling 启动配置。有关更多信息,请参阅 Amazon EC2 Auto Scaling 用户指南 中的启动配置

    1. 使用以下内容创建名为 CLItutorial-launchconfig.json 的文件。您必须替换以下值:

      • ImageId 替换为最新 Amazon Linux 2 Amazon ECS 优化 AMI。有关更多信息,请参阅Amazon ECS-optimized AMI

      • SecurityGroups 值替换为与 VPC 关联的安全组 ID。

      • IamInstanceProfile 值替换为 Amazon ECS 容器实例 IAM 角色的实例配置文件的完整 Amazon 资源名称 (ARN)。当实例启动时,实例配置文件使您可以将 IAM 角色信息传递给 Amazon EC2 实例。如果您的 Amazon ECS 容器实例 IAM 角色已创建,您可以使用以下命令检索实例配置文件的 ARN。将此示例中的容器实例 IAM 角色名称替换为容器实例 IAM 角色的名称。

        aws iam list-instance-profiles-for-role --role-name ecsInstanceRole
      { "LaunchConfigurationName": "CLItutorial-launchconfig", "ImageId": "ami-04240723d51aeeb2d", "SecurityGroups": [ "sg-abcd1234" ], "InstanceType": "t2.micro", "BlockDeviceMappings": [ { "DeviceName": "/dev/xvdcz", "Ebs": { "VolumeSize": 22, "VolumeType": "gp2", "DeleteOnTermination": true, "Encrypted": true } } ], "InstanceMonitoring": { "Enabled": false }, "IamInstanceProfile": "arn:aws:iam::111122223333:instance-profile/ecsInstanceRole", "AssociatePublicIpAddress": true }
    2. 使用以下内容创建名为 CLItutorial-userdata.txt 的文件。此用户数据脚本用于向本教程中使用的 Amazon ECS 集群(我们已命名为 CLItutorial-cluster)注册由 Auto Scaling 组创建的 Amazon EC2 实例。

      #!/bin/bash echo ECS_CLUSTER=CLItutorial-cluster >> /etc/ecs/ecs.config
    3. 创建 Auto Scaling 启动配置。

      aws autoscaling create-launch-configuration --cli-input-json file://CLItutorial-launchconfig.json --user-data file://CLItutorial-userdata.txt --region us-west-2

      如果命令成功,则无任何输出。使用以下命令显示启动配置的详细信息。

      aws autoscaling describe-launch-configurations --launch-configuration-names CLItutorial-launchconfig --region us-west-2
  2. 按照以下步骤创建 Auto Scaling 组。有关更多信息,请参阅 Amazon EC2 Auto Scaling 用户指南 中的 Auto Scaling 组

    1. 使用以下内容创建名为 CLItutorial-asgconfig.json 的文件。您必须替换以下值:

      • AvailabilityZones 值替换为您的子网所在的可用区。

      • VPCZoneIdentifier 值替换为 VPC 中子网的 ID。

      • ServiceLinkedRoleARN 值替换为 Auto Scaling 服务相关 IAM 角色的完整 Amazon 资源名称 (ARN)。有关更多信息,请参阅 Amazon EC2 Auto Scaling 用户指南 中的 Amazon EC2Auto Scaling 的服务相关角色

      重要

      使用 Amazon ECS 托管调整功能进行集群自动调整时,需要为 NewInstancesProtectedFromScaleIn 值指定 true。本教程在后面的步骤中演示使用 Amazon ECS 托管调整功能进行集群自动调整。

      { "LaunchConfigurationName": "CLItutorial-launchconfig", "MinSize": 0, "MaxSize": 100, "DesiredCapacity": 0, "DefaultCooldown": 300, "AvailabilityZones": [ "us-west-2c" ], "HealthCheckType": "EC2", "HealthCheckGracePeriod": 300, "VPCZoneIdentifier": "subnet-abcd1234", "TerminationPolicies": [ "DEFAULT" ], "NewInstancesProtectedFromScaleIn": true, "ServiceLinkedRoleARN": "arn:aws:iam::111122223333:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling" }
    2. 创建 Auto Scaling 组。

      aws autoscaling create-auto-scaling-group --auto-scaling-group-name CLItutorial-asg --cli-input-json file://CLItutorial-asgconfig.json --region us-west-2

      如果命令成功,则无任何输出。

    3. 要创建第二个调整组,请使用不同的 Auto Scaling 组名重复执行相同的命令。

      aws autoscaling create-auto-scaling-group --auto-scaling-group-name CLItutorial-asg-burst --cli-input-json file://CLItutorial-asgconfig.json --region us-west-2

      如果命令成功,则无任何输出。

    4. 检索刚刚创建的两个 Auto Scaling 组的详细信息。

      aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names CLItutorial-asg CLItutorial-asg-burst --region us-west-2

      输出将显示两个 Auto Scaling 组的完整 Amazon 资源名称 (ARN),您在下一步中将需要这些名称。

      {
          "AutoScalingGroups": [
              {
                  "AutoScalingGroupName": "CLItutorial-asg",
                  "AutoScalingGroupARN": "arn:aws:autoscaling:us-west-2:111122223333:autoScalingGroup:24c44d96-606a-427f-826a-f64ba4cc918c:autoScalingGroupName/CLItutorial-asg",
                  "LaunchConfigurationName": "CLItutorial-launchconfig",
                  ...
              },
              {
                  "AutoScalingGroupName": "CLItutorial-asg-burst",
                  "AutoScalingGroupARN": "arn:aws:autoscaling:us-west-2:111122223333:autoScalingGroup:407c3102-fb00-4a0c-a1a8-0b242203a262:autoScalingGroupName/CLItutorial-asg-burst",
                  "LaunchConfigurationName": "CLItutorial-launchconfig",
                  ...
              }
          ]
      }

步骤 2:创建 Amazon ECS 资源

此步骤将引导您完成创建两个 Amazon ECS 容量提供程序和一个 Amazon ECS 集群。您可以将一个 Auto Scaling 组与各个容量提供程序关联。本教程使用 us-west-2 区域。

创建 Amazon ECS 资源

  1. 按照以下步骤创建 Amazon ECS 容量提供程序。

    1. 使用以下内容创建名为 CLItutorial-capacityprovider.json 的文件。将 autoScalingGroupArn 值替换为您在步骤 1 中创建的第一个 Auto Scaling 组的完整 Amazon 资源名称 (ARN)。

      { "name": "CLItutorial-capacityprovider", "autoScalingGroupProvider": { "autoScalingGroupArn": "arn:aws:autoscaling:us-west-2:111122223333:autoScalingGroup:24c44d96-606a-427f-826a-f64ba4cc918c:autoScalingGroupName/CLItutorial-asg", "managedScaling": { "status": "ENABLED", "targetCapacity": 100, "minimumScalingStepSize": 1, "maximumScalingStepSize": 100 }, "managedTerminationProtection": "ENABLED" } }
    2. 创建容量提供程序。

      aws ecs create-capacity-provider --cli-input-json file://CLItutorial-capacityprovider.json --region us-west-2

      输出返回容量提供程序的描述。

      {
          "capacityProvider": {
              "capacityProviderArn": "arn:aws:ecs:us-west-2:111122223333:capacity-provider/CLItutorial-capacityprovider/1a484097-270b-45ea-be85-592924EXAMPLE",
              "name": "CLItutorial-capacityprovider",
              "status": "ACTIVE",
              "autoScalingGroupProvider": {
                  "autoScalingGroupArn": "arn:aws:autoscaling:us-west-2:111122223333:autoScalingGroup:24c44d96-606a-427f-826a-f64ba4cc918c:autoScalingGroupName/CLItutorial-asg",
                  "managedScaling": {
                      "status": "ENABLED",
                      "targetCapacity": 100,
                      "minimumScalingStepSize": 1,
                      "maximumScalingStepSize": 100
                  },
                  "managedTerminationProtection": "ENABLED"
              },
              "tags": []
          }
      }
  2. 按照以下步骤创建第二个 Amazon ECS 容量提供程序。第二个容量提供程序用于为集群提供突增容量。在生产中,您可以使用 Amazon EC2 Spot 实例,但在本教程中,我们将使用个按需实例。

    1. 使用以下内容创建名为 CLItutorial-capacityprovider-burst.json 的文件。将 autoScalingGroupArn 值替换为您在步骤 1 中创建的第二个 Auto Scaling 组的完整 Amazon 资源名称 (ARN)。

      { "name": "CLItutorial-capacityprovider-burst", "autoScalingGroupProvider": { "autoScalingGroupArn": "arn:aws:autoscaling:us-west-2:111122223333:autoScalingGroup:407c3102-fb00-4a0c-a1a8-0b242203a262:autoScalingGroupName/CLItutorial-asg-burst", "managedScaling": { "status": "ENABLED", "targetCapacity": 100, "minimumScalingStepSize": 1, "maximumScalingStepSize": 100 }, "managedTerminationProtection": "ENABLED" } }
    2. 创建容量提供程序。

      aws ecs create-capacity-provider --cli-input-json file://CLItutorial-capacityprovider-burst.json --region us-west-2

      输出返回容量提供程序的描述。

      {
          "capacityProvider": {
              "capacityProviderArn": "arn:aws:ecs:us-west-2:111122223333:capacity-provider/CLItutorial-capacityprovider-burst/5e4344097-270b-78ea-be85-592924EXAMPLE",
              "name": "CLItutorial-capacityprovider-burst",
              "status": "ACTIVE",
              "autoScalingGroupProvider": {
                  "autoScalingGroupArn": "arn:aws:autoscaling:us-west-2:111122223333:autoScalingGroup:407c3102-fb00-4a0c-a1a8-0b242203a262:autoScalingGroupName/CLItutorial-asg-burst",
                  "managedScaling": {
                      "status": "ENABLED",
                      "targetCapacity": 100,
                      "minimumScalingStepSize": 1,
                      "maximumScalingStepSize": 100
                  },
                  "managedTerminationProtection": "ENABLED"
              },
              "tags": []
          }
      }
  3. 创建 Amazon ECS 集群。集群名称必须与您在本教程步骤 1 创建的 Auto Scaling 启动配置中指定的用户数据脚本中指定的名称相匹配。我们在上一步中创建的容量提供程序将与此集群关联。

    运行任务或创建服务时,您可为任务指定要使用的容量提供程序策略。同样,可为集群指定默认容量提供程序策略。这使您能够运行任务和创建服务,而无需指定容量提供程序策略,因为这些任务和操作将使用集群的默认容量提供程序策略。指定默认容量提供程序策略时,您可以选择同时指定基数值和权重值。当您将多个容量提供程序与集群关联时,这些值非常有用。有关更多信息,请参阅 容量提供程序概念

    aws ecs create-cluster --cluster-name CLItutorial-cluster --capacity-providers CLItutorial-capacityprovider CLItutorial-capacityprovider-burst --default-capacity-provider-strategy capacityProvider=CLItutorial-capacityprovider,weight=1 capacityProvider=CLItutorial-capacityprovider-burst,weight=1 --region us-west-2

    输出返回集群的描述,包括集群状态和集群附件详细信息。描述显示 Amazon ECS 为您创建的 AWS Auto Scaling 调整计划。为每个容量提供程序创建一个调整计划。

    {
        "cluster": {
            "clusterArn": "arn:aws:ecs:us-west-2:111122223333:cluster/CLItutorial-cluster",
            "clusterName": "CLItutorial-cluster",
            "status": "PROVISIONING",
            "registeredContainerInstancesCount": 0,
            "runningTasksCount": 0,
            "pendingTasksCount": 0,
            "activeServicesCount": 0,
            "statistics": [],
            "tags": [],
            "settings": [
                {
                    "name": "containerInsights",
                    "value": "disabled"
                }
            ],
            "capacityProviders": [
                "CLItutorial-capacityprovider",
                "CLItutorial-capacityprovider-burst"
            ],
            "defaultCapacityProviderStrategy": [
                {
                    "capacityProvider": "CLItutorial-capacityprovider",
                    "weight": 1,
                    "base": 0
                },
                {
                    "capacityProvider": "CLItutorial-capacityprovider-burst",
                    "weight": 1,
                    "base": 0
                }
            ],
            "attachments": [
                {
                    "id": "4aaee2ac-2a66-457c-b0df-a0bc871f5ead",
                    "type": "asp",
                    "status": "PRECREATED",
                    "details": [
                        {
                            "name": "capacityProviderName",
                            "value": "CLItutorial-capacityprovider"
                        },
                        {
                            "name": "scalingPlanName",
                            "value": "ECSManagedAutoScalingPlan-27eb1e2a-5698-4ae7-b382-1553b8ba1095"
                        }
                    ]
                },
                {
                    "id": "03e99543-935d-4ea2-9a96-4b9dd63d320f",
                    "type": "asp",
                    "status": "PRECREATED",
                    "details": [
                        {
                            "name": "capacityProviderName",
                            "value": "CLItutorial-capacityprovider-burst"
                        },
                        {
                            "name": "scalingPlanName",
                            "value": "ECSManagedAutoScalingPlan-f9ea310b-680e-4654-b8c6-1c4862b29a77"
                        }
                    ]
                }
            ],
            "attachmentsStatus": "UPDATE_IN_PROGRESS"
        }
    }
  4. 在继续执行下一步之前,您必须确保集群处于 ACTIVE 状态,每个集群附件都处于 CREATED 状态,并且附件状态处于 UPDATE_COMPLETE 状态。这可以通过描述集群来完成。

    aws ecs describe-clusters --clusters CLItutorial-cluster --include ATTACHMENTS --region us-west-2

    输出将返回集群的描述。验证集群和附件状态字段。

    {
        "cluster": {
            "clusterArn": "arn:aws:ecs:us-west-2:111122223333:cluster/CLItutorial-cluster",
            "clusterName": "CLItutorial-cluster",
            "status": "ACTIVE",
            "registeredContainerInstancesCount": 0,
            "runningTasksCount": 0,
            "pendingTasksCount": 0,
            "activeServicesCount": 0,
            "statistics": [],
            "tags": [],
            "settings": [
                {
                    "name": "containerInsights",
                    "value": "disabled"
                }
            ],
            "capacityProviders": [
                "CLItutorial-capacityprovider",
                "CLItutorial-capacityprovider-burst"
            ],
            "defaultCapacityProviderStrategy": [
                {
                    "capacityProvider": "CLItutorial-capacityprovider",
                    "weight": 1,
                    "base": 0
                },
                {
                    "capacityProvider": "CLItutorial-capacityprovider-burst",
                    "weight": 1,
                    "base": 0
                }
            ],
            "attachments": [
                {
                    "id": "4aaee2ac-2a66-457c-b0df-a0bc871f5ead",
                    "type": "asp",
                    "status": "CREATED",
                    "details": [
                        {
                            "name": "capacityProviderName",
                            "value": "CLItutorial-capacityprovider"
                        },
                        {
                            "name": "scalingPlanName",
                            "value": "ECSManagedAutoScalingPlan-27eb1e2a-5698-4ae7-b382-1553b8ba1095"
                        }
                    ]
                },
                {
                    "id": "03e99543-935d-4ea2-9a96-4b9dd63d320f",
                    "type": "asp",
                    "status": "CREATED",
                    "details": [
                        {
                            "name": "capacityProviderName",
                            "value": "CLItutorial-capacityprovider-burst"
                        },
                        {
                            "name": "scalingPlanName",
                            "value": "ECSManagedAutoScalingPlan-f9ea310b-680e-4654-b8c6-1c4862b29a77"
                        }
                    ]
                }
            ],
            "attachmentsStatus": "UPDATE_COMPLETE"
        }
    }

第 3 步:注册任务定义

您必须先注册任务定义,然后才能在集群上运行任务。任务定义是分组在一起的一系列容器。以下示例是一个简单的任务定义,它使用 Docker Hub 中的 amazonlinux 映像,并且直接休眠。有关可用任务定义参数的更多信息,请参阅 Amazon ECS 任务定义

注册任务定义

  1. 使用以下内容创建名为 CLItutorial-taskdef.json 的文件。

    { "family": "CLItutorial-taskdef", "containerDefinitions": [ { "name": "sleep", "image": "amazonlinux:2", "memory": 20, "essential": true, "command": [ "sh", "-c", "sleep infinity" ] } ], "requiresCompatibilities": [ "EC2" ] }
  2. 注册任务定义。

    aws ecs register-task-definition --cli-input-json file://CLItutorial-taskdef.json --region us-west-2

    输出在完成其注册后会返回任务定义的描述。

    {
        "taskDefinition": {
            "taskDefinitionArn": "arn:aws:ecs:us-west-2:111122223333:task-definition/CLItutorial-taskdef:1",
            "containerDefinitions": [
                {
                    "name": "sleep",
                    "image": "amazonlinux:2",
                    "cpu": 0,
                    "memory": 20,
                    "portMappings": [],
                    "essential": true,
                    "command": [
                        "sh",
                        "-c",
                        "sleep infinity"
                    ],
                    "environment": [],
                    "mountPoints": [],
                    "volumesFrom": []
                }
            ],
            "family": "sleep360",
            "revision": 1,
            "volumes": [],
            "status": "ACTIVE",
            "placementConstraints": [],
            "compatibilities": [
                "EC2"
            ],
            "requiresCompatibilities": [
                "EC2"
            ]
        }
    }

第 4 步:运行任务

为您的账户注册任务定义后,您可以在集群中运行任务。在本教程中,您将在 CLItutorial-cluster 集群中运行五个 CLItutorial-taskdef:1 任务定义实例。

运行任务

  • 运行您在上一步中注册的五个 sleep360:1 任务定义实例。

    aws ecs run-task --cluster CLItutorial-cluster --count 5 --task-definition CLItutorial-taskdef:1 --region us-west-2

    输出将返回任务的描述。每个任务都有一个与之关联的容量提供程序。

    {
        "tasks": [
            {
                "taskArn": "arn:aws:ecs:us-west-2:111122223333:task/CLItutorial-cluster/12648317756d430e8e320bbc4aEXAMPLE",
                "clusterArn": "arn:aws:ecs:us-west-2:111122223333:cluster/CLItutorial-cluster",
                "taskDefinitionArn": "arn:aws:ecs:us-west-2:111122223333:task-definition/CLItutorial-taskdef:1",
                "overrides": {
                    "containerOverrides": [],
                    "inferenceAcceleratorOverrides": []
                },
                "lastStatus": "PROVISIONING",
                "desiredStatus": "RUNNING",
                "cpu": "0",
                "memory": "20",
                "containers": [],
                "version": 1,
                "createdAt": 1574320187.938,
                "group": "family:CLItutorial-taskdef",
                "launchType": "EC2",
                "capacityProviderName": "CLItutorial-capacityprovider-burst",
                "attachments": [],
                "tags": []
            },
            {
                "taskArn": "arn:aws:ecs:us-west-2:111122223333:task/CLItutorial-cluster/e7f774f1570b4dddaa08626809EXAMPLE",
                "clusterArn": "arn:aws:ecs:us-west-2:111122223333:cluster/CLItutorial-cluster",
                "taskDefinitionArn": "arn:aws:ecs:us-west-2:111122223333:task-definition/CLItutorial-taskdef:1",
                "overrides": {
                    "containerOverrides": [],
                    "inferenceAcceleratorOverrides": []
                },
                "lastStatus": "PROVISIONING",
                "desiredStatus": "RUNNING",
                "cpu": "0",
                "memory": "20",
                "containers": [],
                "version": 1,
                "createdAt": 1574320187.938,
                "group": "family:CLItutorial-taskdef",
                "launchType": "EC2",
                "capacityProviderName": "CLItutorial-capacityprovider-burst",
                "attachments": [],
                "tags": []
            },
            {
                "taskArn": "arn:aws:ecs:us-west-2:111122223333:task/CLItutorial-cluster/f0f06980486e43438bc75a2184EXAMPLE",
                "clusterArn": "arn:aws:ecs:us-west-2:111122223333:cluster/CLItutorial-cluster",
                "taskDefinitionArn": "arn:aws:ecs:us-west-2:111122223333:task-definition/CLItutorial-taskdef:1",
                "overrides": {
                    "containerOverrides": [],
                    "inferenceAcceleratorOverrides": []
                },
                "lastStatus": "PROVISIONING",
                "desiredStatus": "RUNNING",
                "cpu": "0",
                "memory": "20",
                "containers": [],
                "version": 1,
                "createdAt": 1574320187.938,
                "group": "family:CLItutorial-taskdef",
                "launchType": "EC2",
                "capacityProviderName": "CLItutorial-capacityprovider",
                "attachments": [],
                "tags": []
            },
            {
                "taskArn": "arn:aws:ecs:us-west-2:111122223333:task/CLItutorial-cluster/7e3e0da4e71d4bf9ba8e4371dcEXAMPLE",
                "clusterArn": "arn:aws:ecs:us-west-2:111122223333:cluster/CLItutorial-cluster",
                "taskDefinitionArn": "arn:aws:ecs:us-west-2:111122223333:task-definition/CLItutorial-taskdef:1",
                "overrides": {
                    "containerOverrides": [],
                    "inferenceAcceleratorOverrides": []
                },
                "lastStatus": "PROVISIONING",
                "desiredStatus": "RUNNING",
                "cpu": "0",
                "memory": "20",
                "containers": [],
                "version": 1,
                "createdAt": 1574320187.938,
                "group": "family:CLItutorial-taskdef",
                "launchType": "EC2",
                "capacityProviderName": "CLItutorial-capacityprovider",
                "attachments": [],
                "tags": []
            },
            {
                "taskArn": "arn:aws:ecs:us-west-2:111122223333:task/CLItutorial-cluster/c71afd510c6e4ae58b86da3490EXAMPLE",
                "clusterArn": "arn:aws:ecs:us-west-2:111122223333:cluster/CLItutorial-cluster",
                "taskDefinitionArn": "arn:aws:ecs:us-west-2:111122223333:task-definition/CLItutorial-taskdef:1",
                "overrides": {
                    "containerOverrides": [],
                    "inferenceAcceleratorOverrides": []
                },
                "lastStatus": "PROVISIONING",
                "desiredStatus": "RUNNING",
                "cpu": "0",
                "memory": "20",
                "containers": [],
                "version": 1,
                "createdAt": 1574320187.938,
                "group": "family:CLItutorial-taskdef",
                "launchType": "EC2",
                "capacityProviderName": "CLItutorial-capacityprovider",
                "attachments": [],
                "tags": []
            }
        ],
        "failures": []
    }

第 5 步:验证

本教程到目前为止,您应该有两个 Auto Scaling 组,每个组都有一个容量提供程序。容量提供程序已启用 Amazon ECS 托管调整。已创建一个集群,五个任务正在运行。结果是您的 CLItutorial-asg 调整组应该包含两个实例,每个实例上都运行了两个任务,并且您的 CLItutorial-asg-burst 调整组应该包含两个实例,其中一个实例上正在运行一个任务。

验证调整

  1. 描述您的集群,以便确定有多少个容器实例已向它注册。

    aws ecs describe-clusters --clusters CLItutorial-cluster --include ATTACHMENTS --region us-west-2

    输出将返回集群的描述。以下代码段确认已注册的容器实例数量正确。

    {
        "clusters": [
            {
                "clusterArn": "arn:aws:ecs:us-west-2:111122223333:cluster/CLItutorial-cluster",
                "clusterName": "CLItutorial-cluster",
                "status": "ACTIVE",
                "registeredContainerInstancesCount": 3,
                "runningTasksCount": 3,
                ...
                "capacityProviders": [
                    "CLItutorial-capacityprovider-burst",
                    "CLItutorial-capacityprovider"
                ],
                "defaultCapacityProviderStrategy": [
                    {
                        "capacityProvider": "CLItutorial-capacityprovider",
                        "weight": 1,
                        "base": 0
                    },
                    {
                        "capacityProvider": "CLItutorial-capacityprovider-burst",
                        "weight": 1,
                        "base": 0
                    }
                ],
                "attachments": [
                    {
                        "id": "09f0a708-a91c-421f-a4a8-85db689a2244",
                        "type": "asp",
                        "status": "CREATED",
                        "details": [
                            {
                                "name": "capacityProviderName",
                                "value": "CLItutorial-capacityprovider-burst"
                            },
                            {
                                "name": "scalingPlanName",
                                "value": "ECSManagedAutoScalingPlan-811242a0-b26c-418c-86c0-2da93feb23e3"
                            }
                        ]
                    },
                    {
                        "id": "b3f407db-3d5b-4b51-88cd-bcd233d70682",
                        "type": "asp",
                        "status": "CREATED",
                        "details": [
                            {
                                "name": "capacityProviderName",
                                "value": "CLItutorial-capacityprovider"
                            },
                            {
                                "name": "scalingPlanName",
                                "value": "ECSManagedAutoScalingPlan-dcd587aa-484f-45d0-8385-5653da381038"
                            }
                        ]
                    }
                ],
                "attachmentsStatus": "UPDATE_COMPLETE"
            }
        ],
        "failures": []
    }
  2. 描述 Auto Scaling 组以验证调整计划是否设置了适当的期望容量值。

    aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names CLItutorial-asg CLItutorial-asg-burst --region us-west-2

    输出将返回 Auto Scaling 组的描述。以下代码段确认每个 Auto Scaling 组的期望容量和容器实例详细信息。

    {
        "AutoScalingGroups": [
            {
                "AutoScalingGroupName": "CLItutorial-asg-burst",
                "AutoScalingGroupARN": "arn:aws:autoscaling:us-west-2:111122223333:autoScalingGroup:89e0ae05-4f1d-46b7-b862-331a3fef4488:autoScalingGroupName/CLItutorial-asg-burst",
                "LaunchConfigurationName": "CLItutorial-launchconfig",
                "MinSize": 0,
                "MaxSize": 10000,
                "DesiredCapacity": 1,
                ...
                "Instances": [
                    {
                        "InstanceId": "i-0880a00d7040f2b6a",
                        "AvailabilityZone": "us-west-2c",
                        "LifecycleState": "InService",
                        "HealthStatus": "Healthy",
                        "LaunchConfigurationName": "CLItutorial-launchconfig",
                        "ProtectedFromScaleIn": true
                    }
                ],
                ...
                "NewInstancesProtectedFromScaleIn": true,
                "ServiceLinkedRoleARN": "arn:aws:iam::111122223333:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling"
            },
            {
                "AutoScalingGroupName": "CLItutorial-asg",
                "AutoScalingGroupARN": "arn:aws:autoscaling:us-west-2:111122223333:autoScalingGroup:292c0be4-4218-4f65-9903-df914d398658:autoScalingGroupName/CLItutorial-asg",
                "LaunchConfigurationName": "CLItutorial-launchconfig",
                "MinSize": 0,
                "MaxSize": 10000,
                "DesiredCapacity": 2,
                ...
                "Instances": [
                    {
                        "InstanceId": "i-0d29b646b6f6b69c5",
                        "AvailabilityZone": "us-west-2c",
                        "LifecycleState": "InService",
                        "HealthStatus": "Healthy",
                        "LaunchConfigurationName": "CLItutorial-launchconfig",
                        "ProtectedFromScaleIn": true
                    },
                    {
                        "InstanceId": "i-0eb1b33b75fb12da5",
                        "AvailabilityZone": "us-west-2c",
                        "LifecycleState": "InService",
                        "HealthStatus": "Healthy",
                        "LaunchConfigurationName": "CLItutorial-launchconfig",
                        "ProtectedFromScaleIn": true
                    }
                ],
                ...
                "NewInstancesProtectedFromScaleIn": true,
                "ServiceLinkedRoleARN": "arn:aws:iam::111122223333:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling"
            }
        ]
    }

步骤 6:清除

完成本教程后,请清除与其关联的资源,以避免产生与您未使用的资源相关的费用。不支持删除容量提供程序和任务定义,但这些资源不会产生任何开销。

清除教程资源

  1. 列出集群中的任务。

    aws ecs list-tasks --cluster CLItutorial-cluster --region us-west-2

    输出返回具有完整 ARN 的任务列表。

    {
        "taskArns": [
            "arn:aws:ecs:us-west-2:111122223333:task/3769f4fd-fe01-4629-9c9d-19b36bEXAMPLE",
            "arn:aws:ecs:us-west-2:111122223333:task/3a311591-de1a-4d6a-89dd-2be110EXAMPLE",
            "arn:aws:ecs:us-west-2:111122223333:task/5b46ed48-25c0-4eee-842d-8f89c6EXAMPLE",
            "arn:aws:ecs:us-west-2:111122223333:task/61b417b4-5a79-4cf7-9cef-18f5d4EXAMPLE",
            "arn:aws:ecs:us-west-2:111122223333:task/6272948e-09e9-4987-a26f-802de9EXAMPLE"
        ]
    }
  2. 使用上一步输出中的任务的 ID 或完整 ARN 来停止集群中的每个任务。对正在运行的五个任务中的每个任务重复此步骤。

    aws ecs stop-task --cluster CLItutorial-cluster --task 3769f4fd-fe01-4629-9c9d-19b36bEXAMPLE --region us-west-2

    输出将返回任务的描述,更新的期望状态为 STOPPED

  3. 按照以下步骤删除 Auto Scaling 组。指定 --force-delete 参数也会终止容器实例。

    1. 删除第一个 Auto Scaling 组。

      aws autoscaling delete-auto-scaling-group --auto-scaling-group-name CLItutorial-asg --force-delete --region us-west-2
    2. 删除第二个 Auto Scaling 组。

      aws autoscaling delete-auto-scaling-group --auto-scaling-group-name CLItutorial-asg-burst --force-delete --region us-west-2
  4. 删除 Amazon ECS 集群。

    aws ecs delete-cluster --cluster CLItutorial-cluster --region us-west-2