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> 中不允许使用字符 @。
服务¶
服务名称 <service_name> 中不允许使用字符 @。
中间件¶
配置选项¶
| 键(路径) | 说明 | 值 |
|---|---|---|
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 |