2017-06-09 56 views
0

我正在使用Docker/Node/Nginx設置一個Web服務器。我一直在玩docker-compose中的設置,並提出了兩個工作解決方案 - 但在負載平衡方面,其中一個可能太好了,無法成立(因爲它似乎允許我通過沒有創建額外的圖像/容器)。如果我所看到的實際上是合法的,並且多個圖像等不是負載平衡的要求,我正在尋找驗證。這個Docker/NGINX/Node設置實際上是否按預期負載平衡?

溶液1(無附加圖像):

搬運工-compose.yml

version: '3' 

volumes: 
    node_deps: 

services: 
    nginx: 
    build: ./nginx 
    image: nginx_i 
    container_name: nginx_c 
    ports: 
     - '80:80' 
     - '443:443' 
    links: 
     - node 
    restart: always 
    node: 
    build: ./node 
    image: node_i 
    container_name: node_c 
    command: "npm start" 
    ports: 
     - '5000:5000' 
     - '5001:5001' 
     - '5500:5000' 
     - '5501:5001' 
    volumes: 
     - ./node:/src 
     - node_deps:/src/node_modules 

nginx.conf

http { 
    ... 

    upstream shopster-node { 
    server node:5000 weight=10 max_fails=3 fail_timeout=30s; 
    server node:5500 weight=10 max_fails=3 fail_timeout=30s; 
    keepalive 64; 
    } 

    server { 
    ... 
    } 

} 

溶液2(具有附加圖像):

version: '3' 

volumes: 
    node_deps: 

services: 
    nginx: 
    build: ./nginx 
    image: nginx_i 
    container_name: nginx_c 
    ports: 
     - '80:80' 
     - '443:443' 
    links: 
     - node_one 
     - node_two 
    restart: always 
    node_one: 
    build: ./node 
    image: node_one_i 
    container_name: node_one_c 
    command: "npm start" 
    ports: 
     - '5000:5000' 
     - '5001:5001' 
    volumes: 
     - ./node:/src 
     - node_deps:/src/node_modules 
    node_two: 
    build: ./node 
    image: node_two_i 
    container_name: node_two_c 
    command: "npm start" 
    ports: 
     - '5500:5000' 
     - '5501:5001' 
    volumes: 
     - ./node:/src 
     - node_deps:/src/node_modules 

ngin x.conf

http { 
    ... 

    upstream shopster-node { 
    server node_one:5000 weight=10 max_fails=3 fail_timeout=30s; 
    server node_two:5500 weight=10 max_fails=3 fail_timeout=30s; 
    keepalive 64; 
    } 

    server { 
    ... 
    } 

} 

這兩種情況都加載該應用完美,在localhost &在指定端口上。我確信場景2是正確的負載平衡,因爲它模仿了傳統的多服務器場景。

有什麼辦法可以驗證場景1實際上是否按預期進行負載平衡?這將是我首選的方法,我只需要知道我可以信任它。

回答

0

是的,使用圖像jwilder/nginx-proxy,您可以添加許多工人,無需額外的配置。

參見文檔:https://hub.docker.com/r/jwilder/nginx-proxy/

It's很簡單,configurate,你可以用泊塢窗,撰寫規模擴展[的name_service] = [NUM]

的方式configurate是

version: '3' 
services: 
    nginx-proxy: 
    image: jwilder/nginx-proxy 
    container_name: nginx-proxy 
    ports: 
     - "80:80" 
    volumes: 
     - /var/run/docker.sock:/tmp/docker.sock:ro 

    node: 
    build: ./node 
    image: node_i 
    command: "npm start" 
    ports: 
     - '5000' 
    volumes: 
     - ./node:/src 
     - node_deps:/src/node_modules 
    environment: 
     - VIRTUAL_HOST=whoami.local 

要執行容器

$ docker-compose up 
$ docker-compose scale node=2 
$ curl -H "Host: whoami.local" localhost 
0

運行docker-compose up -d,然後使用docker-compose scale添加其他節點容器。

docker-compose scale node=5

這將旋轉起來,除了4升額外的節點的容器到現有容器。如果您然後運行:

docker-compose scale node=2

它將刪除節點容器3,讓你有2