Services

Configuring How to Reach the Services

services

The Services are responsible for configuring how to reach the actual services that will eventually handle the incoming requests.

Configuration Examples

Declaring an HTTP Service with Two Servers — Using the File Provider

  1. ## Dynamic configuration
  2. [http.services]
  3. [http.services.my-service.loadBalancer]
  4. [[http.services.my-service.loadBalancer.servers]]
  5. url = "http://<private-ip-server-1>:<private-port-server-1>/"
  6. [[http.services.my-service.loadBalancer.servers]]
  7. url = "http://<private-ip-server-2>:<private-port-server-2>/"

  1. ## Dynamic configuration
  2. http:
  3. services:
  4. my-service:
  5. loadBalancer:
  6. servers:
  7. - url: "http://<private-ip-server-1>:<private-port-server-1>/"
  8. - url: "http://<private-ip-server-2>:<private-port-server-2>/"

Declaring a TCP Service with Two Servers — Using the File Provider

  1. ## Dynamic configuration
  2. [tcp.services]
  3. [tcp.services.my-service.loadBalancer]
  4. [[tcp.services.my-service.loadBalancer.servers]]
  5. address = "<private-ip-server-1>:<private-port-server-1>"
  6. [[tcp.services.my-service.loadBalancer.servers]]
  7. address = "<private-ip-server-2>:<private-port-server-2>"

  1. tcp:
  2. services:
  3. my-service:
  4. loadBalancer:
  5. servers:
  6. - address: "<private-ip-server-1>:<private-port-server-1>"
  7. - address: "<private-ip-server-2>:<private-port-server-2>"

Configuring HTTP Services

Servers Load Balancer

The load balancers are able to load balance the requests between multiple instances of your programs.

Each service has a load-balancer, even if there is only one server to forward traffic to.

Declaring a Service with Two Servers (with Load Balancing) — Using the File Provider

  1. ## Dynamic configuration
  2. [http.services]
  3. [http.services.my-service.loadBalancer]
  4. [[http.services.my-service.loadBalancer.servers]]
  5. url = "http://private-ip-server-1/"
  6. [[http.services.my-service.loadBalancer.servers]]
  7. url = "http://private-ip-server-2/"

  1. http:
  2. services:
  3. my-service:
  4. loadBalancer:
  5. servers:
  6. - url: "http://private-ip-server-1/"
  7. - url: "http://private-ip-server-2/"

Servers

Servers declare a single instance of your program.The url option point to a specific instance.

Paths in the servers' url have no effect.If you want the requests to be sent to a specific path on your servers,configure your routers to use a corresponding middleware (e.g. the AddPrefix or ReplacePath) middlewares.

A Service with One Server — Using the File Provider

  1. ## Dynamic configuration
  2. [http.services]
  3. [http.services.my-service.loadBalancer]
  4. [[http.services.my-service.loadBalancer.servers]]
  5. url = "http://private-ip-server-1/"

  1. ## Dynamic configuration
  2. http:
  3. services:
  4. my-service:
  5. loadBalancer:
  6. servers:
  7. - url: "http://private-ip-server-1/"

Load-balancing

For now, only round robin load balancing is supported:

Load Balancing — Using the File Provider

  1. ## Dynamic configuration
  2. [http.services]
  3. [http.services.my-service.loadBalancer]
  4. [[http.services.my-service.loadBalancer.servers]]
  5. url = "http://private-ip-server-1/"
  6. [[http.services.my-service.loadBalancer.servers]]
  7. url = "http://private-ip-server-2/"

  1. ## Dynamic configuration
  2. http:
  3. services:
  4. my-service:
  5. loadBalancer:
  6. servers:
  7. - url: "http://private-ip-server-1/"
  8. - url: "http://private-ip-server-2/"

Sticky sessions

When sticky sessions are enabled, a cookie is set on the initial request to track which server handles the first response.On subsequent requests, the client is forwarded to the same server.

Stickiness & Unhealthy Servers

