Skip to content

Traefik 使用 KV 存储的路由配置

Traefik 与 KV 存储

配置示例

配置 KV Store 并部署/暴露一个 Service

启用 KV store provider(示例:Consul)

YAML

yaml
providers:
  consul:
    endpoints:
      - "127.0.0.1:8500"

TOML

toml
[providers.consul]
  endpoints = ["127.0.0.1:8500"]

CLI

bash
--providers.consul.endpoints=127.0.0.1:8500

在 KV 存储中设置键(示例:Consul)

bash
consul kv put traefik/http/routers/my-router/rule "Host(`example.com`)"
consul kv put traefik/http/routers/my-router/service "my-service"
consul kv put traefik/http/services/my-service/loadbalancer/servers/0/url "http://127.0.0.1:8080"

为 Service 指定自定义端口

http://example.com 的请求转发到 http://127.0.0.1:12345

bash
consul kv put traefik/http/routers/my-router/rule "Host(`example.com`)"
consul kv put traefik/http/routers/my-router/service "my-service"
consul kv put traefik/http/services/my-service/loadbalancer/servers/0/url "http://127.0.0.1:12345"

指定多个 router 和 service

将请求转发到多个 service 需要定义多个 routers 和 services。

bash
consul kv put traefik/http/routers/www-router/rule "Host(`example-a.com`)"
consul kv put traefik/http/routers/www-router/service "www-service"
consul kv put traefik/http/services/www-service/loadbalancer/servers/0/url "http://127.0.0.1:8000"

consul kv put traefik/http/routers/admin-router/rule "Host(`example-b.com`)"
consul kv put traefik/http/routers/admin-router/service "admin-service"
consul kv put traefik/http/services/admin-service/loadbalancer/servers/0/url "http://127.0.0.1:9000"

配置选项

Keys

键不区分大小写。

HTTP

Routers

字符 @ 在路由器名称 <router_name> 中不允许。

Key (Path)描述
traefik/http/routers/<router_name>/rule路由规则。Host(example.com)
traefik/http/routers/<router_name>/ruleSyntax规则语法(已弃用)。v3
traefik/http/routers/<router_name>/entrypoints/0entry points。web
traefik/http/routers/<router_name>/entrypoints/1entry points。websecure
traefik/http/routers/<router_name>/middlewares/0中间件。auth
traefik/http/routers/<router_name>/middlewares/1中间件。prefix
traefik/http/routers/<router_name>/serviceservice 名称。myservice
traefik/http/routers/<router_name>/tls启用 TLS。true
traefik/http/routers/<router_name>/tls/certresolver证书解析器。myresolver
traefik/http/routers/<router_name>/tls/domains/0/mainTLS 主域名。example.org
traefik/http/routers/<router_name>/tls/domains/0/sans/0TLS SANs。test.example.org
traefik/http/routers/<router_name>/tls/domains/0/sans/1TLS SANs。dev.example.org
traefik/http/routers/<router_name>/tls/optionsTLS 选项。foobar
traefik/http/routers/<router_name>/observability/accesslogs启用访问日志。true
traefik/http/routers/<router_name>/observability/metrics启用指标。true
traefik/http/routers/<router_name>/observability/tracing启用链路追踪。true
traefik/http/routers/<router_name>/priority优先级。42

Services

字符 @ 在 service 名称 <service_name> 中不允许。

Key (Path)描述
traefik/http/services/myservice/loadbalancer/servers/0/url服务器 URL。http://<ip-server-1>:<port-server-1>/
traefik/http/services/myservice/loadbalancer/servers/0/preservePath保留路径。true
traefik/http/services/myservice/loadbalancer/servers/0/weight服务器权重。1
traefik/http/services/myservice/loadbalancer/serverstransport引用 ServersTransport 资源。foobar@file
traefik/http/services/myservice/loadbalancer/passhostheader转发 Host 头。true
traefik/http/services/myservice/loadbalancer/healthcheck/...健康检查选项(headers、hostname、interval、path、method、status、port、scheme、timeout、followredirects)。
traefik/http/services/myservice/loadbalancer/sticky/...粘性会话选项。
traefik/http/services/myservice/loadbalancer/responseforwarding/flushinterval响应刷新间隔。10
traefik/http/services/<service_name>/mirroring/...镜像选项。
traefik/http/services/<service_name>/weighted/...WRR 选项。
traefik/http/services/<service_name>/failover/...故障转移选项。

