Services

Configuring How to Reach the Services

services

Services负责配置如何到达最终将处理传入请求的实际服务.

Configuration Examples

声明两个服务器的HTTP服务-使用文件提供程序
## Dynamic configuration
[http.services]
  [http.services.my-service.loadBalancer]

    [[http.services.my-service.loadBalancer.servers]]
      url = "http://<private-ip-server-1>:<private-port-server-1>/"
    [[http.services.my-service.loadBalancer.servers]]
      url = "http://<private-ip-server-2>:<private-port-server-2>/"
## Dynamic configuration
http:
  services:
    my-service:
      loadBalancer:
        servers:
        - url: "http://<private-ip-server-1>:<private-port-server-1>/"
        - url: "http://<private-ip-server-2>:<private-port-server-2>/"
声明两个服务器的TCP服务-使用文件提供程序
## Dynamic configuration
[tcp.services]
  [tcp.services.my-service.loadBalancer]
     [[tcp.services.my-service.loadBalancer.servers]]
       address = "<private-ip-server-1>:<private-port-server-1>"
     [[tcp.services.my-service.loadBalancer.servers]]
       address = "<private-ip-server-2>:<private-port-server-2>"
tcp:
  services:
    my-service:
      loadBalancer:
        servers:
        - address: "<private-ip-server-1>:<private-port-server-1>"
        - address: "<private-ip-server-2>:<private-port-server-2>"

Configuring HTTP Services

Servers Load Balancer

负载平衡器能够在程序的多个实例之间对请求进行负载平衡.

每个服务都有一个负载平衡器,即使只有一台服务器将流量转发到该服务器也是如此.

声明具有两台服务器的服务(具有负载平衡)-使用文件提供程序
## Dynamic configuration
[http.services]
  [http.services.my-service.loadBalancer]

    [[http.services.my-service.loadBalancer.servers]]
      url = "http://private-ip-server-1/"
    [[http.services.my-service.loadBalancer.servers]]
      url = "http://private-ip-server-2/"
http:
  services:
    my-service:
      loadBalancer:
        servers:
        - url: "http://private-ip-server-1/"
        - url: "http://private-ip-server-2/"

Servers

服务器声明程序的单个实例. url选项指向特定实例.

服务器url中的路径无效. 如果希望将请求发送到服务器上的特定路径,请配置routers以使用相应的中间件 (例如AddPrefixReplacePath )中间件.

一台服务器的服务-使用文件提供程序
## Dynamic configuration
[http.services]
  [http.services.my-service.loadBalancer]
    [[http.services.my-service.loadBalancer.servers]]
      url = "http://private-ip-server-1/"
## Dynamic configuration
http:
  services:
    my-service:
      loadBalancer:
        servers:
          - url: "http://private-ip-server-1/"

Load-balancing

目前,仅支持循环负载平衡:

负载平衡-使用文件提供程序
## Dynamic configuration
[http.services]
  [http.services.my-service.loadBalancer]
    [[http.services.my-service.loadBalancer.servers]]
      url = "http://private-ip-server-1/"
    [[http.services.my-service.loadBalancer.servers]]
      url = "http://private-ip-server-2/"
## Dynamic configuration
http:
  services:
    my-service:
      loadBalancer:
        servers:
        - url: "http://private-ip-server-1/"
        - url: "http://private-ip-server-2/"

Sticky sessions

启用粘性会话后,将在初始请求上设置cookie,以跟踪哪个服务器处理第一个响应. 根据后续请求,客户端将转发到同一服务器.

粘性和不健康的服务器

如果cookie中指定的服务器不正常,该请求将被转发到新服务器(cookie将跟踪新服务器).

Cookie名称

默认的cookie名称是sha1的缩写(例如: _1d52e ).

安全和HTTPOnly标志

默认情况下,创建的亲和力Cookie不包含这些标志. 但是,可以通过配置更改它.

添加粘性-使用文件提供程序
## Dynamic configuration
[http.services]
  [http.services.my-service]
    [http.services.my-service.loadBalancer.sticky.cookie]
## Dynamic configuration
http:
  services:
    my-service:
      loadBalancer:
        sticky:
         cookie: {}
使用自定义选项添加粘性-使用文件提供程序
## Dynamic configuration
[http.services]
  [http.services.my-service]
    [http.services.my-service.loadBalancer.sticky.cookie]
      name = "my_sticky_cookie_name"
      secure = true
      httpOnly = true
## Dynamic configuration
http:
  services:
    my-service:
      loadBalancer:
        sticky:
          cookie:
            name: my_sticky_cookie_name
            secure: true
            httpOnly: true

Health Check

配置运行状况检查,以从负载平衡循环中删除不正常的服务器. 只要Traefik服务器将2XX3XX之间的状态代码返回到运行状况检查请求(在每个interval ),它们就会认为您的服务器运行状况良好.

