【问题标题】:AWS elastic beanstalk container link not workingAWS弹性beantalk容器链接不起作用
【发布时间】:2018-06-12 16:02:48
【问题描述】:

我创建了一个Dockerrun.aws.json 文件,该文件定义了我想使用 AWS Elastic Beanstalk 运行的两个 Docker 映像。一个是 nginx 容器,带有静态前端网站和 API 路由,另一个是后端 API。但由于某种原因,容器链接不起作用,nginx 容器无法将请求转发到 API。

每当 nginx 尝试 proxy_pass 请求时,我都会收到此错误:

2018/01/02 22:03:01 [error] 5#5: *22 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.1.70, server: localhost, request: "POST /api/auth/login HTTP/1.1", upstream: "http://172.17.0.2:5000/api/auth/login", host: "my-app-dev.us-west-2.elasticbeanstalk.com", referrer: "http://my-app-dev.us-west-2.elasticbeanstalk.com/login"

this aws multi-container documentation 看来,链接似乎是自动生成的?

这是我的 nginx 文件:

server {
    listen       4000;
    server_name  localhost;

    root   /usr/share/nginx/html;
    index  index.html index.htm;

    # Matches any request with a file extension. Adds cache headers.
    location ~ .*\..* {
        try_files $uri =404;
        expires 1y;
        access_log off;
        add_header Cache-Control "public";
    }

    # Matches api requests and redirects to the api server's port.
    location /api {
        proxy_pass http://api:5000;
    }

    # Any route that doesn't have a file extension. This helps react router route page urls using index.html.
    location / {
        try_files $uri $uri/ /index.html;
    }

}

还有我的Dockerrun.aws.json 文件:

{
  "AWSEBDockerrunVersion": "2",
  "containerDefinitions": [
    {
      "name": "proxy",
      "image": "my-app/proxy:1",
      "essential": true,
      "memory": 64,
      "portMappings": [
        {
          "hostPort": 80,
          "containerPort": 4000
        }
      ],
      "links": [
        "api"
      ]
    },
    {
      "name": "api",
      "image": "my-app/api:1",
      "essential": true,
      "memory": 128
    }
  ]
}

同样的设置在本地与 docker-compose 一起工作,但我尝试使用 aws 的方式显然不正确。

【问题讨论】:

    标签: amazon-web-services docker nginx amazon-elastic-beanstalk


    【解决方案1】:

    从上面的 nginx 错误日志中,nginx 能够将上游服务器端点解析为172.17.0.2:5000,这意味着 nginx docker 容器能够解析 api docker 容器的 ip

    抛出的错误是Connection Refused,这可能是由

    • 端口5000没有被api docker容器暴露
    • api容器ip地址(172.17.0.2)的Docker解析不正确
    • nginx容器所在的网络与api容器不同,导致无法连接

    对于第一种情况,你可以通过SSH进入EC2实例,查看api docker容器信息,看5000端口是否暴露,如果端口没有暴露,则Dockerfile应该修改暴露端口

    第二种和第三种情况不应发生,因为它由 AWS 处理

    无论如何要验证第二种和第三种情况,您可以通过 SSH 连接到运行 docker 容器的 EC2 实例,然后连接到 nginx 容器并执行telnet 172.17.0.2 5000,如果不成功则显然是网络问题

    【讨论】:

    • 我尝试过的其中一件事是在 api 容器的 Dockerfile 中公开端口 5000。明天我将尝试 SSH 并连接到 nginx 容器来检查。
    • 好吧,我通过 SSH 连接到 EC2 实例并进入我执行 telnet 172.17.0.2 5000 的 nginx 容器。这给出了错误:telnet: can't connect to remote host (172.17.0.2): Connection refused 这意味着显然存在网络问题......但不知道从这里做什么。 sudo docker ps 显示 api 容器端口信息:0.0.0.0:5000->5000/tcp。我的 Spring 引导日志显示 Tomcat 已使用正确的端口进行了初始化:Tomcat initialized with port(s): 5000 (http) 但我无法连接到它?
    【解决方案2】:

    由于内存不足,我的 java 应用程序似乎没有响应。升级了我的 aws 实例并将 "memory": 128 更改为 "memory": 512 并且它可以工作。毕竟没有网络问题。

    更好的解决方案是将“memory”更改为“memoryReservation”。这样,我定义了所需的最低内存,但它仍然可以尽可能多地利用 EC2 实例的额外内存。

    【讨论】:

    • 在这种情况下容器崩溃了吗?由于内存不足,容器中运行的进程应该已经崩溃,从而导致容器崩溃
    • @VineethGuna 当我登录到 EC2 实例时,容器还活着,只是没有响应。我也从未在日志中看到任何 Java OOM 或容器内存错误。我不知道为什么它如此神秘和沉默。无论哪种方式,我都非常感谢您的帮助,您的回答使我走上了调试它的正确道路!
    猜你喜欢
    • 1970-01-01
    • 2020-11-15
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    相关资源
    最近更新 更多