跳到正文
Træfɪk

Traefik 与 Consul Catalog

Traefik 最出色的特性之一,是将路由配置交由应用层管理。通过 Consul Catalog,Traefik 可以使用附加在服务上的标签生成路由规则。

标签与敏感数据

我们建议不要使用标签存储敏感数据(证书、凭据等)。建议将敏感数据放在更安全的存储中(Secret、文件等)。

配置示例

配置 Consul Catalog 并部署或暴露一个服务

启用 consul catalog 提供者

providers:
  consulCatalog: {}
[providers.consulCatalog]
--providers.consulcatalog=true

为服务附加标签(向 Consul 注册服务时)

consul services register -name=my-service -tag="traefik.http.routers.my-service.rule=Host(`example.com`)"

或者使用服务定义文件:

{
  "service": {
    "name": "my-service",
    "tags": [
      "traefik.http.routers.my-service.rule=Host(`example.com`)"
    ]
  }
}
为容器指定自定义端口

将发往 http://example.com 的请求转发至 http://<private IP of container>:12345:

{
  "service": {
    "name": "my-service",
    "tags": [
      "traefik.http.routers.my-service.rule=Host(`example.com`)",
      "traefik.http.routers.my-service.service=my-service",
      "traefik.http.services.my-service.loadbalancer.server.port=12345"
    ]
  }
}

Traefik 连接到错误端口:HTTP/502 Gateway Error

默认情况下,Traefik 使用容器暴露的第一个端口。

设置标签 traefik.http.services.xxx.loadbalancer.server.port 可覆盖此行为。

为每个容器指定多个路由器和服务

要将请求转发到同一容器的多个端口,需要使用路由器上的 service 参数引用服务负载均衡器的端口定义。

在此示例中,发往 http://example-a.com 的请求会转发至 http://<private IP of container>:8000,同时,发往 http://example-b.com 的请求会转发至 http://<private IP of container>:9000:

{
  "service": {
    "name": "my-service",
    "tags": [
      "traefik.http.routers.www-router.rule=Host(`example-a.com`)",
      "traefik.http.routers.www-router.service=www-service",
      "traefik.http.services.www-service.loadbalancer.server.port=8000",
      "traefik.http.routers.admin-router.rule=Host(`example-b.com`)",
      "traefik.http.routers.admin-router.service=admin-service",
      "traefik.http.services.admin-service.loadbalancer.server.port=9000"
    ]
  }
}

配置选项

标签

标签不区分大小写。

TLS 默认生成的证书

要了解如何配置 Traefik 默认生成的证书,请参阅 TLS 证书页面。

通用说明

Traefik 会为每个 consul Catalog 服务创建相应的服务和路由器。

该 consul Catalog 服务中的每个实例都会自动成为 Traefik 服务的一台服务器,路由器则会附加一条基于服务名称的默认规则。

路由器

要更新自动附加到服务的路由器配置,请添加以 traefik.http.routers.{name-of-your-choice}. 开头、后接要修改的选项的标签。

例如,要修改规则,可以添加标签 traefik.http.routers.my-service.rule=Host(`example.com`)。

配置选项

标签 说明 值
traefik.http.routers.<router_name>.rule 更多信息请参阅规则。 Host(`example.com`)
traefik.http.routers.<router_name>.ruleSyntax 更多信息请参阅 ruleSyntax。
RuleSyntax 选项已弃用,将在下一个主版本中移除。
请勿使用此字段,并将路由器规则重写为 v3 语法。
v3
traefik.http.routers.<router_name>.priority 更多信息请参阅优先级。 42
traefik.http.routers.<router_name>.entrypoints 更多信息请参阅入口点。 web,websecure
traefik.http.routers.<router_name>.middlewares 更多信息请参阅中间件概述。 auth,prefix,cb
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[n].main 更多信息请参阅域名。 example.org
traefik.http.routers.<router_name>.tls.domains[n].sans 更多信息请参阅域名。 test.example.org,dev.example.org
traefik.http.routers.<router_name>.tls.options 更多信息请参阅 TLS 选项;多个路由器服务同一主机名时,请参阅 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 服务配置,请添加以 traefik.http.services.{name-of-your-choice}. 开头、后接要修改的选项的标签。

例如,要修改 passHostHeader 的行为,可以添加标签 traefik.http.services.{name-of-your-choice}.loadbalancer.passhostheader=false。

配置选项

标签 说明 值
traefik.http.services.<service_name>.loadbalancer.server.port 注册一个端口。
适用于服务暴露多个端口的情况。
8080
traefik.http.services.<service_name>.loadbalancer.server.scheme 覆盖默认协议方案。 http
traefik.http.services.<service_name>.loadbalancer.server.weight 覆盖默认权重。 42
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.<header_name> 更多信息请参阅健康检查。 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.unhealthyinterval 更多信息请参阅健康检查。 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.healthcheck.followredirects 更多信息请参阅健康检查。 true
traefik.http.services.<service_name>.loadbalancer.sticky.cookie 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.<service_name>.loadbalancer.responseforwarding.flushinterval 10