If the server specified in the cookie becomes unhealthy, the request will be forwarded to a new server (and the cookie will keep track of the new server).

Cookie Name

The default cookie name is an abbreviation of a sha1 (ex: _1d52e).

Secure & HTTPOnly flags

By default, the affinity cookie is created without those flags. One however can change that through configuration.

Adding Stickiness — Using the File Provider

  1. ## Dynamic configuration
  2. [http.services]
  3. [http.services.my-service]
  4. [http.services.my-service.loadBalancer.sticky.cookie]

  1. ## Dynamic configuration
  2. http:
  3. services:
  4. my-service:
  5. loadBalancer:
  6. sticky:
  7. cookie: {}

Adding Stickiness with custom Options — Using the File Provider

  1. ## Dynamic configuration
  2. [http.services]
  3. [http.services.my-service]
  4. [http.services.my-service.loadBalancer.sticky.cookie]
  5. name = "my_sticky_cookie_name"
  6. secure = true
  7. httpOnly = true

  1. ## Dynamic configuration
  2. http:
  3. services:
  4. my-service:
  5. loadBalancer:
  6. sticky:
  7. cookie:
  8. name: my_sticky_cookie_name
  9. secure: true
  10. httpOnly: true

Health Check

Configure health check to remove unhealthy servers from the load balancing rotation.Traefik will consider your servers healthy as long as they return status codes between 2XX and 3XX to the health check requests (carried out every interval).

Below are the available options for the health check mechanism:

  • path is appended to the server URL to set the health check endpoint.
  • scheme, if defined, will replace the server URL scheme for the health check endpoint
  • hostname, if defined, will replace the server URL hostname for the health check endpoint.
  • port, if defined, will replace the server URL port for the health check endpoint.
  • interval defines the frequency of the health check calls.
  • timeout defines the maximum duration Traefik will wait for a health check request before considering the server failed (unhealthy).
  • headers defines custom headers to be sent to the health check endpoint.

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

  1. ## Dynamic configuration
  2. [http.services]
  3. [http.services.Service-1]
  4. [http.services.Service-1.loadBalancer.healthCheck]
  5. path = "/health"
  6. interval = "10s"
  7. timeout = "3s"

  1. ## Dynamic configuration
  2. http:
  3. services:
  4. Service-1:
  5. loadBalancer:
  6. healthCheck:
  7. path: /health
  8. interval: "10s"
  9. timeout: "3s"

Custom Port — Using the File Provider

  1. ## Dynamic configuration
  2. [http.services]
  3. [http.services.Service-1]
  4. [http.services.Service-1.loadBalancer.healthCheck]
  5. path = "/health"
  6. port = 8080

  1. ## Dynamic configuration
  2. http:
  3. services:
  4. Service-1:
  5. loadBalancer:
  6. healthCheck:
  7. path: /health
  8. port: 8080

Custom Scheme — Using the File Provider

  1. ## Dynamic configuration
  2. [http.services]
  3. [http.services.Service-1]
  4. [http.services.Service-1.loadBalancer.healthCheck]
  5. path = "/health"
  6. scheme = "http"

  1. ## Dynamic configuration
  2. http:
  3. services:
  4. Service-1:
  5. loadBalancer:
  6. healthCheck:
  7. path: /health
  8. scheme: http

Additional HTTP Headers — Using the File Provider

  1. ## Dynamic configuration
  2. [http.services]
  3. [http.services.Service-1]
  4. [http.services.Service-1.loadBalancer.healthCheck]
  5. path = "/health"
  6. [http.services.Service-1.loadBalancer.healthCheck.headers]
  7. My-Custom-Header = "foo"
  8. My-Header = "bar"

  1. ## Dynamic configuration
  2. http:
  3. services:
  4. Service-1:
  5. loadBalancer:
  6. healthCheck:
  7. path: /health
  8. headers:
  9. My-Custom-Header: foo
  10. My-Header: bar

Pass Host Header

The passHostHeader allows to forward client Host header to server.

By default, passHostHeader is true.