以下是运行状况检查机制的可用选项:

  • path附加到服务器URL上以设置运行状况检查端点.
  • scheme (如果已定义)将替换运行状况检查端点的服务器URL scheme
  • hostname (如果已定义)将替换运行状况检查端点的服务器URL hostname .
  • port (如果已定义)将替换运行状况检查端点的服务器URL port .
  • interval定义运行状况检查调用的频率.
  • timeout定义Traefik在考虑服务器故障(不正常)之前等待运行状况检查请求的最长时间.
  • headers定义了要发送到运行状况检查端点的自定义标头.

间隔和超时格式

间隔和超时应以time.ParseDuration可以理解的格式给出. 该间隔必须大于超时. 如果配置不反映这一点,则间隔将设置为超时+ 1秒.

恢复服务器

Traefik继续监视运行状况不佳的服务器的运行状况. 如果服务器已恢复(再次返回2xx > 3xx响应),它将被重新添加到负载平衡器旋转池中.

自定义间隔和超时-使用文件提供程序
## Dynamic configuration
[http.services]
  [http.services.Service-1]
    [http.services.Service-1.loadBalancer.healthCheck]
      path = "/health"
      interval = "10s"
      timeout = "3s"
## Dynamic configuration
http:
  services:
    Service-1:
      loadBalancer:
        healthCheck:
          path: /health
          interval: "10s"
          timeout: "3s"
自定义端口-使用文件提供程序
## Dynamic configuration
[http.services]
  [http.services.Service-1]
    [http.services.Service-1.loadBalancer.healthCheck]
      path = "/health"
      port = 8080
## Dynamic configuration
http:
  services:
    Service-1:
      loadBalancer:
        healthCheck:
          path: /health
          port: 8080
自定义方案-使用文件提供程序
## Dynamic configuration
[http.services]
  [http.services.Service-1]
    [http.services.Service-1.loadBalancer.healthCheck]
      path = "/health"
      scheme = "http"
## Dynamic configuration
http:
  services:
    Service-1:
      loadBalancer:
        healthCheck:
          path: /health
          scheme: http
其他HTTP标头-使用文件提供程序
## Dynamic configuration
[http.services]
  [http.services.Service-1]
    [http.services.Service-1.loadBalancer.healthCheck]
      path = "/health"

      [http.services.Service-1.loadBalancer.healthCheck.headers]
        My-Custom-Header = "foo"
        My-Header = "bar"
## Dynamic configuration
http:
  services:
    Service-1:
      loadBalancer:
        healthCheck:
          path: /health
          headers:
            My-Custom-Header: foo
            My-Header: bar

Pass Host Header

passHostHeader允许将客户端主机标头转发到服务器.

默认情况下, passHostHeader为true.

不要转发主机头-使用文件提供程序
## Dynamic configuration
[http.services]
  [http.services.Service01]
    [http.services.Service01.loadBalancer]
      passHostHeader = false
## Dynamic configuration
http:
  services:
    Service01:
      loadBalancer:
        passHostHeader: false

Response Forwarding

本部分关于配置Traefik如何将响应从后端服务器转发到客户端.

以下是响应转发机制的可用选项:

  • FlushInterval指定在复制响应主体时刷新到客户端之间的间隔. 这是一个持续时间(以毫秒为单位),默认为100.负值表示每次写入客户端后立即刷新. 当ReverseProxy将响应识别为流响应时,将忽略FlushInterval. 对于此类响应,将立即将写操作刷新到客户端.
使用自定义FlushInterval-使用文件提供程序
## Dynamic configuration
[http.services]
  [http.services.Service-1]
    [http.services.Service-1.loadBalancer.responseForwarding]
      flushInterval = "1s"
## Dynamic configuration
http:
  services:
    Service-1:
      loadBalancer:
        responseForwarding:
          flushInterval: 1s

Weighted Round Robin (service)

WRR能够基于权重在多个服务之间平衡请求的负载.

此策略仅可用于在服务之间而不是服务器之间进行负载平衡.

支持的提供商

当前可以使用FileIngressRoute提供程序定义此策略.

## Dynamic configuration
[http.services]
  [http.services.app]
    [[http.services.app.weighted.services]]
      name = "appv1"
      weight = 3
    [[http.services.app.weighted.services]]
      name = "appv2"
      weight = 1

  [http.services.appv1]
    [http.services.appv1.loadBalancer]
      [[http.services.appv1.loadBalancer.servers]]
        url = "http://private-ip-server-1/"

  [http.services.appv2]
    [http.services.appv2.loadBalancer]
      [[http.services.appv2.loadBalancer.servers]]
        url = "http://private-ip-server-2/"
## Dynamic configuration
http:
  services:
    app:
      weighted:
        services:
        - name: appv1
          weight: 3
        - name: appv2
          weight: 1

    appv1:
      loadBalancer:
        servers:
        - url: "http://private-ip-server-1/"

    appv2:
      loadBalancer:
        servers:
        - url: "http://private-ip-server-2/"

Mirroring (service)

镜像能够将发送到服务的请求镜像到其他服务.

支持的提供商

当前可以使用FileIngressRoute提供程序定义此策略.

## Dynamic configuration
[http.services]
  [http.services.mirrored-api]
    [http.services.mirrored-api.mirroring]
      service = "appv1"
    [[http.services.mirrored-api.mirroring.mirrors]]
      name = "appv2"
      percent = 10

  [http.services.appv1]
    [http.services.appv1.loadBalancer]
      [[http.services.appv1.loadBalancer.servers]]
        url = "http://private-ip-server-1/"

  [http.services.appv2]
    [http.services.appv2.loadBalancer]
      [[http.services.appv2.loadBalancer.servers]]
        url = "http://private-ip-server-2/"
## Dynamic configuration
http:
  services:
    mirrored-api:
      mirroring:
        service: appv1
        mirrors:
        - name: appv2
          percent: 10

    appv1:
      loadBalancer:
        servers:
        - url: "http://private-ip-server-1/"

    appv2:
      loadBalancer:
        servers:
        - url: "http://private-ip-server-2/"

Configuring TCP Services

General

服务部分的每个字段代表一种服务. 这意味着,对于每个指定的服务,必须启用一个字段(只有一个)来定义创建哪种服务. 当前,两种可用的类型是LoadBalancerWeighted .

Servers Load Balancer

服务器负载平衡器负责平衡同一服务的服务器之间的请求.

声明两个服务器的服务-使用文件提供程序
## Dynamic configuration
[tcp.services]
  [tcp.services.my-service.loadBalancer]
    [[tcp.services.my-service.loadBalancer.servers]]
      address = "xx.xx.xx.xx:xx"
    [[tcp.services.my-service.loadBalancer.servers]]
       address = "xx.xx.xx.xx:xx"
## Dynamic configuration
tcp:
  services:
    my-service:
      loadBalancer:
        servers:
        - address: "xx.xx.xx.xx:xx"
        - address: "xx.xx.xx.xx:xx"

Servers

服务器声明程序的单个实例. address选项(IP:Port)指向特定实例.

一台服务器的服务-使用文件提供程序
## Dynamic configuration
[tcp.services]
  [tcp.services.my-service.loadBalancer]
    [[tcp.services.my-service.loadBalancer.servers]]
      address = "xx.xx.xx.xx:xx"
## Dynamic configuration
tcp:
  services:
    my-service:
      loadBalancer:
        servers:
          - address: "xx.xx.xx.xx:xx"

Termination Delay

作为客户端和服务器之间的代理,任何一方(例如,客户端)都可能决定终止其在连接上的写入功能(即FIN数据包的发布). 代理需要将该意图传播到另一侧,因此,当这种情况发生时,它在与另一侧(例如后端)的连接中也执行相同的操作.

但是,如果另一端由于某种原因(错误的执行或恶意的目的)最终也没有做同样的事情,则连接将保持半开状态,这将使资源锁定很长时间.

为此,代理服务器一旦进入此终止顺序,就为完全终止双方的连接设置了最后期限.

终止延迟控制该截止日期. 这是一个持续时间(以毫秒为单位),默认为100.负值表示无限期(即连接永远不会被代理本身完全终止).

具有终止延迟的服务-使用文件提供程序
## Dynamic configuration
[tcp.services]
  [tcp.services.my-service.loadBalancer]
    [[tcp.services.my-service.loadBalancer]]
      terminationDelay = 200
## Dynamic configuration
tcp:
  services:
    my-service:
      loadBalancer:
        terminationDelay: 200

Weighted Round Robin

服务的加权轮循(别名WRR )负载均衡器负责根据提供的权重平衡多个服务之间的请求.

此策略仅可用于在服务之间而不是服务器之间进行负载平衡.

支持的提供商

当前可以使用FileIngressRoute提供程序定义此策略.

## Dynamic configuration
[tcp.services]
  [tcp.services.app]
    [[tcp.services.app.weighted.services]]
      name = "appv1"
      weight = 3
    [[tcp.services.app.weighted.services]]
      name = "appv2"
      weight = 1

  [tcp.services.appv1]
    [tcp.services.appv1.loadBalancer]
      [[tcp.services.appv1.loadBalancer.servers]]
        address = "private-ip-server-1:8080/"

  [tcp.services.appv2]
    [tcp.services.appv2.loadBalancer]
      [[tcp.services.appv2.loadBalancer.servers]]
        address = "private-ip-server-2:8080/"
## Dynamic configuration
tcp:
  services:
    app:
      weighted:
        services:
        - name: appv1
          weight: 3
        - name: appv2
          weight: 1

    appv1:
      loadBalancer:
        servers:
        - address: "xxx.xxx.xxx.xxx:8080"

    appv2:
      loadBalancer:
        servers:
        - address: "xxx.xxx.xxx.xxx:8080"