服务¶
配置如何到达服务
该Services
负责配置如何到达实际的服务,最终将处理传入的请求。
配置示例¶
使用两个服务器声明HTTP服务 - 使用文件提供程序
[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/"
使用两个服务器声明TCP服务 - 使用文件提供程序
[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"
tcp:
services:
my-service:
loadBalancer:
servers:
- address: "xx.xx.xx.xx:xx"
- address: "xx.xx.xx.xx:xx"
配置HTTP服务¶
一般¶
目前,LoadBalancer
是唯一受支持的HTTP类型Service
(见下文)。但是,由于Traefik是一个不断发展的项目,未来将提供其他类型的HTTP服务,这是您必须指定它的原因
负载均衡器¶
负载平衡器能够在多个程序实例之间对请求进行负载平衡
使用两个服务器声明服务(使用负载平衡) - 使用文件提供程序
[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/"
服务器¶
服务器声明您的程序的单个实例。 该url选项指向特定实例。
Note
服务器中的路径url没有任何效果。
如果您希望将请求发送到服务器上的特定路径,
请将您配置routers
为使用相应的中间件
(例如AddPrefix或ReplacePath)中间件。
一台服务器 - 使用 File Provider
[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-1/"
负载均衡¶
目前,仅支持循环负载平衡:
负载平衡 - 使用 File Provider
[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/"
粘性会话¶
启用粘性会话时,会在初始请求上设置cookie,以跟踪哪个服务器处理第一个响应。 在后续请求中,客户端将转发到同一服务器。
粘性和不健康的服务器
如果cookie中指定的服务器变得不健康,则该请求将被转发到新服务器(并且cookie将跟踪新服务器)。
Cookie名称
默认cookie名称是sha1的缩写(例如:)_1d52e。
安全和HTTPOnly标志
默认情况下,创建关联cookie而不使用这些标志。然而,一个可以通过配置改变它。
Adding Stickiness
[http.services]
[http.services.my-service]
[http.services.my-service.loadBalancer.stickiness]
http:
services:
my-service:
loadBalancer:
stickiness: {}
Adding Stickiness with a Custom Cookie Name
[http.services]
[http.services.my-service]
[http.services.my-service.loadBalancer.stickiness]
cookieName = "my_stickiness_cookie_name"
secureCookie = true
httpOnlyCookie = true
http:
services:
my-service:
loadBalancer:
stickiness:
cookieName: my_stickiness_cookie_name
secureCookie: true
httpOnlyCookie: true
健康检查¶
配置运行状况检查以从负载平衡轮换中删除不健康的服务器。Traefik会考虑你的服务器的健康,只要他们之间的返回状态代码2XX
,并3XX
以健康检查的要求(进行的每项interval
)
以下是运行状况检查机制的可用选项:
path
附加到服务器URL以设置运行状况检查端点。scheme
, 如果已定义,将替换scheme运行状况检查端点的服务器URLhostname
, 如果已定义,将替换hostname运行状况检查端点的服务器URL 。port
, 如果已定义,将替换port运行状况检查端点的服务器URL 。interval
定义健康检查呼叫的频率。timeout
定义Traefik在考虑服务器失败(不健康)之前等待健康检查请求的最长持续时间。headers
定义要发送到运行状况检查端点的自定义标头。
Interval & Timeout Format
Interval and timeout are to be given in a format understood by time.ParseDuration. The interval must be greater than the timeout. If configuration doesn't reflect this, the interval will be set to timeout + 1 second.
Recovering Servers
Traefik keeps monitoring the health of unhealthy servers.
If a server has recovered (returning 2xx
-> 3xx
responses again), it will be added back to the load balacer rotation pool.
Custom Interval & Timeout -- Using the File Provider
[http.services]
[http.servicess.Service-1]
[http.services.Service-1.loadBalancer.healthCheck]
path = "/health"
interval = "10s"
timeout = "3s"
http:
servicess:
Service-1:
loadBalancer:
healthCheck:
path: /health
interval: "10s"
timeout: "3s"
Custom Port -- Using the File Provider
[http.services]
[http.services.Service-1]
[http.services.Service-1.loadBalancer.healthCheck]
path = "/health"
port = 8080
http:
services:
Service-1:
loadBalancer:
healthCheck:
path: /health
port: 8080
Custom Scheme -- Using the File Provider
[http.services]
[http.services.Service-1]
[http.services.Service-1.loadBalancer.healthCheck]
path = "/health"
scheme = "http"
http:
services:
Service-1:
loadBalancer:
healthCheck:
path: /health
scheme: http
Additional HTTP Headers -- Using the File Provider
[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"
http:
services:
Service-1:
loadBalancer:
healthCheck:
path: /health
headers:
My-Custom-Header: foo
My-Header: bar
配置TCP服务¶
一般¶
目前,LoadBalancer
是唯一受支持的TCP类型Service。
但是,由于Traefik是一个不断发展的项目,未来将提供其他类型的TCP服务,这是您必须指定它的原因。
负载均衡器¶
负载平衡器能够在多个程序实例之间对请求进行负载平衡。
Declaring a Service with Two Servers -- Using the File Provider
[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"
tcp:
services:
my-service:
loadBalancer:
servers:
- address: "xx.xx.xx.xx:xx"
- address: "xx.xx.xx.xx:xx"
Servers¶
服务器声明您的程序的单个实例。 该address选项(IP:端口)指向一个特定的实例。
A Service with One Server -- Using the File Provider
[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"