traefik:获得与服务副本一样多的前端(预计只有一个)

时间:2019-06-09 17:09:34

标签: docker docker-compose label docker-swarm traefik

我尝试为一个集群群集设置两个docker-compose堆栈:一个用于traefik,另一个用于httpd。 它工作正常,除了:我有一个名为http的后​​端,其中包含4个副本。但我得到4个前端,每个前端都具有相同的路由规则。

这是我的docker-compose文件

version: '3.7'

networks:
  default:
    external: true
    name: common

services:
  httpd:
    image: httpd:2.4 # A container that exposes an API to show its IP address
    labels:
      traefik.frontend.rule: "Host:httpd.docker.localhost"
      traefik.enable: "true"
      traefik.backend: "http"
    deploy:
      replicas: 4
    networks:
      - default

version: '3.7'

networks:
  common:
    name: common 
    driver: overlay
    attachable: true


services:
  reverse-proxy:
    image: traefik # The official Traefik docker image
    command: --api --docker # Enables the web UI and tells Traefik to listen to docker
    ports:
      - "80:80"     # The HTTP port
      - "8080:8080" # The Web UI (enabled by --api)
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock # 
      - ./traefik.toml:/etc/traefik/traefik.toml # 
    networks:
      - common 

这是我的traefik.toml

debug = true

################################################################
# API and dashboard configuration
################################################################
[api]

#defaultEntryPoints = ["http", "https", "ws", "wss"]

################################################################
# Web configuration backend
################################################################
[web]
address = ":8080"
[web.auth.basic]
# User: toto | Password: password
users = ["toto:$2y$05$zNs3wc5UPB4su8vbFugVPuKEaLJXMf5Z.9hAI1ulJpBbhbBprfppO"]

################################################################
# Entry-points configuration
################################################################
#[entryPoints]
#  [entryPoints.http]
#    address = ":80"
#    [entryPoints.http.redirect]
#      entryPoint = "https"
#  [entryPoints.https]
#    address = ":443"
#    [entryPoints.https.tls]

################################################################
# Docker configuration backend
################################################################
[docker]
domain = "docker.local"
watch = true
#exposedbydefault = false

这就是我得到的

4Frontends
frontend-Host-httpd-docker-localhost-0
Route Rule
Host:httpd.docker.localhost
Entry Points
http
Backend
backend-http
frontend-Host-httpd-docker-localhost-1
Route Rule
Host:httpd.docker.localhost
Entry Points
http
Backend
backend-http
frontend-Host-httpd-docker-localhost-2
Route Rule
Host:httpd.docker.localhost
Entry Points
http
Backend
backend-http
frontend-Host-httpd-docker-localhost-3
Route Rule
Host:httpd.docker.localhost
Entry Points
http
Backend
backend-http

期望:仅获得一个前端,而同一路由规则仅到达一个后端(因为我只有一个后端,这对我有好处)

忘了:四个具有相同路由规则的前端仅连接到一个后端(因为我只有一个后端,这对我有好处)

1 个答案:

答案 0 :(得分:0)

version: '3.7'

networks:
  default:
    external: true
    name: common

services:
  httpd:
    image: httpd:2.4 # A container that exposes an API to show its IP address
    deploy:
      replicas: 4
      labels:
        traefik.frontend.rule: "Host:httpd.docker.localhost"
        traefik.enable: "true"
        traefik.backend: "http"
    networks:
      - default
version: '3.7'

networks:
  common:
    name: common
    driver: overlay
    attachable: true


services:
  reverse-proxy:
    image: traefik:v1.7.12
    ports:
      - "80:80"
      - "443:443"
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./traefik.toml:/etc/traefik/traefik.toml
    networks:
      - common
&*