中间件

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

例如,要声明一个名为 my-redirect 的 redirectscheme 中间件,可以写为 traefik.http.middlewares.my-redirect.redirectscheme.scheme: https。

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

声明并引用中间件
# ...
# Declaring a middleware
traefik.http.middlewares.my-redirect.redirectscheme.scheme=https
# Referencing a middleware
traefik.http.routers.my-service.middlewares=my-redirect

声明冲突

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

TCP

可以使用标签声明 TCP 路由器、中间件和/或服务。

声明 TCP 路由器和服务
traefik.tcp.routers.my-router.rule=HostSNI(`example.com`)
traefik.tcp.routers.my-router.tls=true
traefik.tcp.services.my-service.loadbalancer.server.port=4123

TCP 与 HTTP

如果声明 TCP 路由器或服务,将阻止 Traefik 自动创建 HTTP 路由器或服务(未定义 TCP 路由器或服务时,Traefik 默认会自动创建它们)。可以为同一个 consul 服务同时声明 TCP 路由器或服务与 HTTP 路由器或服务,但需要手动完成。

TCP 路由器

配置选项
标签 说明 值
traefik.tcp.routers.<router_name>.entrypoints 更多信息请参阅入口点。 ep1,ep2
traefik.tcp.routers.<router_name>.rule 更多信息请参阅规则。 HostSNI(`example.com`)
traefik.tcp.routers.<router_name>.ruleSyntax 按路由器配置用于解析规则的规则语法。
RuleSyntax 选项已弃用,将在下一个主版本中移除。
请勿使用此字段,并将路由器规则重写为 v3 语法。
v3
traefik.tcp.routers.<router_name>.priority 更多信息请参阅优先级。 42
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[n].main 更多信息请参阅 TLS。 example.org
traefik.tcp.routers.<router_name>.tls.domains[n].sans 更多信息请参阅 TLS。 test.example.org,dev.example.org
traefik.tcp.routers.<router_name>.tls.options 更多信息请参阅 TLS。 mysoptions
traefik.tcp.routers.<router_name>.tls.passthrough 更多信息请参阅透传。 true

TCP 服务

配置选项
标签 说明 值
traefik.tcp.services.<service_name>.loadbalancer.server.port 注册应用的一个端口。 423
traefik.tcp.services.<service_name>.loadbalancer.server.tls 决定连接后端时是否使用 TLS。 true
traefik.tcp.services.<service_name>.loadbalancer.serverstransport 允许引用通过 File 提供者或 Kubernetes CRD 提供者定义的 ServersTransport 资源。
更多信息请参阅 serverstransport。
foobar@file

TCP 中间件

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

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

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

声明并引用中间件
# ...
# Declaring a middleware
traefik.tcp.middlewares.test-inflightconn.inflightconn.amount=10
# Referencing a middleware
traefik.tcp.routers.my-service.middlewares=test-inflightconn

声明冲突

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

UDP

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

声明 UDP 路由器和服务
traefik.udp.routers.my-router.entrypoints=udp
traefik.udp.services.my-service.loadbalancer.server.port=4123

UDP 与 HTTP

如果声明 UDP 路由器或服务,将阻止 Traefik 自动创建 HTTP 路由器或服务(未定义 UDP 路由器或服务时,Traefik 默认会自动创建它们)。可以为同一个 consul 服务同时声明 UDP 路由器或服务与 HTTP 路由器或服务,但需要手动完成。

UDP 路由器

配置选项
标签 说明 值
traefik.udp.routers.<router_name>.entrypoints 更多信息请参阅入口点。 ep1,ep2
traefik.udp.routers.<router_name>.service 更多信息请参阅服务。 myservice

UDP 服务

配置选项
标签 说明 值
traefik.udp.services.<service_name>.loadbalancer.server.port 注册应用的一个端口。 423

提供者专属选项

标签 说明 值
traefik.enable 可以将 traefik.enable 设置为 true 或 false,告知 Traefik 是否考虑此服务。
此选项会覆盖 exposedByDefault 的值。
true
traefik.consulcatalog.connect 可以将 traefik.consulcatalog.connect 设置为 true 或 false,告知 Traefik 是否将此服务视为支持 Connect 的服务。
此选项会覆盖 connectByDefault 的值。
true
traefik.consulcatalog.canary 在 Nomad 编排器环境中,ConsulCatalog 作为 Traefik 的服务注册提供者时,可能需要在 Traefik 中区分服务的金丝雀实例和生产实例。
例如,可能不希望它们属于同一个负载均衡器。

因此,此选项应作为 Nomad service 段中 canary_tags 字段的值之一提供,让 Traefik 能识别关联实例是金丝雀实例。
true

端口查找

Traefik 能够按照默认的 consul Catalog 流程检测要使用的端口。也就是说,如果在 consul Catalog 界面中暴露了一个端口,例如 :1337,Traefik 就会发现并使用该端口。


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

文档由 HeadFile Studio 翻译。

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