Middleware

traefik/http/middlewares/mymiddleware/middleware_type/middleware_option - 中间件类型(如 forwardAuth、headers 等)和要设置的中间件选项(例如对于 addPrefix 中间件:prefix)。

字符 @ 在中间件名称中不允许。

声明冲突

如果你使用相同的名称但不同的参数声明多个中间件,则中间件声明失败。

ServerTransport

traefik/http/serversTransports/<serversTransportName>/st_option - ServersTransport 选项(如 maxIdleConnsPerHost)。

TCP

你可以使用 KV 声明 TCP Routers 和/或 Services。

Routers

Key (Path)描述
traefik/tcp/routers/mytcprouter/entrypoints/0entry points。ep1
traefik/tcp/routers/mytcprouter/entrypoints/1entry points。ep2
traefik/tcp/routers/my-router/rule规则。HostSNI(example.com)
traefik/tcp/routers/mytcprouter/serviceservice 名称。myservice
traefik/tcp/routers/mytcprouter/tls启用 TLS。true
traefik/tcp/routers/mytcprouter/tls/certresolver证书解析器。myresolver
traefik/tcp/routers/mytcprouter/tls/domains/0/mainTLS 主域名。example.org
traefik/tcp/routers/mytcprouter/tls/domains/0/sans/0TLS SANs。test.example.org
traefik/tcp/routers/mytcprouter/tls/optionsTLS 选项。foobar
traefik/tcp/routers/mytcprouter/tls/passthrough透传 TLS。true
traefik/tcp/routers/mytcprouter/priority优先级。42

Services

Key (Path)描述
traefik/tcp/services/mytcpservice/loadbalancer/servers/0/address服务器地址。xx.xx.xx.xx:xx
traefik/tcp/services/mytcpservice/loadbalancer/servers/0/tls与后端通信时使用 TLS。true
traefik/tcp/services/myservice/loadbalancer/serverstransport引用 ServersTransport 资源。foobar@file
traefik/tcp/services/<service_name>/weighted/services/0/nameWRR service 名称。foobar
traefik/tcp/services/<service_name>/weighted/services/0/weightWRR 权重。42

Middleware

例如,要声明名为 test-inflightconnInFlightConn 中间件,你可以写 traefik/tcp/middlewares/test-inflightconn/inflightconn/amount=10

ServerTransport

traefik/tcp/serversTransports/<serversTransportName>/st_option - TCP ServersTransport 选项。

UDP

你可以使用 KV 声明 UDP Routers 和/或 Services。

Routers

Key (Path)描述
traefik/udp/routers/myudprouter/entrypoints/0entry points。foobar
traefik/udp/routers/myudprouter/serviceservice 名称。foobar

Services

Key (Path)描述
traefik/udp/services/loadBalancer/servers/<n>/address服务器地址。foobar
traefik/udp/services/weighted/services/0/nameWRR service 名称。foobar
traefik/udp/services/weighted/servers/0/weightWRR 权重。42

TLS

TLS Options

使用 KV provider,你可以使用 tls/options 键配置 TLS 连接的一些参数。

例如,你可以这样定义基本设置:

Key (Path)描述
traefik/tls/options/Options0/alpnProtocols/0ALPN 协议。foobar
traefik/tls/options/Options0/cipherSuites/0密码套件。foobar
traefik/tls/options/Options0/clientAuth/caFiles/0客户端 CA 证书。foobar
traefik/tls/options/Options0/disableSessiontickets禁用 session tickets。true

TLS 默认生成证书

你可以配置 Traefik 使用 ACME provider(如 Let's Encrypt)生成默认证书。

解析默认证书的配置应在 TLS store 中定义。

Key (Path)描述
traefik/tls/stores/Store0/defaultGeneratedCert/domain/main默认证书主域名。foobar
traefik/tls/stores/Store0/defaultGeneratedCert/domain/sans/0默认证书 SANs。foobar
traefik/tls/stores/Store0/defaultGeneratedCert/domain/sans/1默认证书 SANs。foobar
traefik/tls/stores/Store0/defaultGeneratedCert/resolverACME 解析器名称。foobar

在生产环境使用 Traefik OSS?

如果你在工作中使用 Traefik,可以考虑为其添加企业级 API 网关能力或获取 Traefik OSS 的商业支持。

基于 MIT 协议发布