AWS弹性beanstalk容器链接不起作用

问题描述 投票:0回答:2

我制作了一个Dockerrun.aws.json文件,它定义了我想用AWS Elastic Beanstalk运行的两个Docker镜像。一个是带有静态前端网站和API路由的nginx容器,另一个是后端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 elastic-beanstalk
2个回答
1
投票

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

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

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

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

第二和第三种情况不应该发生,因为它由AWS负责

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


0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.