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

Wes*_*den 6 traefik

我想使用Traefik的多个docker-compose项目作为反向代理.按照文档后我创建了两个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"
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

lde*_*dez 1

我想你想要的是:

http://example.com/
|-> app1 who serve http://example.com/foo
|-> app2 who serve http://example.com/bar
Run Code Online (Sandbox Code Playgroud)

为此,您必须使用另一个匹配器(例如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
Run Code Online (Sandbox Code Playgroud)

如果您只想扩展,则在 composefile 中只需要一项服务:

traefik.frontend.rule: Host:http://example.com/
|-> 10 x app (docker-compose scale app=10)
Run Code Online (Sandbox Code Playgroud)