跳到正文
Træfɪk

Traefik 与 KV 存储

配置示例

配置 KV 存储并部署或暴露一个服务

启用 KV 存储提供者(示例:Consul)

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

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

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"
为服务指定自定义端口

将发往 http://example.com 的请求转发至 http://127.0.0.1:12345:

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"
指定多个路由器和服务

要将请求转发到多个服务,需要定义多个路由器和服务。

在此示例中,发往 http://example-a.com 的请求会转发至 http://127.0.0.1:8000,同时,发往 http://example-b.com 的请求会转发至 http://127.0.0.1:9000:

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"

配置选项

键

键不区分大小写。

HTTP

路由器

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

键(路径) 说明 值
traefik/http/routers/<router_name>/rule 更多信息请参阅规则。 Host(`example.com`)
traefik/http/routers/<router_name>/ruleSyntax 更多信息请参阅规则。
RuleSyntax 选项已弃用,将在下一个主版本中移除。
请勿使用此字段,并将路由器规则重写为 v3 语法。
v3
traefik/http/routers/<router_name>/entrypoints/0 更多信息请参阅入口点。 web
traefik/http/routers/<router_name>/entrypoints/1 更多信息请参阅入口点。 websecure
traefik/http/routers/<router_name>/middlewares/0 更多信息请参阅中间件概述。 auth
traefik/http/routers/<router_name>/middlewares/1 prefix
traefik/http/routers/<router_name>/service 更多信息请参阅服务。 myservice
traefik/http/routers/<router_name>/tls 更多信息请参阅 TLS。 true
traefik/http/routers/<router_name>/tls/certresolver 更多信息请参阅 certResolver。 myresolver
traefik/http/routers/<router_name>/tls/domains/0/main 更多信息请参阅域名。 example.org
traefik/http/routers/<router_name>/tls/domains/0/sans/0 更多信息请参阅域名。 test.example.org
traefik/http/routers/<router_name>/tls/domains/0/sans/1 更多信息请参阅域名。 dev.example.org
traefik/http/routers/<router_name>/tls/options 更多信息请参阅 TLS 选项。 foobar
traefik/http/routers/<router_name>/observability/accesslogs accessLogs 选项控制路由器是否生成访问日志。 true
traefik/http/routers/<router_name>/observability/metrics metrics 选项控制路由器是否生成指标。 true
traefik/http/routers/<router_name>/observability/tracing tracing 选项控制路由器是否生成追踪数据。 true
traefik/http/routers/<router_name>/priority 更多信息请参阅优先级。 42

服务

服务名称 <service_name> 中不允许使用字符 @。

