本文介绍了使用AWS ECS服务和Elastic LoadBalancer向公众公开多个端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拥有公开多个端口的服务,并且可以与kubernetes一起正常工作,但是现在我们将其移至AWS ECS.似乎我只能通过Load Balancer公开端口,并且每个服务/任务只能使用1个端口,即使docker定义了多个端口,我也必须选择一个端口

I have service that exposes multiple ports and it worked fine with kubernetes but now we move it to AWS ECS. It seems I can only expose ports via Load Balancer and I am limited to 1 port per service/tasks even when docker defines multiple ports I have to choose one port

添加到负载均衡器按钮允许添加一个端口.添加后,将没有按钮添加第二个端口.

Add to load balancer button allows to add one port. Once added there is no button to add second port.

是否有比提供第二个代理服务来公开第二个端口更好的工作环境?

Is there any nicer workarround than making second proxy service to expose second port?

更新:我使用基于Fargate的服务.

UPDATE: I use fargate based service.

推荐答案

更新:我可以使用Terraform来配置目标组,但到目前为止在AWS控制台上找不到此选项.

Update:I was able to configure target group using Terraform but did not find so far this option on AWS console.

resource "aws_ecs_service" "multiple_target_example" {
  name            = "multiple_target_example1"
  cluster         = "${aws_ecs_cluster.main.id}"
  task_definition = "${aws_ecs_task_definition.with_lb_changes.arn}"
  desired_count   = 1
  iam_role        = "${aws_iam_role.ecs_service.name}"

  load_balancer {
    target_group_arn = "${aws_lb_target_group.target2.id}"
    container_name   = "ghost"
    container_port   = "3000"
  }

  load_balancer {
    target_group_arn = "${aws_lb_target_group.target2.id}"
    container_name   = "ghost"
    container_port   = "3001"
  }

  depends_on = [
    "aws_iam_role_policy.ecs_service",
  ]
}

ecs_service_terraform

我不能说这将是一个不错的解决方法,但是我正在开发一个项目,该项目需要使用AWS ECS运行Ejabberd,但是当它将服务端口绑定到负载均衡器时发生了相同的问题.

I can't say that this will be a nice workaround but I was working on a project where I need to run Ejabberd using AWS ECS but the same issue happened when its come to bind port of the service to the load balancer.

我正在使用terraform,由于AWS ECS的这一限制,我们同意为每个实例运行一个容器来解决端口问题,因为我们应该公开两个端口.

I was working with terraform and due to this limitation of AWS ECS, we agree to run one container per instance to resolve the port issue as we were supposed to expose two port.

如果您不想为容器分配动态端口,并且希望每个实例运行一个容器,那么该解决方案肯定会起作用.

If you do not want to assign a dynamic port to your container and you want to run one container per instance then the solution will definitely work.

  1. 创建目标组并指定容器的第二个端口.

  1. Create a target group and specify the second port of the container.

转到ECS集群的AutoScalingGroups

Go to the AutoScalingGroups of your ECS cluster

在ECS集群的Autoscaling组中编辑并添加新创建的目标组

Edit and add the newly created target group of in the Autoscaling group of the ECS cluster

因此,如果您缩放到两个容器,则意味着将有两个实例,因此新启动的实例将注册到第二个目标组,而Autoscaling组将负责该操作.在我看来,这种方法效果很好,但是无需考虑任何事情.

So if you scale to two containers it's mean there will be two instances so the newly launch instance will register to the second target group and Autoscaling group take care of it.This approach working fine in my case, but few things need to be consider.

当Docker容器中有动态端口公开时,此方法将不起作用.

This approach will not work when there is dynamic port expose in Docker container.

AWS应该更新其ECS代理以处理这种情况.

AWS should update its ECS agent to handle such scenario.

这篇关于使用AWS ECS服务和Elastic LoadBalancer向公众公开多个端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 22:21