Traefik Dashboard returns 在 Docker Swarm 部署中总是 404

Traefik Dashboard returns always 404 in Docker Swarm deployment

我正在尽最大努力通过 http://gateway.localhost/dashboard/ 获取 Traefik 仪表板,但我总是从 Traefik 收到 404 响应*。可以s.o。请查看我的堆栈文件并告诉我,为什么它不起作用?

我在我的服务器上用一个有效的域试过了,但它要么在那里工作,要么在本地主机上工作,Docker 桌面处于 Swarm 模式。可以通过正确的 http://localhost 访问 WhoAmI 服务。

docker stack deploy -c traefik.yml traefik

*404 也返回这些路由:http://gateway.localhost, http://gateway.localhost/dashboard

traefik.yml:

version: '3'

services:
  reverse-proxy:
    image: traefik:v2.5
    command:
      - "--providers.docker.swarmmode=true"
      - "--providers.docker.exposedByDefault=false"
      - "--api.dashboard=true"
      - "--entrypoints.web.address=:80"
      # Logging
      - "--accesslog"
      - "--log.level=INFO"
    ports:
      - "80:80"
    deploy:
      labels:
        #Because Swarm API does not support automatic way
        - "traefik.http.services.reverse-proxy.loadbalancer.server.port=80"
        #Dashboard
        - "traefik.http.routers.dashboard.rule=Host(`gateway.localhost`) && (PathPrefix(`/api`) || PathPrefix(`/dashboard`))"
        - "traefik.http.routers.dashboard.service=api@internal"
        - "traefik.http.routers.dashboard.entrypoints=web"
        - "traefik.http.routers.dashboard.middlewares=auth"
        - "traefik.http.middlewares.auth.basicauth.users=test:$$apr1$$H6uskkkW$$IgXLP6ewTrSuBkTrqE8wj/,test2:$$apr1$$d9hr9HBB$HxwgUir3HP4EsggP/QNo0"
      placement:
        constraints:
          - node.role == manager
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro

  whoami:
    image: traefik/whoami
    deploy:
      labels:
        - "traefik.enable=true"
        - "traefik.http.routers.whoami.rule=Host(`localhost`)"
        - "traefik.http.routers.whoami.entrypoints=web"
        - "traefik.http.services.whoami.loadbalancer.server.port=80"

您需要为带有 traefik.enable=true 标签的容器启用 traefik:

version: '3'

services:
  reverse-proxy:
    image: traefik:v2.5
    command:
      - "--providers.docker.swarmmode=true"
      - "--providers.docker.exposedByDefault=false"
      - "--api.dashboard=true"
      - "--entrypoints.web.address=:80"
      # Logging
      - "--accesslog"
      - "--log.level=INFO"
    ports:
      - "80:80"
    deploy:
      labels:
        ######## add the following label to enable traefik #######
        - "traefik.enable=true"
        #Because Swarm API does not support automatic way
        - "traefik.http.services.reverse-proxy.loadbalancer.server.port=80"
        #Dashboard
        - "traefik.http.routers.dashboard.rule=Host(`gateway.localhost`) && (PathPrefix(`/api`) || PathPrefix(`/dashboard`))"
        - "traefik.http.routers.dashboard.service=api@internal"
        - "traefik.http.routers.dashboard.entrypoints=web"
        - "traefik.http.routers.dashboard.middlewares=auth"
        - "traefik.http.middlewares.auth.basicauth.users=test:$$apr1$$H6uskkkW$$IgXLP6ewTrSuBkTrqE8wj/,test2:$$apr1$$d9hr9HBB$HxwgUir3HP4EsggP/QNo0"
      placement:
        constraints:
          - node.role == manager
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro

  whoami:
    image: traefik/whoami
    deploy:
      labels:
        - "traefik.enable=true"
        - "traefik.http.routers.whoami.rule=Host(`localhost`)"
        - "traefik.http.routers.whoami.entrypoints=web"
        - "traefik.http.services.whoami.loadbalancer.server.port=80"