AWS EC2 容器服务/Elastic Beanstalk Docker 容器端口 udp 绑定

AWS EC2 Container Service / Elastic Beanstalk Docker Container Port udp binding

根据 ECS 任务定义的文档,这就是我们定义端口映射的方式。

"portMappings": [
    {
        "containerPort": integer,
        "hostPort": integer
    }
    ...
]

默认情况下,这是 tcp 上的绑定端口。如何在 AWS Elastic Beanstalk(多容器)或 AWS ECS 中实现 udp 到 docker 容器 运行 的端口绑定?

UDP 支持在 Amazon EC2 Container Service, see Ports are assumed to be TCP (issue #2) of the Amazon ECS Container Agent. Luckily this surprising gap has already been addressed and the new ECS agent version is pending release - I would expect this release to happen anytime soon and the AWS Elastic Beanstalk team is usually quick to update their official images in due course (keep an eye on the Elastic Beanstalk forum 的 GA 版本中仍然缺失。公告)。

根据https://github.com/aws/amazon-ecs-agent/issues/2,现在应该支持:

"portMappings": [
    {
        "containerPort": integer,
        "hostPort": integer,
        "protocol": "udp",
    }
    ...
]

@jrc 的回答是正确的。我已经在 Elastic Beanstalk 多容器 Docker 环境(与使用 的单容器 Docker 环境相反)上使用 Raintank 的 Graphite 堆栈图像 raintank/graphite-stack 对其进行了测试nginx 作为容器端口的反向代理,不支持多端口和 UDP,是 ECS 的前端)。对应的 Dockerrun.aws.json 是这样的:

{
  "AWSEBDockerrunVersion": 2,
  "containerDefinitions": [
    {
      "name": "stack",
      "image": "raintank/graphite-stack",
      "essential": true,
      "memory": 850,
      "portMappings": [
        {
          "containerPort": 3000,
          "hostPort": 80
        },
        {
          "containerPort": 8125,
          "hostPort": 8125,
          "protocol": "udp"
        }
      ]
    }
  ]
}

然后登录到相应的 EC2 实例并键入 sudo docker ps 得到:

CONTAINER ID        IMAGE                            COMMAND                  CREATED             STATUS              PORTS                                                                       NAMES
12a5711dab47        raintank/graphite-stack          "/usr/bin/supervisord"   2 minutes ago       Up 2 minutes        443/tcp, 2003/tcp, 8125/tcp, 0.0.0.0:8125->8125/udp, 0.0.0.0:80->3000/tcp   ecs-awseb-test-abc-1-stack-abc
930a9b814df4        amazon/amazon-ecs-agent:latest   "/agent"                 3 minutes ago       Up 3 minutes                                                                                    ecs-agent

手动发出 statsd UDP 数据包,echo "test.statsd:1|c" | nc -w 1 -u test.aws-region.elasticbeanstalk.com 8125,我看到它出现在 Graphite 中。