6.4 KiB
Traefik & Consul Catalog
A Story of Labels, Services & Containers {: .subtitle }
Attach labels to your services and let Traefik do the rest!
Configuration Examples
??? example "Configuring Consul Catalog & Deploying / Exposing Services"
Enabling the consulcatalog provider
```toml tab="File (TOML)"
[providers.consulcatalog]
```
```yaml tab="File (YAML)"
providers:
consulcatalog: {}
```
```bash tab="CLI"
--providers.consulcatalog=true
```
Attaching labels to services
```yaml
labels:
- traefik.http.services.my-service.rule=Host(`mydomain.com`)
```
Routing Configuration
See the dedicated section in routing.
Provider Configuration
??? tip "Browse the Reference" If you're in a hurry, maybe you'd rather go through the configuration reference:
```toml tab="File (TOML)"
--8<-- "content/providers/consul-catalog.toml"
```
```yaml tab="File (YAML)"
--8<-- "content/providers/consul-catalog.yml"
```
```bash tab="CLI"
--8<-- "content/providers/consul-catalog.txt"
```
exposedByDefault
Optional, Default=true
[providers.consulcatalog]
exposedByDefault = false
# ...
providers:
consulcatalog:
exposedByDefault: false
# ...
--providers.consulcatalog.exposedByDefault=false
# ...
Expose Consul Catalog services by default in Traefik.
If set to false, services that don't have a traefik.enable=true
label will be ignored from the resulting routing configuration.
See also Restrict the Scope of Service Discovery.
defaultRule
Optional, Default=Host(`{{ normalize .Name }}`)
[providers.consulcatalog]
defaultRule = "Host(`{{ .Name }}.{{ index .Labels \"customLabel\"}}`)"
# ...
providers:
consulcatalog:
defaultRule: "Host(`{{ .Name }}.{{ index .Labels \"customLabel\"}}`)"
# ...
--providers.consulcatalog.defaultRule="Host(`{{ .Name }}.{{ index .Labels \"customLabel\"}}`)"
# ...
The default host rule for all services.
For a given container if no routing rule was defined by a label, it is defined by this defaultRule instead.
It must be a valid Go template,
augmented with the sprig template functions.
The service name can be accessed as 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.
enableServiceHealthFilter
Optional, Default=true
[providers.consulcatalog]
enableServiceHealthFilter = false
# ...
providers:
consulcatalog:
enableServiceHealthFilter: false
# ...
--providers.consulcatalog.enableServiceHealthFilter=false
# ...
Filter services with unhealthy states and inactive states.
refreshSeconds
Optional, Default=15
[providers.consulcatalog]
refreshSeconds = 30
# ...
providers:
consulcatalog:
refreshSeconds: 30
# ...
--providers.consulcatalog.refreshSeconds=30
# ...
Defines the polling interval (in seconds).
intervalPoll
Optional, Default=false
[providers.consulcatalog]
intervalPoll = true
# ...
providers:
consulcatalog:
intervalPoll: true
# ...
--providers.consulcatalog.intervalPoll=true
# ...
Poll the Consul Catalog metadata service for changes every consulcatalog.refreshSeconds
,
which is less accurate than the default long polling technique which will provide near instantaneous updates to Traefik.
prefix
Optional, Default=/latest
[providers.consulcatalog]
prefix = "/test"
# ...
providers:
consulcatalog:
prefix: "/test"
# ...
--providers.consulcatalog.prefix="/test"
# ...
Prefix used for accessing the Consul Catalog service
constraints
Optional, Default=""
[providers.consulcatalog]
constraints = "Label(`a.label.name`, `foo`)"
# ...
providers:
consulcatalog:
constraints: "Label(`a.label.name`, `foo`)"
# ...
--providers.consulcatalog.constraints="Label(`a.label.name`, `foo`)"
# ...
Constraints is an expression that Traefik matches against the container's labels to determine whether to create any route for that container. That is to say, if none of the container's labels match the expression, no route for the 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.+`)"
```
See also Restrict the Scope of Service Discovery.