Docker compose 和 nginx 负载均衡器

Docker compose 和 nginx 负载均衡器

目前我有2个Docker容器:

  • 应用程序(python flask)
  • 负载均衡器 (nginx)

我将分别启动两个容器docker-composer up Nginx 正在监听端口 8080,应该将 HTTP 请求转发到监听端口 8081 的应用程序。目前这不起作用,因为 nginx 对应用程序容器一无所知。

86615a5b0f02        parzee/loadbalancer   "/usr/local/bin/start"   3 minutes ago       Up 3 minutes        127.0.0.1:8080->8080/tcp   loadbalancer_loadbalancer_1
9adc8a04b356        parzee/application    "/usr/local/bin/start"   6 minutes ago       Up 5 minutes        127.0.0.1:8081->8081/tcp   application_application_1

这是我的默认配置

upstream application {
    ip_hash;
        server application_application_1:8081 fail_timeout=0;
        server application_application_2:8081 fail_timeout=0;
    keepalive 64;
}

server {
    listen 8080;
    server_name loadbalancer;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    location / {
        proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;

    #Timeout after 8 hours
    proxy_read_timeout 43200000;
    proxy_connect_timeout 43200000;

        proxy_redirect off;
        if (!-f $request_filename) {
            proxy_pass http://application;
            break;
        }
    }
}

我得到:

2016/02/15 08:42:07 [emerg] 21#0: host not found in upstream "application_application_1:8081" in /etc/nginx/sites-enabled/default:3
2016/02/15 08:42:10 [emerg] 22#0: host not found in upstream "application_application_1:8081" in /etc/nginx/sites-enabled/default:3

如何通过使用主机或 IP 地址来定义我的 nginx 配置?我没有使用 IP 地址,因为 docker 可能会从已知范围内分配不同的 IP。有什么建议吗?我应该在单个 docker-compose.yml 文件中启动它们吗?

答案1

你定义了一个关联到docker-compose.yml文件中的application_application_1?

答案2

链接做了这个伎俩:

database:
  image: parzee/database
  volumes_from:
    - dbdata
  ports:
    - "5432:5432"

# RABBITMQ TCP:5672
rabbitmq:
  image: parzee/rabbitmq-server
  hostname: rabbitmq
  ports:
    - "5672:5672"
    - "15672:15672"

# LIGHTTPD TCP:8083
lighttpd:
  image: parzee/lighttpd
  hostname: lighttpd
  ports: 
    - "8083:8083"
  volumes:
    - /usr/local/src/containers/application/volumes/imbue/:/usr/local/src/imbue/application/imbue/utils/security/des/
    - /usr/local/src/containers/lighttpd/volumes/log/:/var/log/lighttpd/ 

# APPLICATION TCP:8081
application_1:
  image: parzee/application
  hostname: application_1
  links:
        - database:database
        - lighttpd:lighthttpd
        - rabbitmq:rabbitmq

相关内容