键(路径) 说明 值
traefik/http/services/<service_name>/loadbalancer/servers/0/url 更多信息请参阅服务器。 http://<ip-server-1>:<port-server-1>/
traefik/http/services/<service_name>/loadbalancer/servers/0/preservePath 更多信息请参阅服务器。 true
traefik/http/services/<service_name>/loadbalancer/servers/0/weight 更多信息请参阅服务器。 1
traefik/http/services/<service_name>/loadbalancer/serverstransport 允许引用通过 File 提供者或 Kubernetes CRD 提供者定义的 ServersTransport 资源。
更多信息请参阅 serverstransport。
foobar@file
traefik/http/services/<service_name>/loadbalancer/passhostheader 更多信息请参阅服务。 true
traefik/http/services/<service_name>/loadbalancer/healthcheck/headers/X-Foo 更多信息请参阅健康检查。 foobar
traefik/http/services/<service_name>/loadbalancer/healthcheck/hostname 更多信息请参阅健康检查。 example.org
traefik/http/services/<service_name>/loadbalancer/healthcheck/interval 更多信息请参阅健康检查。 10
traefik/http/services/<service_name>/loadbalancer/healthcheck/path 更多信息请参阅健康检查。 /foo
traefik/http/services/<service_name>/loadbalancer/healthcheck/method 更多信息请参阅健康检查。 foobar
traefik/http/services/<service_name>/loadbalancer/healthcheck/status 更多信息请参阅健康检查。 42
traefik/http/services/<service_name>/loadbalancer/healthcheck/port 更多信息请参阅健康检查。 42
traefik/http/services/<service_name>/loadbalancer/healthcheck/scheme 更多信息请参阅健康检查。 http
traefik/http/services/<service_name>/loadbalancer/healthcheck/timeout 更多信息请参阅健康检查。 10
traefik/http/services/<service_name>/loadbalancer/sticky 更多信息请参阅服务。 true
traefik/http/services/<service_name>/loadbalancer/sticky/cookie/httponly 更多信息请参阅服务。 true
traefik/http/services/<service_name>/loadbalancer/sticky/cookie/name 更多信息请参阅服务。 foobar
traefik/http/services/<service_name>/loadbalancer/sticky/cookie/path 更多信息请参阅服务。 /foobar
traefik/http/services/<service_name>/loadbalancer/sticky/cookie/secure 更多信息请参阅服务。 true
traefik/http/services/<service_name>/loadbalancer/sticky/cookie/samesite 更多信息请参阅服务。 none
traefik/http/services/<service_name>/loadbalancer/sticky/cookie/maxage 更多信息请参阅服务。 42
traefik/http/services/myservice/loadbalancer/responseforwarding/flushinterval 更多信息请参阅服务。 10
traefik/http/services/<service_name>/mirroring/service 更多信息请参阅服务。 foobar
traefik/http/services/<service_name>/mirroring/mirrors/<n>/name 更多信息请参阅服务。 foobar
traefik/http/services/<service_name>/mirroring/mirrors/<n>/percent 更多信息请参阅服务。 42
traefik/http/services/<service_name>/weighted/services/<n>/name 更多信息请参阅服务。 foobar
traefik/http/services/<service_name>/weighted/services/<n>/weight 更多信息请参阅服务。 42
traefik/http/services/<service_name>/weighted/sticky/cookie/name 更多信息请参阅服务。 foobar
traefik/http/services/<service_name>/weighted/sticky/cookie/secure 更多信息请参阅服务。 true
traefik/http/services/<service_name>/weighted/sticky/cookie/samesite 更多信息请参阅服务。 none
traefik/http/services/<service_name>/weighted/sticky/cookie/httpOnly 更多信息请参阅服务。 true
traefik/http/services/<service_name>/weighted/sticky/cookie/maxage 更多信息请参阅服务。 42
traefik/http/services/<service_name>/failover/fallback 更多信息请参阅故障转移。 backup
traefik/http/services/<service_name>/failover/healthcheck 更多信息请参阅故障转移。 {}
traefik/http/services/<service_name>/failover/service 更多信息请参阅故障转移。 main

中间件

配置选项
键(路径) 说明 值
traefik/http/middlewares/<middleware_name>/middleware_type/middleware_option 其中,middleware_type 是中间件类型(例如 forwardAuth、headers 等),
middleware_option 是要设置的中间件选项(例如 addPrefix 中间件的 prefix)。
有关可用中间件的更多信息,请参阅专门的中间件章节。
foobar

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

声明冲突

如果声明多个名称相同但参数不同的中间件,该中间件将无法声明。

ServerTransport

配置选项
键(路径) 说明 值
traefik/http/serversTransports/<servers_transport_name>/st_option 其中,st_option 是要设置的 ServerTransport 选项(例如 maxIdleConnsPerHost)。
有关可用选项的更多信息,请参阅专门的 ServerTransport 章节。
ServerTransport 选项

TCP

可以使用 KV 声明 TCP 路由器和/或服务。

路由器

键(路径) 说明 值
traefik/tcp/routers/<router_name>/entrypoints/0 更多信息请参阅入口点。 ep1
traefik/tcp/routers/<router_name>/entrypoints/1 更多信息请参阅入口点。 ep2
traefik/tcp/routers/my-router/rule 更多信息请参阅入口点。 HostSNI(`example.com`)
traefik/tcp/routers/<router_name>/service 更多信息请参阅服务。 myservice
traefik/tcp/routers/<router_name>/tls 更多信息请参阅 TLS。 true
traefik/tcp/routers/<router_name>/tls/certresolver 更多信息请参阅 certResolver。 myresolver
traefik/tcp/routers/<router_name>/tls/domains/0/main 更多信息请参阅 TLS。 example.org
traefik/tcp/routers/<router_name>/tls/domains/0/sans/0 更多信息请参阅 TLS。 test.example.org
traefik/tcp/routers/<router_name>/tls/domains/0/sans/1 更多信息请参阅 TLS。 dev.example.org
traefik/tcp/routers/<router_name>/tls/options 更多信息请参阅 TLS。 foobar
traefik/tcp/routers/<router_name>/tls/passthrough 更多信息请参阅 TLS。 true
traefik/tcp/routers/<router_name>/priority 更多信息请参阅优先级。 42

