如何设置 ECS 服务最小和最大任务

How to set ECS Service minimum & maximum tasks

如何通过API调用设置ECS服务的最小和最大任务数?我知道您可以通过以下 api 设置所需的任务数,但我没有看到任何地方可以设置最小和最大任务数?我错过了什么吗?我正在使用 PHP API,但此处的任何见解都会有所帮助。

https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-ecs-2014-11-13.html#updateservice

我的理解是最小值和最大值只能通过 ECS 服务的 Auto Scaling 策略设置。 https://docs.aws.amazon.com/AmazonECS/latest/developerguide/service-auto-scaling.html。 您将需要创建自动缩放策略来设置这些。

Amazon ECS、CloudWatch 和应用程序 Auto Scaling APIs 的组合使服务 Auto Scaling 成为可能。

在您的情况下,只需使用 Application AutoScaling API、registerScalableTarget 方法调用就足够了。这是示例。

https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.ApplicationAutoScaling.ApplicationAutoScalingClient.html

$result = $client->registerScalableTarget([
    'MaxCapacity' => 20,
    'MinCapacity' => 2,
    'ResourceId' => 'service/default/sample-webapp', // REQUIRED
    'RoleARN' => 'arn:aws:iam::012345678910:role/ApplicationAutoscalingECSRole',
    'ScalableDimension' => 'ecs:service:DesiredCount', // REQUIRED
    'ServiceNamespace' => 'ecs', // REQUIRED
]);

对于那些正在寻找 CLI 示例的人,

aws application-autoscaling \
register-scalable-target \
--service-namespace ecs \
--resource-id service/cluster-name/service-name \
--scalable-dimension ecs:service:DesiredCount \
--min-capacity 2 \
--max-capacity 4