【问题标题】:error ClientException: Links are not supported when networkMode=awsvpc in fargate terraform错误ClientException:在fargate terraform中networkMode = awsvpc时不支持链接
【发布时间】:2022-02-08 10:24:16
【问题描述】:

我在 terraform 中遇到错误:

ClientException: Links are not supported when networkMode=awsvpc.

我将 nginx 与我的 nodejs 应用程序链接起来。我希望 nodejs 在 3000 端口上运行,而 nginx 在 80 上运行,当请求传入时,nginx 将对 nodejs 进行代理。 (我已将 nginx.conf 与代理定义为 3000)。

它发生在 Fargate 模式下。但是我这里没有选择,网络模式只有awsvpc

我能做什么?有没有办法在容器之间进行链接?一个conatiner,nginx和nodejs里面?只是nodejs?其他解决方案?

这里是我的 terraform 设置:

resource "aws_ecs_service" "nginx" {
  name            = "nginx-${var.app}"
  cluster         = aws_ecs_cluster.demo.id
  task_definition = aws_ecs_task_definition.nginx.arn
  desired_count   = 4
  launch_type = "FARGATE"

  lifecycle {
    ignore_changes = [task_definition]
  }
 ...
}

resource "aws_ecs_task_definition" "nginx" {
  family = "nginx-${var.app}"

  container_definitions = file("container_definitions.json")

  requires_compatibilities = ["FARGATE"]
  network_mode = "awsvpc"
}

container_definitions.json:

[
  {
    "name": "nginx-...nginx-www",
    "image": "311443.dkr.ecr.us-east-2.amazonaws.com/...-nginx:latest",
    "cpu": 256,
    "memory": 256,
    "essential": true,
    "links": ["www-app"],
    "portMappings": [
      {
        "protocol": "tcp",
        "containerPort": 80
      }
    ],
    "logConfiguration": {
      "logDriver": "awslogs",
      "options": {
        "awslogs-group": "...",
        "awslogs-region": "us-east-2",
        "awslogs-stream-prefix": "ecs"
      }
    }
  },
  {
    "name": "www-app",
    "image": "93934.dkr.ecr.us-east-2.amazonaws.com/..../www-app:latest",
    "cpu": 256,
    "memory": 256,
    "essential": true,
    "logConfiguration": {
      "logDriver": "awslogs",
      "options": {
        "awslogs-group": "/ecs-..../nginx",
        "awslogs-region": "us-east-2",
        "awslogs-stream-prefix": "ecs"
      }
    }
  }
]

【问题讨论】:

标签: node.js nginx terraform terraform-provider-aws


【解决方案1】:

TL;DR

我建议您将位于 s3 的配置文件链接到 ecs nginx。


在 ecs 容器之间,彼此链接在同一个网络上。如果您使用 Fargate,AWS 会强制执行网络模式。

有两种解决方法。

  1. 为容器 nginx 映像配置 nginx 配置。

只需将 web 端口 80 转发到 3000 节点端口。 Nginx 主要是设计该主题的。

将 nginx.config 文件上传到 s3,然后使用“EnvironmentFile”字段将该 s3 arn 链接到 ecs。

或者您可以使用自定义配置构建自己的 nginx 映像(不推荐)。

  1. 干脆放弃使用 nginx。 Web 端口可以通过应用负载均衡器转发。

您可以在 Application Load Balancer 上从 80 转发到 3000。这取决于您的应用程序大小。如果那是简单的应用程序,你不必使用 nginx。

通常Nginx用作反向代理,但可以用来控制http缓冲(proxy buffer)、优雅重启、排队请求等。

【讨论】:

    猜你喜欢
    • 2019-12-10
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 2021-06-27
    • 2021-01-30
    • 1970-01-01
    • 2021-01-16
    • 2019-11-03
    相关资源
    最近更新 更多