Don't forward the host header — Using the File Provider

  1. ## Dynamic configuration
  2. [http.services]
  3. [http.services.Service01]
  4. [http.services.Service01.loadBalancer]
  5. passHostHeader = false

  1. ## Dynamic configuration
  2. http:
  3. services:
  4. Service01:
  5. loadBalancer:
  6. passHostHeader: false

Response Forwarding

This section is about configuring how Traefik forwards the response from the backend server to the client.

Below are the available options for the Response Forwarding mechanism:

  • FlushInterval specifies the interval in between flushes to the client while copying the response body.It is a duration in milliseconds, defaulting to 100.A negative value means to flush immediately after each write to the client.The FlushInterval is ignored when ReverseProxy recognizes a response as a streaming response;for such responses, writes are flushed to the client immediately.

Using a custom FlushInterval — Using the File Provider

  1. ## Dynamic configuration
  2. [http.services]
  3. [http.services.Service-1]
  4. [http.services.Service-1.loadBalancer.responseForwarding]
  5. flushInterval = "1s"

  1. ## Dynamic configuration
  2. http:
  3. services:
  4. Service-1:
  5. loadBalancer:
  6. responseForwarding:
  7. flushInterval: 1s

Weighted Round Robin (service)

The WRR is able to load balance the requests between multiple services based on weights.

This strategy is only available to load balance between services and not between servers.

Supported Providers

This strategy can be defined currently with the File or IngressRoute providers.

  1. ## Dynamic configuration
  2. [http.services]
  3. [http.services.app]
  4. [[http.services.app.weighted.services]]
  5. name = "appv1"
  6. weight = 3
  7. [[http.services.app.weighted.services]]
  8. name = "appv2"
  9. weight = 1
  10. [http.services.appv1]
  11. [http.services.appv1.loadBalancer]
  12. [[http.services.appv1.loadBalancer.servers]]
  13. url = "http://private-ip-server-1/"
  14. [http.services.appv2]
  15. [http.services.appv2.loadBalancer]
  16. [[http.services.appv2.loadBalancer.servers]]
  17. url = "http://private-ip-server-2/"

  1. ## Dynamic configuration
  2. http:
  3. services:
  4. app:
  5. weighted:
  6. services:
  7. - name: appv1
  8. weight: 3
  9. - name: appv2
  10. weight: 1
  11. appv1:
  12. loadBalancer:
  13. servers:
  14. - url: "http://private-ip-server-1/"
  15. appv2:
  16. loadBalancer:
  17. servers:
  18. - url: "http://private-ip-server-2/"

Mirroring (service)

The mirroring is able to mirror requests sent to a service to other services.

Supported Providers

This strategy can be defined currently with the File or IngressRoute providers.

  1. ## Dynamic configuration
  2. [http.services]
  3. [http.services.mirrored-api]
  4. [http.services.mirrored-api.mirroring]
  5. service = "appv1"
  6. [[http.services.mirrored-api.mirroring.mirrors]]
  7. name = "appv2"
  8. percent = 10
  9. [http.services.appv1]
  10. [http.services.appv1.loadBalancer]
  11. [[http.services.appv1.loadBalancer.servers]]
  12. url = "http://private-ip-server-1/"
  13. [http.services.appv2]
  14. [http.services.appv2.loadBalancer]
  15. [[http.services.appv2.loadBalancer.servers]]
  16. url = "http://private-ip-server-2/"

  1. ## Dynamic configuration
  2. http:
  3. services:
  4. mirrored-api:
  5. mirroring:
  6. service: appv1
  7. mirrors:
  8. - name: appv2
  9. percent: 10
  10. appv1:
  11. loadBalancer:
  12. servers:
  13. - url: "http://private-ip-server-1/"
  14. appv2:
  15. loadBalancer:
  16. servers:
  17. - url: "http://private-ip-server-2/"

Configuring TCP Services

General

Each of the fields of the service section represents a kind of service.Which means, that for each specified service, one of the fields, and only one,has to be enabled to define what kind of service is created.Currently, the two available kinds are LoadBalancer, and Weighted.

