首页 > 解决方案 > AWS - 在 Terraform 中运行带有 ALB 和超过 5 个端口的 ECS EC2 集群运行容器

问题描述

我正在运行一个包含大约 20 个容器的 ECS 集群。我有一个在 1 个容器上运行的大型单体应用程序,需要监听 10 个端口。

然而,AWS 要求一个 ECS 服务中最多有 5 个负载均衡器目标组链接。

任何想法如何克服这个(如果可能的话)?这是我尝试过的:

  1. 定义 10 多个目标群体,每个群体有 1 个听众。不起作用,因为 AWS 在 aws_ecs_service 中最多需要 5 个负载均衡器定义 - 供参考 - 此处:https ://docs.aws.amazon.com/AmazonECS/latest/developerguide/service-load-balancing.html如所述在“服务负载平衡注意事项”下的第二个项目符号中
  2. 定义 10 多个具有 1 个目标组的侦听器 - 但是所有侦听器都转发到容器上的单个端口...
  3. 尝试在 aws_ecs_service 的 load_balancer 定义中未指定端口,但 AWS 抱怨缺少参数
  4. 尝试未在 aws_lb_target_group 中指定端口,但 AWS 抱怨目标类型为“ip”,因此需要端口...

这是我当前的代码:

resource "aws_ecs_service" "service_app" {
    name = "my_service_name"
    cluster = var.ECS_CLUSTER_ID
    task_definition = aws_ecs_task_definition.task_my_app.arn
    desired_count = 1
    force_new_deployment = true

...

   load_balancer { # Note: I've stripped the for_each to simplify reading
        target_group_arn = var.tga
        container_name = var.n
        container_port = var.p
    }
}


resource "aws_lb_target_group" "tg_mytg" {
    name = "Name"
    protocol = "HTTP"
    port = 3000
    target_type = "ip"
    vpc_id = aws_vpc.my_vpc.id
}
resource "aws_lb_listener" "ls_3303" {
  load_balancer_arn = aws_lb.my_lb.id
  port = "3303"
  protocol = "HTTP"
  default_action {
    type = "forward"
    target_group_arn = aws_lb_target_group.tg_mytg.arn
  }
}

...

标签: amazon-web-servicesamazon-ec2terraformload-balancingamazon-ecs

解决方案


推荐阅读