Wener Notes

Traefik

约 3 分钟阅读
bash
# 启动
# 配置文件支持 yaml、toml、json
# 人为配置推荐 yaml
traefik --configfile traefik.yaml

基础配置

yaml
providers:
  file:
    # 自动监听该目录配置
    directory: traefik.d
    watch: true

entryPoints:
  http:
    address: ':8080'
  https:
    address: ':8443'

规则

  • Host 匹配域名
  • HostRegexp
    • HostRegexp("a.example.com","{subdomain}.domain.com","{subdomain:[a-z]+}.domain.com")
    • 并不是真正的完全正则匹配 - 只有在 {} 内的才会处理
    • 使用的是 gorilla/mux 的 HostGitHub Filegorilla/mux/route.go#L293:17

配置

静态配置项

yaml
# 全局配置
global:
  checkNewVersion: false
  sendAnonymousUsage: false
# 服务端传输配置
serversTransport:
  insecureSkipVerify: false
  rootCAs: ['foobar', 'foobar']
  maxIdleConnsPerHost: 42
  forwardingTimeouts:
    dialTimeout: 42
    responseHeaderTimeout: 42
    idleConnTimeout: 42

# 默认
entryPoints:
  web:
    address: ':80'
  websecure:
    address: ':443'
# 配置发现
providers:
  providersThrottleDuration: 42
  file:
    directory: traefik.d
    watch: true
    filename: traefik.yaml
    debugLogGeneratedTemplate: true
# 启用 API
api:
  # 不安全模式可以直接访问
  # 安全模式会定义 api@internal 内部服务,需要自行配置路由
  insecure: true
  dashboard: true
  # 添加 /debug 端口
  debug: true
# 指标监控
metrics:
  prometheus:
    buckets: []
    addEntryPointsLabels: true
    addServicesLabels: true
    entryPoint: traefik
# 启用 PING - 用于健康检查 - 端口为 /ping
ping:
  entryPoint: traefik
# 日志配置
log:
  level: INFO
  filePath: traefik.log
  # common/json
  format: common
# 访问日志配置
accessLog:
  filePath: access.log
  format: json
  # 在内存缓冲的行数
  bufferingSize: 100
  # 落日志的条件 - 或 关系
  filters:
    statusCodes: [400, 401]
    # 重试
    retryAttempts: true
    # 至少 10ms
    minDuration: 10
  # 字段控制
  fields:
    # 保留字段
    defaultMode: keep
    names:
      # 丢弃字段
      ClientUsername: drop
      name1: foobar
    headers:
      # 将字段值替换为 redacted
      'User-Agent': 'redact'

# 调用跟踪
tracing:
  serviceName: foobar
  spanNameLimit: 42
  zipkin:
    httpEndpoint: foobar
    sameSpan: true
    id128Bit: true
    sampleRate: 42
# 主机域名解析
hostResolver:
  cnameFlattening: true
  resolvConfig: foobar
  resolvDepth: 5
# 证书解析
certificatesResolvers:
  CertificateResolver0:
    # let‘s encrypt
    acme:
      email: foobar
      caServer: foobar
      storage: foobar
      keyType: foobar
      dnsChallenge:
        provider: foobar
        delayBeforeCheck: 42
        resolvers:
          - foobar
          - foobar
        disablePropagationCheck: true
      httpChallenge:
        entryPoint: foobar
      tlsChallenge: {}

PowerDNS 证书

yaml
certificatesResolvers:
  pdns:
    acme:
      dnsChallenge:
        # 通过环境变量配置链接信息 PDNS_API_KEY, PDNS_API_URL
        # 环境变量也可以使用 *_FILE 指向文件来配置
        provider: pdns

使用

yaml
http:
  routers:
    traefik:
      entryPoints: [https]
      rule: Host(`traefik.example.com`)
      service: api@internal
      tls:
        # 引用定义
        certResolver: pdns
        # 使用通配符证书
        domains:
          - main: example.com
            sans:
              - '*.example.com'

Docker 配置发现

yaml
# 运行 docker 添加 -v /var/run/docker.sock:/var/run/docker.sock
providers:
  docker:
    endpoint: 'unix:///var/run/docker.sock'
    # 是否默认暴露服务 - 正常需要 traefik.enable=true
    exposedByDefault: false
    # 默认的映射规则
    # 支持 https://masterminds.github.io/sprig 函数
    # defaultRule: "Host(`{{ .Name }}.{{ index .Labels \"customLabel\"}}`)"
bash
docker run --rm -it \
  -l traefik.enable=true \
  -l traefik.http.routers.my-container.rule=Host(`mydomain.com`) \
  -l traefik.http.services.my-container-service.loadbalancer.server.port=80
  --name web nginx

# 指定端口 - 多个逗号分割
# traefik.http.routers.my-container.entrypoints=https
# 启用 tls
# traefik.http.routers.my-container.tls=true

FAQ

traefik v1 vs v2

  • 新特性
    • 支持带 SNI 的 TCP 路由和多协议端口
    • 支持 TCP 和 HTTP 在同一个端口 - 使用 SNI 区分
    • 新的结构 Routers, Middlewares, Services
    • 新的监控页面
    • 带权重的 AB 发布
    • 服务负载均衡支持镜像请求 - 将请求发送到多个端忽略返回结果
  • 迁移点
    • 没有了 Frontends 和 Backends 概念,新的核心组件为 Routers, Middlewares, Services
    • TLS 每个路由动态配置 - v1 为全局静态
    • HTTP 调整 HTTPS 现在是路由 - v1 需要在入口配置
    • LetsEncrypt 配置调整
    • 日志不在作为全局配置,配置在 log 区块下
    • 移除了所有全局设置
    • v2 的 provider 支持较少 - 会逐渐完善
  • 参考

关联信息

反向链接、本文链接的其他页面和外部资料。

References

GitHub4 条
其他外链5 条
最近更新commit c6bc0d8Edit

On this page