Servers Load Balancer

The servers load balancer is in charge of balancing the requests between the servers of the same service.

Declaring a Service with Two Servers — Using the File Provider

  1. ## Dynamic configuration
  2. [tcp.services]
  3. [tcp.services.my-service.loadBalancer]
  4. [[tcp.services.my-service.loadBalancer.servers]]
  5. address = "xx.xx.xx.xx:xx"
  6. [[tcp.services.my-service.loadBalancer.servers]]
  7. address = "xx.xx.xx.xx:xx"

  1. ## Dynamic configuration
  2. tcp:
  3. services:
  4. my-service:
  5. loadBalancer:
  6. servers:
  7. - address: "xx.xx.xx.xx:xx"
  8. - address: "xx.xx.xx.xx:xx"

Servers

Servers declare a single instance of your program.The address option (IP:Port) point to a specific instance.

A Service with One Server — Using the File Provider

  1. ## Dynamic configuration
  2. [tcp.services]
  3. [tcp.services.my-service.loadBalancer]
  4. [[tcp.services.my-service.loadBalancer.servers]]
  5. address = "xx.xx.xx.xx:xx"

  1. ## Dynamic configuration
  2. tcp:
  3. services:
  4. my-service:
  5. loadBalancer:
  6. servers:
  7. - address: "xx.xx.xx.xx:xx"

Termination Delay

As a proxy between a client and a server, it can happen that either side (e.g. client side) decides to terminate its writing capability on the connection (i.e. issuance of a FIN packet).The proxy needs to propagate that intent to the other side, and so when that happens, it also does the same on its connection with the other side (e.g. backend side).

However, if for some reason (bad implementation, or malicious intent) the other side does not eventually do the same as well,the connection would stay half-open, which would lock resources for however long.

To that end, as soon as the proxy enters this termination sequence, it sets a deadline on fully terminating the connections on both sides.

The termination delay controls that deadline.It is a duration in milliseconds, defaulting to 100.A negative value means an infinite deadline (i.e. the connection is never fully terminated by the proxy itself).

A Service with a termination delay — Using the File Provider

  1. ## Dynamic configuration
  2. [tcp.services]
  3. [tcp.services.my-service.loadBalancer]
  4. [[tcp.services.my-service.loadBalancer]]
  5. terminationDelay = 200

  1. ## Dynamic configuration
  2. tcp:
  3. services:
  4. my-service:
  5. loadBalancer:
  6. terminationDelay: 200

Weighted Round Robin

The Weighted Round Robin (alias WRR) load-balancer of services is in charge of balancing the requests between multiple services based on provided weights.

This strategy is only available to load balance between services and not between servers.

Supported Providers

This strategy can be defined currently with the File or IngressRoute providers.

  1. ## Dynamic configuration
  2. [tcp.services]
  3. [tcp.services.app]
  4. [[tcp.services.app.weighted.services]]
  5. name = "appv1"
  6. weight = 3
  7. [[tcp.services.app.weighted.services]]
  8. name = "appv2"
  9. weight = 1
  10. [tcp.services.appv1]
  11. [tcp.services.appv1.loadBalancer]
  12. [[tcp.services.appv1.loadBalancer.servers]]
  13. address = "private-ip-server-1:8080/"
  14. [tcp.services.appv2]
  15. [tcp.services.appv2.loadBalancer]
  16. [[tcp.services.appv2.loadBalancer.servers]]
  17. address = "private-ip-server-2:8080/"

  1. ## Dynamic configuration
  2. tcp:
  3. services:
  4. app:
  5. weighted:
  6. services:
  7. - name: appv1
  8. weight: 3
  9. - name: appv2
  10. weight: 1
  11. appv1:
  12. loadBalancer:
  13. servers:
  14. - address: "xxx.xxx.xxx.xxx:8080"
  15. appv2:
  16. loadBalancer:
  17. servers:
  18. - address: "xxx.xxx.xxx.xxx:8080"