Skip to content

向 Traefik 提供动态配置

向 Traefik 提供动态(路由)配置

动态配置(现在也称为路由配置)定义了 Traefik 如何将传入请求路由到正确的 service。这与安装配置(旧称静态配置)不同,后者设置 Traefik 的核心组件和 providers。

根据你的环境和偏好,有几种方式可以提供此路由配置:

  • File 或结构化 Provider:使用 TOML 或 YAML 文件。
  • Docker 和 ECS Providers:使用容器标签。
  • Kubernetes Providers:使用注解。
  • KV Providers:使用键值对。
  • 其他 Providers(Consul、Nomad 等):使用 tags。

使用 File Provider

File provider 允许你使用 TOML 或 YAML 语法在静态文件中定义路由配置。此方法非常适合无法自动发现 service 或你希望手动管理配置的环境。

启用 File Provider

要启用 File provider,请将以下内容添加到你的 Traefik 安装配置中:

YAML

yaml
providers:
  file:
    directory: "/path/to/dynamic/conf"

TOML

toml
[providers.file]
  directory = "/path/to/dynamic/conf"

使用 file provider 声明 routers 和 services 的示例

File (YAML)

yaml
http:
  routers:
    my-router:
      rule: "Host(`example.com`)"
      service: my-service

  services:
    my-service:
      loadBalancer:
        servers:
          - url: "http://localhost:8080"

File (TOML)

toml
[http]
  [http.routers]
    [http.routers.my-router]
      rule = "Host(`example.com`)"
      service = "my-service"

  [http.services]
    [http.services.my-service.loadBalancer]
      [[http.services.my-service.loadBalancer.servers]]
        url = "http://localhost:8080"

在 Docker 和 ECS 中使用 Labels

当使用 Docker 或 Amazon ECS 时,你可以使用容器标签定义路由配置。此方法允许 Traefik 自动发现 service 并应用配置,无需其他文件。

Docker 示例

部署 Docker 容器时,可以指定标签以定义路由规则和 services:

yaml
services:
  my-service:
    image: my-image
    labels:
      - "traefik.http.routers.my-router.rule=Host(`example.com`)"
      - "traefik.http.services.my-service.loadbalancer.server.port=80"

ECS 示例

在 ECS 中,你可以使用 task definition 标签来实现相同的效果:

json
{
  "containerDefinitions": [
    {
      "name": "my-service",
      "image": "my-image",
      "dockerLabels": {
        "traefik.http.routers.my-router.rule": "Host(`example.com`)",
        "traefik.http.services.my-service.loadbalancer.server.port": "80"
      }
    }
  ]
}

使用 Kubernetes Providers

对于 Kubernetes providers,你可以使用原生 Ingress 或自定义资源(如 IngressRoute)配置 Traefik。Ingress 或 IngressRoute 定义中的注解允许你定义路由规则和中间件设置。例如:

Kubernetes 示例

yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: whoami
  namespace: apps
  annotations:
    traefik.ingress.kubernetes.io/router.entrypoints: websecure
    traefik.ingress.kubernetes.io/router.priority: "42"
    traefik.ingress.kubernetes.io/router.tls: "true"
    traefik.ingress.kubernetes.io/router.tls.options: apps-opt@kubernetescrd
spec:
  rules:
    - host: my-domain.example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: whoami
                namespace: apps
                port:
                  number: 80
  tls:
    - secretName: supersecret

在 KV Providers 中使用键值对

对于 KV providers,你可以使用键值对配置 Traefik。

示例

etcd

bash
# 设置路由器规则
etcdctl put /traefik/http/routers/my-router/rule "Host(`example.com`)"
# 定义与路由器关联的 service
etcdctl put /traefik/http/routers/my-router/service "my-service"
# 设置 service 的后端服务器 URL
etcdctl put /traefik/http/services/my-service/loadbalancer/servers/0/url "http://localhost:8080"

Redis

bash
# 设置路由器规则
redis-cli set traefik/http/routers/my-router/rule "Host(`example.com`)"
# 定义与路由器关联的 service
redis-cli set traefik/http/routers/my-router/service "my-service"
# 设置 service 的后端服务器 URL
redis-cli set traefik/http/services/my-service/loadbalancer/servers/0/url "http://localhost:8080"

ZooKeeper

bash
# 设置路由器规则
create /traefik/http/routers/my-router/rule "Host(`example.com`)"
# 定义与路由器关联的 service
create /traefik/http/routers/my-router/service "my-service"
# 设置 service 的后端服务器 URL
create /traefik/http/services/my-service/loadbalancer/servers/0/url "http://localhost:8080"

在其他 Providers 中使用 Tags

对于不支持标签的 providers,如 Consul 和 Nomad,你可以使用 tags 来提供路由配置。

示例

Consul / Nomad

json
{
  "Name": "my-service",
  "Tags": [
    "traefik.http.routers.my-router.rule=Host(`example.com`)",
    "traefik.http.services.my-service.loadbalancer.server.port=80"
  ],
  "Address": "localhost",
  "Port": 8080
}

基于 MIT 协议发布