服务

键(路径) 说明 值
traefik/tcp/services/<service_name>/loadbalancer/servers/0/address 更多信息请参阅服务器。 xx.xx.xx.xx:xx
traefik/tcp/services/<service_name>/loadbalancer/servers/0/tls 更多信息请参阅服务器。 true
traefik/tcp/services/<service_name>/loadbalancer/serverstransport 允许引用通过 File 提供者或 Kubernetes CRD 提供者定义的 ServersTransport 资源。
更多信息请参阅 serverstransport。
foobar@file
traefik/tcp/services/<service_name>/weighted/services/0/name 更多信息请参阅服务。 foobar
traefik/tcp/services/<service_name>/weighted/services/0/weight 更多信息请参阅服务。 42

中间件

配置选项

可以使用以 traefik/tcp/middlewares/{name-of-your-choice}. 开头、后接中间件类型和选项的标签来声明中间件。

例如,要声明一个名为 test-inflightconn 的 InFlightConn 中间件,可以写为 traefik/tcp/middlewares/test-inflightconn/inflightconn/amount=10。

有关可用中间件的更多信息,请参阅专门的中间件章节。

键(路径) 说明 值
traefik/tcp/middlewares/<middleware_name>/middleware_type/middleware_option 其中,middleware_type 是中间件类型(例如 inflightconn),
middleware_option 是要设置的中间件选项(例如 inflightconn 中间件的 amount)。
有关可用中间件的更多信息,请参阅专门的中间件章节。
foobar

声明冲突

如果声明多个名称相同但参数不同的中间件,该中间件将无法声明。

ServerTransport

配置选项
键(路径) 说明 值
traefik/tcp/serversTransports/<servers_transport_name>/st_option 其中,st_option 是要设置的 ServerTransport 选项(例如 maxIdleConnsPerHost)。
有关可用选项的更多信息,请参阅专门的 ServerTransport 章节。
ServerTransport 选项

UDP

可以使用 KV 声明 UDP 路由器和/或服务。

路由器

键(路径) 说明 值
traefik/udp/routers/<router_name>/entrypoints/0 更多信息请参阅 UDP 路由器。 foobar
traefik/udp/routers/<router_name>/service 更多信息请参阅 UDP 路由器。 foobar

服务

键(路径) 说明 值
traefik/udp/services/<service_name>/loadBalancer/servers/<n>/address 更多信息请参阅 UDP 服务。 foobar
traefik/udp/services/<service_name>/weighted/services/0/name 更多信息请参阅 UDP 服务。 foobar
traefik/udp/services/<service_name>/weighted/services/0/weight 更多信息请参阅 UDP 服务。 42

TLS

TLS 选项

使用 KV 提供者时,通过 tls/options 键配置 TLS 连接的部分参数。

例如,可以按以下方式定义基本配置:

键(路径) 说明 值
traefik/tls/options/Options0/alpnProtocols/0 更多信息请参阅 TLS 选项。 foobar
traefik/tls/options/Options0/cipherSuites/0 更多信息请参阅 TLS 选项。 foobar
traefik/tls/options/Options0/clientAuth/caFiles/0 更多信息请参阅 TLS 选项。 foobar
traefik/tls/options/Options0/disableSessiontickets 更多信息请参阅 TLS 选项。 true

TLS 默认生成的证书

可以配置 Traefik 使用 ACME 提供者(例如 Let's Encrypt)生成默认证书。

用于解析默认证书的配置应定义在 TLS 存储中。

键(路径) 说明 值
traefik/tls/stores/Store0/defaultGeneratedCert/domain/main 更多信息请参阅 TLS。 foobar
traefik/tls/stores/Store0/defaultGeneratedCert/domain/sans/0 更多信息请参阅 TLS。 foobar
traefik/tls/stores/Store0/defaultGeneratedCert/domain/sans/1 更多信息请参阅 TLS。 foobar
traefik/tls/stores/Store0/defaultGeneratedCert/resolver 更多信息请参阅 TLS。 foobar

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

文档由 HeadFile Studio 翻译。

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