traefik/docs/content/providers/rancher.md

294 lines
7 KiB
Markdown
Raw Normal View History

---
title: ""Traefik Configuration Discovery: Rancher""
description: "Read the official Traefik documentation to learn how to expose Rancher services by default in Traefik Proxy."
---
2019-04-05 10:22:04 +00:00
# Traefik & Rancher
A Story of Labels, Services & Containers
2019-04-05 10:22:04 +00:00
{: .subtitle }
![Rancher](../assets/img/providers/rancher.png)
Attach labels to your services and let Traefik do the rest!
2019-09-23 12:32:04 +00:00
!!! important "This provider is specific to Rancher 1.x."
2021-02-11 18:04:03 +00:00
Rancher 2.x requires Kubernetes and does not have a metadata endpoint of its own for Traefik to query.
2021-02-11 18:04:03 +00:00
As such, Rancher 2.x users should utilize the [Kubernetes CRD provider](./kubernetes-crd.md) directly.
2019-04-05 10:22:04 +00:00
## Configuration Examples
2019-04-26 09:48:04 +00:00
??? example "Configuring Rancher & Deploying / Exposing Services"
2019-04-05 10:22:04 +00:00
2021-02-11 18:04:03 +00:00
Enabling the Rancher provider
2019-04-05 10:22:04 +00:00
2019-07-02 15:36:04 +00:00
```yaml tab="File (YAML)"
providers:
rancher: {}
```
2021-02-11 18:04:03 +00:00
```toml tab="File (TOML)"
[providers.rancher]
```
2019-07-02 15:36:04 +00:00
```bash tab="CLI"
2019-07-22 07:58:04 +00:00
--providers.rancher=true
2019-07-02 15:36:04 +00:00
```
2019-04-05 10:22:04 +00:00
Attaching labels to services
```yaml
labels:
2020-03-13 21:50:05 +00:00
- traefik.http.services.my-service.rule=Host(`example.com`)
2019-04-05 10:22:04 +00:00
```
2019-09-23 12:32:04 +00:00
## Routing Configuration
See the dedicated section in [routing](../routing/providers/rancher.md).
## Provider Configuration
2019-04-05 10:22:04 +00:00
??? tip "Browse the Reference"
2021-02-11 18:04:03 +00:00
For an overview of all the options that can be set with the Rancher provider, see the following snippets:
2019-07-02 15:36:04 +00:00
```yaml tab="File (YAML)"
--8<-- "content/providers/rancher.yml"
```
2021-02-11 18:04:03 +00:00
```toml tab="File (TOML)"
--8<-- "content/providers/rancher.toml"
```
2019-07-02 15:36:04 +00:00
```bash tab="CLI"
--8<-- "content/providers/rancher.txt"
```
2019-04-05 10:22:04 +00:00
2019-07-02 15:36:04 +00:00
### `exposedByDefault`
2019-04-05 10:22:04 +00:00
_Optional, Default=true_
2021-02-11 18:04:03 +00:00
Expose Rancher services by default in Traefik.
If set to `false`, services that do not have a `traefik.enable=true` label are ignored from the resulting routing configuration.
For additional information, refer to [Restrict the Scope of Service Discovery](./overview.md#restrict-the-scope-of-service-discovery).
2019-07-02 15:36:04 +00:00
```yaml tab="File (YAML)"
providers:
rancher:
exposedByDefault: false
# ...
```
```toml tab="File (TOML)"
[providers.rancher]
exposedByDefault = false
# ...
```
2019-07-02 15:36:04 +00:00
```bash tab="CLI"
--providers.rancher.exposedByDefault=false
# ...
```
### `defaultRule`
2019-04-05 10:22:04 +00:00
_Optional, Default=```Host(`{{ normalize .Name }}`)```_
2019-04-05 10:22:04 +00:00
2021-02-11 18:04:03 +00:00
The default host rule for all services.
The `defaultRule` option defines what routing rule to apply to a container if no rule is defined by a label.
2021-09-10 09:42:07 +00:00
It must be a valid [Go template](https://pkg.go.dev/text/template/), and can use
2021-08-31 16:54:06 +00:00
[sprig template functions](https://masterminds.github.io/sprig/).
2021-02-11 18:04:03 +00:00
The service name can be accessed with the `Name` identifier,
and the template has access to all the labels defined on this container.
This option can be overridden on a container basis with the `traefik.http.routers.Router1.rule` label.
2019-07-02 15:36:04 +00:00
```yaml tab="File (YAML)"
providers:
rancher:
defaultRule: "Host(`{{ .Name }}.{{ index .Labels \"customLabel\"}}`)"
# ...
```
```toml tab="File (TOML)"
[providers.rancher]
defaultRule = "Host(`{{ .Name }}.{{ index .Labels \"customLabel\"}}`)"
# ...
```
2019-07-02 15:36:04 +00:00
```bash tab="CLI"
--providers.rancher.defaultRule=Host(`{{ .Name }}.{{ index .Labels \"customLabel\"}}`)
2019-07-02 15:36:04 +00:00
# ...
```
??? info "Default rule and Traefik service"
The exposure of the Traefik container, combined with the default rule mechanism,
can lead to create a router targeting itself in a loop.
In this case, to prevent an infinite loop,
Traefik adds an internal middleware to refuse the request if it comes from the same router.
2019-07-02 15:36:04 +00:00
### `enableServiceHealthFilter`
_Optional, Default=true_
2021-02-11 18:04:03 +00:00
Filter out services with unhealthy states and inactive states.
2019-07-02 15:36:04 +00:00
```yaml tab="File (YAML)"
providers:
rancher:
enableServiceHealthFilter: false
# ...
2019-04-26 09:48:04 +00:00
```
```toml tab="File (TOML)"
[providers.rancher]
enableServiceHealthFilter = false
# ...
```
2019-07-02 15:36:04 +00:00
```bash tab="CLI"
--providers.rancher.enableServiceHealthFilter=false
# ...
```
2019-04-05 10:22:04 +00:00
2019-07-02 15:36:04 +00:00
### `refreshSeconds`
2019-04-05 10:22:04 +00:00
_Optional, Default=15_
2021-02-11 18:04:03 +00:00
Defines the polling interval (in seconds).
2019-07-02 15:36:04 +00:00
```yaml tab="File (YAML)"
providers:
rancher:
refreshSeconds: 30
# ...
```
```toml tab="File (TOML)"
[providers.rancher]
refreshSeconds = 30
# ...
```
2019-07-02 15:36:04 +00:00
```bash tab="CLI"
--providers.rancher.refreshSeconds=30
# ...
```
### `intervalPoll`
2019-04-05 10:22:04 +00:00
_Optional, Default=false_
2021-02-11 18:04:03 +00:00
Poll the Rancher metadata service for changes every `rancher.refreshSeconds`,
which is less accurate than the default long polling technique which provides near instantaneous updates to Traefik.
2019-07-02 15:36:04 +00:00
```yaml tab="File (YAML)"
providers:
rancher:
intervalPoll: true
# ...
```
```toml tab="File (TOML)"
[providers.rancher]
intervalPoll = true
# ...
```
2019-07-02 15:36:04 +00:00
```bash tab="CLI"
--providers.rancher.intervalPoll=true
# ...
```
### `prefix`
2019-04-05 10:22:04 +00:00
2021-02-11 18:04:03 +00:00
_Optional, Default="/latest"_
Prefix used for accessing the Rancher metadata service.
2019-04-05 10:22:04 +00:00
2019-07-02 15:36:04 +00:00
```yaml tab="File (YAML)"
providers:
rancher:
prefix: "/test"
# ...
```
```toml tab="File (TOML)"
[providers.rancher]
prefix = "/test"
# ...
```
2019-07-02 15:36:04 +00:00
```bash tab="CLI"
--providers.rancher.prefix=/test
2019-07-02 15:36:04 +00:00
# ...
```
### `constraints`
_Optional, Default=""_
2021-02-11 18:04:03 +00:00
The `constraints` option can be set to an expression that Traefik matches against the container labels to determine whether
to create any route for that container. If none of the container tags match the expression, no route for that container is
created. If the expression is empty, all detected containers are included.
2019-07-02 15:36:04 +00:00
2021-02-11 18:04:03 +00:00
The expression syntax is based on the `Label("key", "value")`, and `LabelRegex("key", "value")` functions, as well as
the usual boolean logic, as shown in examples below.
??? example "Constraints Expression Examples"
```toml
# Includes only containers having a label with key `a.label.name` and value `foo`
constraints = "Label(`a.label.name`, `foo`)"
```
2021-02-11 18:04:03 +00:00
```toml
# Excludes containers having any label with key `a.label.name` and value `foo`
constraints = "!Label(`a.label.name`, `value`)"
```
2021-02-11 18:04:03 +00:00
```toml
# With logical AND.
constraints = "Label(`a.label.name`, `valueA`) && Label(`another.label.name`, `valueB`)"
```
2021-02-11 18:04:03 +00:00
```toml
# With logical OR.
constraints = "Label(`a.label.name`, `valueA`) || Label(`another.label.name`, `valueB`)"
```
2021-02-11 18:04:03 +00:00
```toml
# With logical AND and OR, with precedence set by parentheses.
constraints = "Label(`a.label.name`, `valueA`) && (Label(`another.label.name`, `valueB`) || Label(`yet.another.label.name`, `valueC`))"
```
2021-02-11 18:04:03 +00:00
```toml
# Includes only containers having a label with key `a.label.name` and a value matching the `a.+` regular expression.
2019-09-09 08:36:08 +00:00
constraints = "LabelRegex(`a.label.name`, `a.+`)"
```
2021-02-11 18:04:03 +00:00
For additional information, refer to [Restrict the Scope of Service Discovery](./overview.md#restrict-the-scope-of-service-discovery).
```yaml tab="File (YAML)"
providers:
rancher:
constraints: "Label(`a.label.name`,`foo`)"
# ...
```
```toml tab="File (TOML)"
[providers.rancher]
constraints = "Label(`a.label.name`,`foo`)"
# ...
```
2021-02-11 18:04:03 +00:00
```bash tab="CLI"
--providers.rancher.constraints=Label(`a.label.name`,`foo`)
# ...
```