简体   繁体   English

AWS ECS Fargate 和端口映射

[英]AWS ECS Fargate and port mapping

I have two containers and they expose the same port.我有两个容器,它们暴露同一个端口。 I want to run them in the same task as they are part of the same system.我想在同一个任务中运行它们,因为它们是同一个系统的一部分。 But I cannot do this with Fargate because there are no port mapping and the host port should be the same as container port for the awsvpc network mode (only supported by Fargate).但我无法使用 Fargate 执行此操作,因为没有端口映射,并且主机端口应与 awsvpc 网络模式的容器端口相同(仅受 Fargate 支持)。

It's an essential feature of Docker and it's strange that it seems to be not supported by Fargate.这是 Docker 的一个基本特性,奇怪的是 Fargate 似乎不支持它。 Is there really no way to do this or I'm missing something?真的没有办法做到这一点还是我错过了什么?

Use application load balancer to your service and set your custom port in target group and host port should be set the same as container port.对您的服务使用应用程序负载均衡器并在目标组中设置您的自定义端口,主机端口应设置为与容器端口相同。 This is our tested solution.这是我们经过测试的解决方案。

The simplest way to solve this problem is to make the port of your Docker container configurable and then pass it to the container as an environment variable.解决这个问题最简单的方法是让你的 Docker 容器的端口可配置,然后将它作为环境变量传递给容器。 Example:例子:

Dockerfile Dockerfile

FROM python:3.10-slim-bullseye
ENV PORT 5000

# Do all your container setup
# ...

EXPOSE $PORT
ENTRYPOINT path/to/entrypoint.sh

In entrypoint.sh , you need to set the app itself to use the port provided in the PORT environment var.entrypoint.sh中,您需要将应用程序本身设置为使用PORT环境变量中提供的端口。

Then in your task definition, set the port mapping to a different port per container and provide the port as an env var:然后在您的任务定义中,将端口映射设置为每个容器的不同端口,并将端口作为 env var 提供:

{
// ...
      "portMappings": [
        {
          "hostPort": 5001,
          "protocol": "tcp",
          "containerPort": 5001
        }
      ],
// ...
      "environment": [
        {
          "name": "PORT",
          "value": "5001"
        },
      ]
}

If you don't override the port via an environment variable, it will use the default port declared in the Dockerfile (in this case, 5000).如果您不通过环境变量覆盖端口,它将使用 Dockerfile 中声明的默认端口(在本例中为 5000)。

You will have to switch to ec2 based ecs instead of fargate.您将不得不切换到基于 ec2 的 ecs 而不是 fargate。 Also you can run on different port and use service discovery feature in fargate to communicate each other.您也可以在不同的端口上运行并使用 Fargate 中的服务发现功能相互通信。 Might need code changes.可能需要更改代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM