跳到正文
Træfɪk

Traefik 与 ECS

Traefik 最出色的功能之一,是将路由配置交由应用层管理。 使用 ECS 时,Traefik 可以利用附加到容器的标签生成路由规则。

标签与敏感数据

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

配置示例

配置 ECS 并部署/暴露一个服务

启用 ECS 提供者

providers:
  ecs: {}
[providers.ecs]
--providers.ecs=true

为容器附加标签(在 ECS 任务定义中)

{
  "family": "my-service",
  "containerDefinitions": [
    {
      "name": "my-container",
      "image": "my-image:latest",
      "labels": {
        "traefik.http.routers.my-container.rule": "Host(`example.com`)"
      }
    }
  ]
}
为容器指定自定义端口

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

{
  "family": "my-service",
  "containerDefinitions": [
    {
      "name": "my-container",
      "image": "my-image:latest",
      "labels": {
        "traefik.http.routers.my-container.rule": "Host(`example.com`)",
        "traefik.http.routers.my-container.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:

{
  "family": "my-service",
  "containerDefinitions": [
    {
      "name": "my-container",
      "image": "my-image:latest",
      "labels": {
        "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 为每个弹性服务创建一个对应的服务和路由器。

服务会自动为每个弹性容器添加一个服务器,路由器则会获得一条基于服务名称的默认规则。

路由器

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

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

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

配置选项

标签 说明 值
traefik.http.routers.<router_name>.rule 更多信息请参阅规则。 Host(`example.com`)
traefik.http.routers.<router_name>.ruleSyntax 更多信息请参阅 ruleSyntax。
RuleSyntax 选项已弃用,将在下一个主版本中移除。
请勿使用此字段,并将路由器规则改写为 v3 语法。
v3
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.http.routers.<router_name>.priority 更多信息请参阅优先级。 42

服务

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

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

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

配置选项

标签 说明 值
traefik.http.services.<service_name>.loadbalancer.server.port 注册一个端口。
适用于服务暴露多个端口的情况。
8080
traefik.http.services.<service_name>.loadbalancer.server.scheme 覆盖默认协议方案。 http
traefik.http.services.<service_name>.loadbalancer.serverstransport 允许引用使用文件提供者或 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 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 路由器/服务时,默认会自动创建)。 可以为同一个弹性服务同时声明 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>.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
traefik.tcp.routers.<router_name>.priority 更多信息请参阅优先级。 42

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 允许引用使用文件提供者或 Kubernetes CRD 提供者定义的 ServersTransport 资源。
更多信息请参阅 serverstransport。
foobar@file

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 路由器/服务时,默认会自动创建)。 可以为同一个弹性服务同时声明 UDP 路由器/服务和 HTTP 路由器/服务(但必须手动声明)。

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 路由器

配置选项
标签 说明 值
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 是否处理该 ECS 服务。
此选项覆盖 exposedByDefault 的值。
true

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

文档由 HeadFile Studio 翻译。

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