Ubuntu

Docker compose 和 nginx 負載均衡器

  • July 19, 2017

目前我有 2 個 Docker 容器:

  • 應用程序(python燒瓶)
  • 負載均衡器 (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 文件中啟動所有這些嗎?

您是否在 docker-compose.yml 文件中定義了指向 application_application_1 的連結?

連結成功了:

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

引用自:https://serverfault.com/questions/757235