traefik2 服务发现 Gateway timeout 问题

在做 traefik v1 迁移到 v2 时遇到访问容器内服务 Gateway timeout 的问题,经过排查问题出在 network 的配置,在版本一中的 traefik docker-compose 配置如下

traefik v1 docker-compose.yml

version: '3'
 
services:
  reverse-proxy:
    container_name: traefik
    image: traefik:1.7.9
    restart: always
    ports:
      - 80:80
      - 443:443
      - 4399:4399
      - 4398:4398
    networks:
      - traefik
    command: traefik -c /etc/traefik.toml --sendAnonymousUsage=false
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./traefik-dev.toml:/etc/traefik.toml
      - ./ssl:/data/ssl
      - ./logs:/data/logs
      #- ./acme.json:/data/acme.json
 
# 记得先创建外部网卡
# docker network create traefik
networks:
  traefik:
    external: true

traefik v2 docker-compose.yml

version: '3'
 
services:
  reverse-proxy:
    image: traefik:v2.0.0
    container_name: "traefik"
    labels:
      - traefik.enable=true
      - traefik.docker.network=traefik
    ports:
      - 80:80
      - 443:443
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./traefik.toml:/traefik.toml
      - ./dynamic_conf.toml:/dynamic_conf.toml
      - ./ssl:/data/ssl/
    networks:
      - default
      - traefik

networks:
  traefik:
    external: true

traefik v1 版应用容器的配置

version: '3.5'
services:
  caixie.home:
    image: flashspys/nginx-static
    restart: always
    expose:
      - 80
    networks:
      - traefik
    labels:
      - "traefik.backend=caixie.home"
      - "traefik.docker.network=traefik"
      - "traefik.enable=true"
      - "traefik.passHostHeader=true"
      - "traefik.frontend.priority=10"
      - "traefik.port=80"
      - "traefik.frontend.entryPoints=https,http"
      - "traefik.frontend.rule=Host:caixie.top, caixie.ltd, www.caixie.top, www.caixie.ltd"
      - "traefik.frontend.headers.customResponseHeaders=Access-Control-Allow-Origin:*"
      - "traefik.frontend.headers.STSSeconds=315360000"
      - "traefik.frontend.headers.STSIncludeSubdomains=true"
      - "traefik.frontend.headers.STSPreload=true"
    volumes:
      - ./mime.types:/etc/nginx/mime.types
      - ./public:/static
networks:
  traefik:
    external: true

traefik v2 版应用容器的配置

version: '3.5'
services:
  caixie.home:
    build: .
    restart: 'unless-stopped'
    networks:
      - traefik
      - default
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.home.entrypoints=web, web-secure"
      - "traefik.http.routers.home.tls=true"
      - "traefik.http.routers.home.rule=Host(`www.caixie.top`, `caixie.top`, `caixie.ltd`, `www.caixie.ltd`)"
      - "traefik.http.routers.app_https.service=home"
      - "traefik.http.services.home.loadbalancer.server.port=80"
      - "traefik.docker.network=traefik"
    volumes:
      - ./public:/pub
networks:
  traefik:
     external: true

主要差别不大,主要是在 "traefik.http.services.home.loadbalancer.server.port=80" 的处理,当然v2 的配置更加简洁清晰

推荐阅读更多精彩内容