【问题标题】:How to make ALB slow_start work during ECS update serviceECS更新服务时如何让ALB slow_start工作
【发布时间】:2023-02-01 16:04:09
【问题描述】:

对于我在 ECS Fargate 中运行的高流量容器化应用程序,新容器需要缓慢启动,以避免启动后立即出现内存不足的情况。当所有容器同时被替换时,这在更新服务操作期间尤其重要。

我怎样才能让它与 ECS Fargate 和 ALB 一起工作,确保旧容器一直存在,直到新容器的 slow_start 期结束?

这是我当前的地形设置。我启用了slow_start,但在更新服务期间,旧容器过早停止,因此新容器立即获得全部流量。

resource "aws_alb_target_group" "my_target_group" {
  name        = "my_service"
  port        = 8080
  protocol    = "HTTP"
  vpc_id      = data.aws_vpc.active.id
  target_type = "ip"
  slow_start  = 120

  health_check {
    enabled             = true
    port                = 8080
    path                = "/healthCheck"
    unhealthy_threshold = 2
    healthy_threshold   = 2
  }
}

resource "aws_ecs_service" "my_service" {
  name                               = "my_service"
  cluster                            = aws_ecs_cluster.my_services.id
  task_definition                    = aws_ecs_task_definition.my_services.arn
  launch_type                        = "FARGATE"
  desired_count                      = var.desired_count
  deployment_maximum_percent         = 400
  deployment_minimum_healthy_percent = 100
  enable_execute_command             = true

  wait_for_steady_state = true

  network_configuration {
    subnets         = data.aws_subnets.private.ids
    security_groups = [aws_security_group.my_service_container.id]
  }

  load_balancer {
    container_name   = "my-service"
    container_port   = 8080
    target_group_arn = aws_alb_target_group.my_target_group.arn
  }

  lifecycle {
    create_before_destroy = true
    ignore_changes        = [desired_count]
  }
}

【问题讨论】:

  • 你可以试试stopTimeout
  • 嗯,我认为 stopTimeout 仅用于拒绝关闭并需要强行杀死的容器。这里不是这种情况,我的应用程序干净地关闭了。
  • 您是否尝试过在 deregistration_delay 选项中设置更大的值?
  • 文档说默认的 deregistration_delay 是 300 秒,但是我的容器在大约 40 秒后停止,只要新容器启动并运行。此外,我的请求的响应时间非常短,约为 10-30 毫秒,因此我认为注销不是这里的主要问题。我的感觉是 ECS 部署不知道 ALB slow_start 特性,所以它在启动完成之前终止了容器。
  • 你的 desired_count 是多少?我想知道问题是否是 min% 太低,导致 ECS 部署过早终止“旧”任务——这只留下“新”任务,因此它们会立即退出慢启动模式?

标签: amazon-web-services terraform amazon-ecs terraform-provider-aws aws-application-load-balancer


【解决方案1】:

aws ecs 通常会发送 sigterm 以正常关闭。如果 30 秒过去了,则发送 sigkill。所以你可以处理这个 sigterm 信号(例如在 python 中捕获这个信号)并在你的代码中添加延迟。之后,您需要在 ContainerDefinition 中使用 stopTimeout 调整 sigkill 30 秒等待,这样 aws 就不会快速关闭您的 ecs

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-09
    • 1970-01-01
    • 2018-08-09
    • 2019-12-31
    • 1970-01-01
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多