gpt4 book ai didi

docker-compose --scale X nginx.conf 配置

转载 作者:行者123 更新时间:2023-12-01 23:41:50 25 4
gpt4 key购买 nike

我的 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 是否做自动负载均衡?

最佳答案

Nginx

动态上游在 Nginx 中是可能的(正常,无 Plus),但有技巧和限制。

  1. 您放弃 upstream 指令并使用普通的 proxy_pass

    它提供了循环负载平衡和故障转移,但没有 the directive 的额外功能例如权重、故障模式、超时等。

  2. 您的上游主机名必须通过变量传递给 proxy_pass,并且您必须提供解析器

    它强制 Nginx 重新解析主机名(针对 Docker 网络的 DNS)。

  3. 您会丢失与尾部斜杠相关的location/proxy_pass行为。

    在反向代理到裸 / 的情况下,就像问题中那样,这并不重要。否则,您必须手动重写路径(请参阅下面的引用资料)。

让我们看看它是如何工作的。

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: 4

nginx.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"

...生成类似以下内容的内容,表明请求分布在 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

引用文献:

  1. Nginx with dynamic upstreams
  2. Using Containers to Learn Nginx Reverse Proxy
  3. Dynamic Nginx configuration for Docker with Python

Traefik

Traefik 直接依赖于 Docker API,可能是一个更简单、更可配置的选项。让我们看看它的实际效果。

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"

...还产生一些输出,表明请求分布在 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 UI(示例中的 http://localhost:8081/dashboard/)中,您可以看到它识别了 4 个 app 容器:

Backends

引用文献:

  1. The Traefik Quickstart (Using Docker)

关于docker-compose --scale X nginx.conf 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50203408/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com