gpt4 book ai didi

带有 docker-compose 的 Traefik 创建了太多的前端

转载 作者:行者123 更新时间:2023-12-04 15:51:28 24 4
gpt4 key购买 nike

我想将多个 docker-compose 项目与 Traefik 一起用作反向代理。在遵循文档之后,我创建了两个 docker-compose 文件;一个用于 Traefik,另一个用于具有 2 个“whoami”容器的示例项目。

这对后端很有用,但似乎 Traefik 为每个正在运行的容器创建了一个前端。因此,我定义了两个前端,而不是 2 个 whoami 容器的 1 个前端:“frontend-Host-whoami-localhost-0”和“frontend-Host-whoami-localhost-1”。

如果我扩展 whoami 服务(通过在 docker-compose.yaml 文件中复制它们的定义,或使用 docker-compose scale whoami=10 ),Traefik 将创建更多前端。

我只想要一个“Host:whoami.localhost”规则的前端,它指向一个后端,并连接了多个正在运行的容器。我怎样才能做到这一点?

traefik.toml:

defaultEntryPoints = ["http"]

[web]
address = ":8080"

[entryPoints]
[entryPoints.http]
address = ":80"

[docker]
endpoint = "unix:///var/run/docker.sock"
domain = "localhost"

docker-compose.yaml(用于 traefik):
version: "2"
services:
traefik:
container_name: traefik
image: traefik
networks:
- webgateway
ports:
- "80:80"
- "8080:8080"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ./traefik.toml:/traefik.toml
labels:
traefik.backend: web
traefik.frontend.rule: Host:monitor.localhost

networks:
webgateway:
driver: bridge

whoami/docker-compose.yaml:
version: "2"
services:
whoami:
image: emilevauge/whoami
networks:
- webgateway
labels:
traefik.backend: whoami
traefik.frontend.rule: Host:whoami.localhost

whoami_2:
image: emilevauge/whoami
networks:
- webgateway
labels:
traefik.backend: whoami
traefik.frontend.rule: Host:whoami.localhost

networks:
webgateway:
external:
name: traefikdocker_webgateway

最佳答案

我想你想要这样:

http://example.com/
|-> app1 who serve http://example.com/foo
|-> app2 who serve http://example.com/bar

为此,您必须使用另一个 matcher (例如 PathPrefix 示例):
traefik.frontend.rule: Host:http://example.com/; PathPrefix:/foo
|-> app1 who serve http://example.com/foo

traefik.frontend.rule: Host:http://example.com/; PathPrefix:/bar
|-> app2 who serve http://example.com/bar

如果你只是想扩展,你的 composefile 中只需要一个服务:
traefik.frontend.rule: Host:http://example.com/
|-> 10 x app (docker-compose scale app=10)

关于带有 docker-compose 的 Traefik 创建了太多的前端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47946997/

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