簡體   English   中英

這個Docker / NGINX / Node設置是否實際上達到了預期的負載平衡?

[英]Is this Docker / NGINX / Node setup actually load balancing as expected?

我正在使用Docker / Node / Nginx設置Web服務器。 我一直在研究docker-compose中的設置,並提出了2個有效的解決方案-但就負載平衡而言,其中之一可能太好了而無法實現(因為它似乎使我節省了空間,而不必創建其他圖像/容器)。 我正在尋找驗證,如果我看到的實際上是合法的,並且負載均衡不需要多個圖像等。

解決方案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

nginx.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是否確實達到了預期的負載平衡? 這將是我的首選方法,我只需要知道我可以信任它即可。

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

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

它的配置非常簡單,您可以使用docker-compose scale [name_service] = [num]進行擴展

配置方式是

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

在場景1上運行docker-compose up -d 。然后使用docker-compose scale添加其他節點容器。

docker-compose scale node=5

除了現有容器之外,這還將啟動4個其他節點容器。 如果然后運行:

docker-compose scale node=2

它將刪除3個節點容器,剩下2個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM