我正在使用Docker / Node / Nginx建立一个Web服务器。我一直在使用docker-compose中的设置,并提出了两个有效的解决方案 - 但在负载平衡方面,其中一个可能太好了,不可能是真的(因为它似乎允许我节省空间不必创建额外的图像/容器)。我正在寻找验证,如果我所看到的实际上是合法的,并且多个图像等不是负载平衡的要求。
解决方案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_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 {
...
}
}
解决方案2(附加图片):
document.getElementById('guessed-letters').innerHTML = key_press;
nginx.conf
document.getElementById('guessed-letters').innerHTML += key_press;
这两种方案都可以在localhost&上完美地加载应用程序。在指定的端口上。我确信方案2是正确的负载平衡,因为它模仿传统的多服务器方案。
有什么办法可以验证方案1是否按预期实际负载均衡?这将是我的首选方法,我只需要知道我可以相信它。
答案 0 :(得分:0)
是的,使用图像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 :(得分:0)
在方案1上运行docker-compose up -d
。然后使用docker-compose scale添加其他节点容器。
docker-compose scale node=5
除现有容器外,这将增加4个额外的节点容器。如果你然后运行:
docker-compose scale node=2
它会删除3个节点容器,留下2个。