我想将多个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将创建更多的前端。
我只想要#34;主持人的一个前端: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 /搬运工-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
答案 0 :(得分:1)
我想你想要那个:
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)