【问题标题】:Attach Auto-Scaling Policy to ECS service from CLI从 CLI 将 Auto-Scaling 策略附加到 ECS 服务
【发布时间】:2019-04-10 12:53:18
【问题描述】:

我在使用 Fargate 部署的 ECS 上运行了一项服务。我正在使用ecs-cli compose 启动此服务。这是我目前使用的命令:

ecs-cli compose service up --cluster my_cluster —-launch-type FARGATE

我还有一个ecs-params.yml 来配置这个服务。内容如下:

version: 1
task_definition:
  task_execution_role: ecsTaskExecutionRole
  task_role_arn: arn:aws:iam::XXXXXX:role/MyExecutionRole 
  ecs_network_mode: awsvpc
  task_size:
    mem_limit: 2GB
    cpu_limit: 1024
run_params:
  network_configuration:
    awsvpc_configuration:
      subnets:
        - "subnet-XXXXXXXXXXXXXXXXX"
        - "subnet-XXXXXXXXXXXXXXXXX"
      security_groups:
        - "sg-XXXXXXXXXXXXXX"
      assign_public_ip: ENABLED

创建服务后,我必须登录 AWS 控制台并通过 AWS GUI 附加自动扩展策略。有没有更简单的方法可以通过 CLI 或在我的 YAML 配置中附加自动扩展策略?

【问题讨论】:

  • 通过 ecs-cli 或 Yaml 配置是不可能的,因为要添加/创建扩展策略,您需要进行多个不同的 AWS API 调用。您可以使用 AWS CLI 或 boto3 客户端来执行这些操作。

标签: amazon-web-services amazon-cloudwatch amazon-ecs aws-fargate


【解决方案1】:

虽然您可以使用 AWS CLI 本身(请参阅文档中的 application-autoscaling),

我认为在一个部署中执行整个操作要好得多,为此,您可以使用诸如Terraform 之类的工具。

你可以使用 Github 上 arminc 写的terraform-ecs module,也可以自己动手!这是整个集群的一个快速(而且非常肮脏)的示例,但如果您不想将整个部署集中在一个地方,您也可以只获取自动缩放部分并使用它:

provider "aws" {
  region = "us-east-1" # insert your own region
  profile = "insert aw cli profile, should be located in ~/.aws/credentials file"
  # you can also use your aws credentials instead
  # access_key = "insert_access_key"
  # secret_key = "insert_secret_key"
}


resource "aws_ecs_cluster" "cluster" {
  name = "my-cluster"
}

resource "aws_ecs_service" "service" {
  name = "my-service"
  cluster = "${aws_ecs_cluster.cluster.id}"

  task_definition = "${aws_ecs_task_definition.task_definition.family}:${aws_ecs_task_definition.task_definition.revision}"

  network_configuration {
    # These can also be created with Terraform and applied dynamically instead of hard-coded
    # look it up in the Docs

    security_groups = ["SG_IDS"]
    subnets         = ["SUBNET_IDS"] # can also be created with Terraform
    assign_public_ip = true
  }
}

resource "aws_ecs_task_definition" "task_definition" {
  family = "my-service"
  execution_role_arn = "ecsTaskExecutionRole"
  task_role_arn = "INSERT_ARN"
  network_mode = "awsvpc"
  container_definitions = <<DEFINITION
[
  {
    "name": "my_service"
    "cpu": 1024,
    "environment": [{
      "name": "exaple_ENV_VAR",
      "value": "EXAMPLE_VALUE"
    }],
    "essential": true,
    "image": "INSERT IMAGE URL",
    "memory": 2048,
    "networkMode": "awsvpc"
  }
]
DEFINITION
}

#
# Application AutoScaling resources
#
resource "aws_appautoscaling_target" "main" {
  service_namespace  = "ecs"
  resource_id    = "service/${var.cluster_name}/${aws_ecs_service.service.name}"
  scalable_dimension = "ecs:service:DesiredCount"
  # Insert Min and Max capacity here
  min_capacity       = "1"
  max_capacity       = "4"

  depends_on = [
    "aws_ecs_service.main",
  ]
}

resource "aws_appautoscaling_policy" "up" {
  name               = "scaling_policy-${aws_ecs_service.service.name}-up"
  service_namespace  = "ecs"
  resource_id        = "service/${aws_ecs_cluster.cluster.name}/${aws_ecs_service.service.name}"
  scalable_dimension = "ecs:service:DesiredCount"

  step_scaling_policy_configuration {
    adjustment_type         = "ChangeInCapacity"
    cooldown                = "60" # In seconds
    metric_aggregation_type = "Average"

    step_adjustment {
      metric_interval_lower_bound = 0
      scaling_adjustment          = 1 # you can also use negative numbers for scaling down
    }
  }

  depends_on = [
    "aws_appautoscaling_target.main",
  ]
}

resource "aws_appautoscaling_policy" "down" {
  name               = "scaling_policy-${aws_ecs_service.service.name}-down"
  service_namespace  = "ecs"
  resource_id        = "service/${aws_ecs_cluster.cluster.name}/${aws_ecs_service.service.name}"
  scalable_dimension = "ecs:service:DesiredCount"

  step_scaling_policy_configuration {
    adjustment_type         = "ChangeInCapacity"
    cooldown                = "60" # In seconds
    metric_aggregation_type = "Average"

    step_adjustment {
      metric_interval_upper_bound = 0
      scaling_adjustment          = -1 # scale down example
    }
  }

  depends_on = [
    "aws_appautoscaling_target.main",
  ]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 2021-03-04
    • 2023-03-14
    • 2020-12-03
    • 2020-08-07
    • 1970-01-01
    • 2021-01-07
    相关资源
    最近更新 更多