我的nginx.conf文件当前有直接定义的路由:
worker_processes auto;
events { worker_connections 1024; }
http {
upstream wordSearcherApi {
least_conn;
server api1:61370 max_fails=3 fail_timeout=30s;
server api2:61370 max_fails=3 fail_timeout=30s;
server api3:61370 max_fails=3 fail_timeout=30s;
}
server {
listen 80;
server_name server_name 0.0.0.0;
location / {
proxy_pass http://wordSearcherApi;
}
}
}是否有任何方法只在docker-compose.yml中创建一个服务?当docker-compose up --scale api=3时,nginx是否实现自动负载平衡?
发布于 2018-05-07 04:47:04
使用当前的配置是不可能的,因为它是静态的。你有两个选择-
1.使用停靠引擎群模式-您可以定义副本&群内部DNS将自动平衡这些副本之间的负载。
参考文献- https://docs.docker.com/engine/swarm/
2.使用著名的Jwilder代理--这个映像侦听停靠套接字,在缩放容器时使用GO中的模板动态更改nginx信任。
发布于 2019-01-27 13:39:44
Nginx
在Nginx (normal,sans )中,动态向上流是可能的,但是有一些技巧和限制。
upstream指令,使用普通proxy_pass。
它提供了循环负载平衡和故障转移,但没有额外的指令功能,如权重,故障模式,超时等。proxy_pass,并且您必须提供一个resolver。
它迫使Nginx重新解析主机名(针对Docker网络的DNS)。location/proxy_pass行为。
在反向代理裸/的情况下,就像问题中一样,这并不重要。否则,您必须手动rewrite路径(请参阅下面的引用)。让我们看看它是如何工作的。
docker-compose.yml
version: '2.2'
services:
reverse-proxy:
image: nginx:1.15-alpine
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
ports:
- 8080:8080
app:
# A container that exposes an API to show its IP address
image: containous/whoami
scale: 4nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
access_log /dev/stdout;
error_log /dev/stderr;
server {
listen 8080;
server_name localhost;
resolver 127.0.0.11 valid=5s;
set $upstream app;
location / {
proxy_pass http://$upstream:80;
}
}
}然后..。
docker-compose up -d
seq 10 | xargs -I -- curl -s localhost:8080 | grep "IP: 172"...produces,如下所示,它指示请求分布在4个app容器中:
IP: 172.30.0.2
IP: 172.30.0.2
IP: 172.30.0.3
IP: 172.30.0.3
IP: 172.30.0.6
IP: 172.30.0.5
IP: 172.30.0.3
IP: 172.30.0.6
IP: 172.30.0.5
IP: 172.30.0.5参考文献:
特雷菲克
Traefik直接依赖Docker,并且可能是一个更简单和更可配置的选项。让我们看看它的行动。
docker-compose.yml
version: '2.2'
services:
reverse-proxy:
image: traefik
# Enables the web UI and tells Traefik to listen to docker
command: --api --docker
ports:
- 8080:80
- 8081:8080 # Traefik's web UI, enabled by --api
volumes:
# So that Traefik can listen to the Docker events
- /var/run/docker.sock:/var/run/docker.sock
app:
image: containous/whoami
scale: 4
labels:
- "traefik.frontend.rule=Host:localhost"然后..。
docker-compose up -d
seq 10 | xargs -I -- curl -s localhost:8080 | grep "IP: 172"...also产生的输出表明请求分布在4个app容器中:
IP: 172.31.0.2
IP: 172.31.0.5
IP: 172.31.0.6
IP: 172.31.0.4
IP: 172.31.0.2
IP: 172.31.0.5
IP: 172.31.0.6
IP: 172.31.0.4
IP: 172.31.0.2
IP: 172.31.0.5在Traefik (示例中的http://localhost:8081/dashboard/)中,您可以看到它识别了4个app容器:

参考文献:
https://stackoverflow.com/questions/50203408
复制相似问题