diff --git a/docs/basics.md b/docs/basics.md index df967f308..e0c700a16 100644 --- a/docs/basics.md +++ b/docs/basics.md @@ -222,6 +222,17 @@ For example: - Another possible value for `extractorfunc` is `client.ip` which will categorize requests based on client source ip. - Lastly `extractorfunc` can take the value of `request.header.ANY_HEADER` which will categorize requests based on `ANY_HEADER` that you provide. +Sticky sessions are supported with both load balancers. When sticky sessions are enabled, a cookie called `_TRAEFIK_BACKEND` is set on the initial +request. On subsequent requests, the client will be directed to the backend stored in the cookie if it is still healthy. If not, a new backend +will be assigned. + +For example: +```toml +[backends] + [backends.backend1] + [backends.backend1.loadbalancer] + sticky = true +``` ## Servers Servers are simply defined using a `URL`. You can also apply a custom `weight` to each server (this will be used by load-balancing). diff --git a/docs/toml.md b/docs/toml.md index 0c5557d04..bd6dfb11e 100644 --- a/docs/toml.md +++ b/docs/toml.md @@ -716,6 +716,7 @@ Labels can be used on containers to override default behaviour: - `traefik.protocol=https`: override the default `http` protocol - `traefik.weight=10`: assign this weight to the container - `traefik.enable=false`: disable this container in Træfɪk +- `traefik.stickysession=true`: enable backend sticky sessions - `traefik.frontend.rule=Host:test.traefik.io`: override the default frontend rule (Default: `Host:{containerName}.{domain}`). - `traefik.frontend.passHostHeader=true`: forward client `Host` header to the backend. - `traefik.frontend.priority=10`: override default frontend priority diff --git a/provider/docker.go b/provider/docker.go index e33c11831..eb08b6afb 100644 --- a/provider/docker.go +++ b/provider/docker.go @@ -439,6 +439,13 @@ func (provider *Docker) getWeight(container dockerData) string { return "1" } +func (provider *Docker) getSticky(container dockertypes.ContainerJSON) string { + if label, err := getLabel(container, "traefik.stickysession"); err == nil { + return true + } + return false +} + func (provider *Docker) getDomain(container dockerData) string { if label, err := getLabel(container, "traefik.domain"); err == nil { return label diff --git a/templates/docker.tmpl b/templates/docker.tmpl index 5b9acce33..943a89237 100644 --- a/templates/docker.tmpl +++ b/templates/docker.tmpl @@ -7,6 +7,7 @@ {{if hasLoadBalancerLabel .}} [backends.backend-{{getBackend .}}.loadbalancer] method = "{{getLoadBalancerMethod .}}" + sticky = {{getSticky . }} {{end}} {{if hasMaxConnLabels .}}