繁体   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