跳到正文
Træfɪk

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

动态配置现在也称为路由配置,它定义 Traefik 如何将传入请求路由到正确的服务。这与安装配置(以前称为静态配置)不同,后者用于设置 Traefik 的核心组件和提供者。

根据运行环境和偏好,可以通过以下几种方式提供路由配置:

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

使用文件提供者

文件提供者支持在静态文件中使用 TOML 或 YAML 语法定义路由配置。这种方法适合无法自动发现服务的环境,也适合希望手动管理配置的情况。

启用文件提供者

要启用文件提供者,请在 Traefik 安装配置中添加以下内容:

providers:
  file:
    directory: "/path/to/dynamic/conf"
[providers.file]
  directory = "/path/to/dynamic/conf"
使用文件提供者声明路由器和服务的示例
http:
  routers:
    my-router:
      rule: "Host(`example.com`)"
      service: my-service

  services:
    my-service:
      loadBalancer:
        servers:
          - url: "http://localhost:8080"
[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 中使用容器标签

使用 Docker 或 Amazon ECS 时,可以通过容器标签定义路由配置。这样 Traefik 就能自动发现服务并应用配置,无需额外文件。

Docker 示例

部署 Docker 容器时,可以通过标签指定路由规则和服务:

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 中,可以使用任务定义标签达到相同效果:

{
  "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 提供者

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

Kubernetes 示例
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
                port:
                  number: 80
  tls:
    - secretName: supersecret    

在 KV 提供者中使用键值对

对于 KV 提供者,可以使用键值对配置 Traefik。

示例
# Set a router rule
etcdctl put /traefik/http/routers/my-router/rule "Host(`example.com`)"
# Define the service associated with the router
etcdctl put /traefik/http/routers/my-router/service "my-service"
# Set the backend server URL for the service
etcdctl put /traefik/http/services/my-service/loadbalancer/servers/0/url "http://localhost:8080"
# Set a router rule
redis-cli set traefik/http/routers/my-router/rule "Host(`example.com`)"
# Define the service associated with the router
redis-cli set traefik/http/routers/my-router/service "my-service"
# Set the backend server URL for the service
redis-cli set traefik/http/services/my-service/loadbalancer/servers/0/url "http://localhost:8080"
# Set a router rule
create /traefik/http/routers/my-router/rule "Host(`example.com`)"
# Define the service associated with the router
create /traefik/http/routers/my-router/service "my-service"
# Set the backend server URL for the service
create /traefik/http/services/my-service/loadbalancer/servers/0/url "http://localhost:8080"

在其他提供者中使用标签

对于 Consul 和 Nomad 等不支持容器标签的提供者,可以使用标签(tag)提供路由配置。

示例
{
  "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
}

版权所有 © 头文件软件工作室

文档由 HeadFile Studio 翻译。

Traefik v3.7 中文快照 · 原文版权归 Traefik Labs 及贡献者所有 · MIT 许可证