traefik/docs/content/providers/rancher.md
2024-11-18 14:40:05 +01:00

7 KiB


title: ""Traefik Configuration Discovery: Rancher"" description: "Read the official Traefik documentation to learn how to expose Rancher services by default in Traefik Proxy."

Traefik & Rancher

A Story of Labels, Services & Containers {: .subtitle }

Rancher

Attach labels to your services and let Traefik do the rest!

!!! important "This provider is specific to Rancher 1.x."

Rancher 2.x requires Kubernetes and does not have a metadata endpoint of its own for Traefik to query.
As such, Rancher 2.x users should utilize the [Kubernetes CRD provider](./kubernetes-crd.md) directly.

Configuration Examples

??? example "Configuring Rancher & Deploying / Exposing Services"

Enabling the Rancher provider

```yaml tab="File (YAML)"
providers:
  rancher: {}
```

```toml tab="File (TOML)"
[providers.rancher]
```

```bash tab="CLI"
--providers.rancher=true
```

Attaching labels to services

```yaml
labels:
  - traefik.http.services.my-service.rule=Host(`example.com`)
```

Routing Configuration

See the dedicated section in routing.

Provider Configuration

??? tip "Browse the Reference"

For an overview of all the options that can be set with the Rancher provider, see the following snippets:

```yaml tab="File (YAML)"
--8<-- "content/providers/rancher.yml"
```

```toml tab="File (TOML)"
--8<-- "content/providers/rancher.toml"
```

```bash tab="CLI"
--8<-- "content/providers/rancher.txt"
```

exposedByDefault

Optional, Default=true

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.

providers:
  rancher:
    exposedByDefault: false
    # ...
[providers.rancher]
  exposedByDefault = false
  # ...
--providers.rancher.exposedByDefault=false
# ...

defaultRule

Optional, Default=Host(`{{ normalize .Name }}`)

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.

It must be a valid Go template, and can use sprig template functions. 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.

providers:
  rancher:
    defaultRule: "Host(`{{ .Name }}.{{ index .Labels \"customLabel\"}}`)"
    # ...
[providers.rancher]
  defaultRule = "Host(`{{ .Name }}.{{ index .Labels \"customLabel\"}}`)"
  # ...
--providers.rancher.defaultRule='Host(`{{ .Name }}.{{ index .Labels "customLabel"}}`)'
# ...

??? 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.

enableServiceHealthFilter

Optional, Default=true

Filter out services with unhealthy states and inactive states.

providers:
  rancher:
    enableServiceHealthFilter: false
    # ...
[providers.rancher]
  enableServiceHealthFilter = false
  # ...
--providers.rancher.enableServiceHealthFilter=false
# ...

refreshSeconds

Optional, Default=15

Defines the polling interval (in seconds).

providers:
  rancher:
    refreshSeconds: 30
    # ...
[providers.rancher]
  refreshSeconds = 30
  # ...
--providers.rancher.refreshSeconds=30
# ...

intervalPoll

Optional, Default=false

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.

providers:
  rancher:
    intervalPoll: true
    # ...
[providers.rancher]
  intervalPoll = true
  # ...
--providers.rancher.intervalPoll=true
# ...

prefix

Optional, Default="/latest"

Prefix used for accessing the Rancher metadata service.

providers:
  rancher:
    prefix: "/test"
    # ...
[providers.rancher]
  prefix = "/test"
  # ...
--providers.rancher.prefix=/test
# ...

constraints

Optional, Default=""

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.

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`)"
```

```toml
# Excludes containers having any label with key `a.label.name` and value `foo`
constraints = "!Label(`a.label.name`, `value`)"
```

```toml
# With logical AND.
constraints = "Label(`a.label.name`, `valueA`) && Label(`another.label.name`, `valueB`)"
```

```toml
# With logical OR.
constraints = "Label(`a.label.name`, `valueA`) || Label(`another.label.name`, `valueB`)"
```

```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`))"
```

```toml
# Includes only containers having a label with key `a.label.name` and a value matching the `a.+` regular expression.
constraints = "LabelRegex(`a.label.name`, `a.+`)"
```

For additional information, refer to Restrict the Scope of Service Discovery.

providers:
  rancher:
    constraints: "Label(`a.label.name`,`foo`)"
    # ...
[providers.rancher]
  constraints = "Label(`a.label.name`,`foo`)"
  # ...
--providers.rancher.constraints=Label(`a.label.name`,`foo`)
# ...