首页 文章

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

提问于
浏览
6

我想使用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"

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

1 回答

  • 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)
    

相关问题