【发布时间】:2021-01-09 15:42:43
【问题描述】:
我在 ECS 集群上配置了目标跟踪策略,用于管理 ASG 自动缩放的服务和容量提供程序
在我的集群服务中的最小和最大任务数与 ASG 中的最小和最大容量相同。
当执行规模化操作时,任务减少到最少。但 ASG 仍有 1 个或多个未使用(任务未放置在此 EC2 实例上)ec2 实例
如何使用容量提供程序配置我的集群以执行缩减到最小计数 ASG 容量?
# CLUSTER
resource "aws_ecs_cluster" "default" {
name = local.name
capacity_providers = [aws_ecs_capacity_provider.asg.name]
tags = local.tags
default_capacity_provider_strategy {
base = 0
capacity_provider = aws_ecs_capacity_provider.asg.name
weight = 1
}
}
# SERVICE
resource "aws_ecs_service" "ecs_service" {
name = "${local.name}-service"
cluster = aws_ecs_cluster.default.id
task_definition = aws_ecs_task_definition.ecs_task.arn
health_check_grace_period_seconds = 60
deployment_maximum_percent = 50
deployment_minimum_healthy_percent = 100
load_balancer {
target_group_arn = element(module.aws-alb-common-module.target_group_arns, 1)
container_name = local.name
container_port = 8080
}
lifecycle {
ignore_changes = [desired_count, task_definition]
}
}
# CAPACITY PROVIDER
resource "aws_ecs_capacity_provider" "asg" {
name = aws_autoscaling_group.ecs_nodes.name
auto_scaling_group_provider {
auto_scaling_group_arn = aws_autoscaling_group.ecs_nodes.arn
managed_termination_protection = "DISABLED"
managed_scaling {
maximum_scaling_step_size = 10
minimum_scaling_step_size = 1
status = "ENABLED"
target_capacity = 100
}
}
}
# SERVICE AUTOSCALING POLICY
resource "aws_appautoscaling_target" "ecs_target" {
max_capacity = 20
min_capacity = 2
resource_id = "service/${local.name}/${aws_ecs_service.ecs_service.name}"
scalable_dimension = "ecs:service:DesiredCount"
service_namespace = "ecs"
}
resource "aws_appautoscaling_policy" "ecs_policy" {
name = "${local.name}-scale-policy"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.ecs_target.resource_id
scalable_dimension = aws_appautoscaling_target.ecs_target.scalable_dimension
service_namespace = aws_appautoscaling_target.ecs_target.service_namespace
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "ECSServiceAverageCPUUtilization"
}
target_value = 2
}
# ASG
resource "aws_autoscaling_group" "ecs_nodes" {
name_prefix = "${local.name}-node"
max_size = 20
min_size = 2
vpc_zone_identifier = local.subnets_ids
protect_from_scale_in = false
mixed_instances_policy {
instances_distribution {
on_demand_percentage_above_base_capacity = local.spot
}
launch_template {
launch_template_specification {
launch_template_id = aws_launch_template.node.id
version = "$Latest"
}
dynamic "override" {
for_each = local.instance_types
content {
instance_type = override.key
weighted_capacity = override.value
}
}
}
}
lifecycle {
create_before_destroy = true
}
tag {
key = "AmazonECSManaged"
propagate_at_launch = true
value = ""
}
}
【问题讨论】:
-
可能与 base = 1 设置有关吗?
-
我尝试设置 base = 1 或 base = 2 但有 4 个实例或 3 个而不是 1 .. 或 asg (2) 的最小容量
-
啊,我误解了最初的问题,但是您对 ASG 最小容量为 2 的澄清使它更清楚。 managed_termination_protection 可能会导致终止实例的延迟,即使它们上没有正在运行的任务。您是否离开配置足够长的时间以确保其行为不符合设计?有关示例,请参见 aws.amazon.com/blogs/containers/… 的“缩放”部分。
-
我在 asg 中禁用了终止保护并禁用了受保护的实例。 ASG 扩展到 10,但仅扩展到 6,而不是最小 (2),但任务扩展到最小 (2)
-
您能否将当前版本的代码以及 aws_autoscaling_group.asg 的代码和变量定义附加到您的帖子中,包括目标跟踪扩展策略和 ASG 实例缩减保护配置?另外,是否涉及任何ELB?您是否检查了实例级缩减保护设置?
标签: amazon-web-services terraform amazon-ecs