Traefik & Consul Catalog

Configuration Example

You can enable the Consul Catalog provider as detailed below:

File (YAML)

  1. providers:
  2. consulCatalog: {}

File (TOML)

  1. [providers.consulCatalog]

CLI

  1. --providers.consulcatalog=true

Attaching tags to services:

  1. - traefik.http.routers.my-router.rule=Host(`example.com`)

Configuration Options

FieldDescriptionDefaultRequired
providers.providersThrottleDurationMinimum amount of time to wait for, after a configuration reload, before taking into account any new configuration refresh event.
If multiple events occur within this time, only the most recent one is taken into account, and all others are discarded.
This option cannot be set per provider, but the throttling algorithm applies to each of them independently.
2sNo
providers.consulCatalog.refreshIntervalDefines the polling interval.15sNo
providers.consulCatalog.prefixDefines the prefix for Consul Catalog tags defining Traefik labels.traefikyes
providers.consulCatalog.requireConsistentForces the read to be fully consistent. See here for more information.falseyes
providers.consulCatalog.exposedByDefaultExpose Consul Catalog services by default in Traefik. If set to false, services that do not have a traefik.enable=true tag will be ignored from the resulting routing configuration. See here.trueno
providers.consulCatalog.defaultRuleThe Default Host rule for all services. See here for more information.“Host({{ normalize .Name }})”No
providers.consulCatalog.connectAwareEnable Consul Connect support. If set to true, Traefik will be enabled to communicate with Connect services.falseNo
providers.consulCatalog.connectByDefaultConsider every service as Connect capable by default. If set to true, Traefik will consider every Consul Catalog service to be Connect capable by default. The option can be overridden on an instance basis with the traefik.consulcatalog.connect tag.falseNo
providers.consulCatalog.serviceNameDefines the name of the Traefik service in Consul Catalog.“traefik”No
providers.consulCatalog.constraintsDefines an expression that Traefik matches against the container labels to determine whether to create any route for that container. See here for more information.“”No
providers.consulCatalog.namespacesDefines the namespaces to query. See here for more information.“”no
providers.consulCatalog.staleInstruct Traefik to use stale consistency for catalog reads.falseno
providers.consulCatalog.cacheInstruct Traefik to use local agent caching for catalog reads.falseno
providers.consulCatalog.endpointDefines the Consul server endpoint.-yes
providers.consulCatalog.endpoint.addressDefines the address of the Consul server.127.0.0.1:8500no
providers.consulCatalog.endpoint.schemeDefines the URI scheme for the Consul server.“”no
providers.consulCatalog.endpoint.datacenterDefines the datacenter to use. If not provided in Traefik, Consul uses the default agent datacenter.“”no
providers.consulCatalog.endpoint.tokenDefines a per-request ACL token which overwrites the agent’s default token.“”no
providers.consulCatalog.endpoint.endpointWaitTimeDefines a duration for which a watch can block. If not provided, the agent default values will be used.“”no
providers.consulCatalog.endpoint.httpAuthDefines authentication settings for the HTTP client using HTTP Basic Authentication.N/Ano
providers.consulCatalog.endpoint.httpAuth.usernameDefines the username to use for HTTP Basic Authentication.“”no
providers.consulCatalog.endpoint.httpAuth.passwordDefines the password to use for HTTP Basic Authentication.“”no
providers.consulCatalog.strictChecksDefine which Consul Service health checks are allowed to take on traffic.“passing,warning”no
providers.consulCatalog.tls.caDefines the path to the certificate authority used for the secure connection to Consul Calatog, it defaults to the system bundle.“”No
providers.consulCatalog.tls.certDefines the path to the public certificate used for the secure connection to Consul Calatog. When using this option, setting the key option is required.“”Yes
providers.consulCatalog.tls.keyDefines the path to the private key used for the secure connection to Consul Catalog. When using this option, setting the cert option is required.“”Yes
providers.consulCatalog.tls.insecureSkipVerifyInstructs the provider to accept any certificate presented by Consul Catalog when establishing a TLS connection, regardless of the hostnames the certificate covers.falseNo
providers.consulCatalog.watchWhen set to true, watches for Consul changes (Consul watches checks).falseNo

requireConsistent

Forces the read to be fully consistent. Setting this option can be expensive due to an extra round-trip but prevents ever performing a stale read.

For more information, see the Consul documentation on consistency.

defaultRule

The default host rule for all services.

For a given service, if no routing rule was defined by a tag, it is defined by this defaultRule instead. The defaultRule must be set to a valid Go template, and can include sprig template functions. The service name can be accessed with the Name identifier, and the template has access to all the labels (i.e. tags beginning with the prefix) defined on this service.

The option can be overridden on an instance basis with the traefik.http.routers.{name-of-your-choice}.rule tag.

File (YAML)

  1. providers:
  2. consulCatalog:
  3. defaultRule: "Host(`{{ .Name }}.{{ index .Labels \"customLabel\"}}`)"
  4. # ...

File (TOML)

  1. [providers.consulCatalog]
  2. defaultRule = "Host(`{{ .Name }}.{{ index .Labels \"customLabel\"}}`)"
  3. # ...

CLI

  1. --providers.consulcatalog.defaultRule="Host(`{{ .Name }}.{{ index .Labels \"customLabel\"}}`)"

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.

constraints

The constraints option can be set to an expression that Traefik matches against the service tags to determine whether to create any route for that service. If none of the service tags match the expression, no route for that service is created. If the expression is empty, all detected services are included.

The expression syntax is based on the Tag(`tag`), and TagRegex(`tag`) functions, as well as the usual boolean logic, as shown in examples below.

Constraints Expression Examples

  1. # Includes only services having the tag `a.tag.name=foo`
  2. constraints = "Tag(`a.tag.name=foo`)"
  1. # Excludes services having any tag `a.tag.name=foo`
  2. constraints = "!Tag(`a.tag.name=foo`)"
  1. # With logical AND.
  2. constraints = "Tag(`a.tag.name`) && Tag(`another.tag.name`)"
  1. # With logical OR.
  2. constraints = "Tag(`a.tag.name`) || Tag(`another.tag.name`)"
  1. # With logical AND and OR, with precedence set by parentheses.
  2. constraints = "Tag(`a.tag.name`) && (Tag(`another.tag.name`) || Tag(`yet.another.tag.name`))"
  1. # Includes only services having a tag matching the `a\.tag\.t.+` regular expression.
  2. constraints = "TagRegex(`a\.tag\.t.+`)"

File (YAML)

  1. providers:
  2. consulCatalog:
  3. constraints: "Tag(`a.tag.name`)"
  4. # ...

File (TOML)

  1. [providers.consulCatalog]
  2. constraints = "Tag(`a.tag.name`)"
  3. # ...

CLI

  1. --providers.consulcatalog.constraints="Tag(`a.tag.name`)"
  2. # ...

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

namespaces

The namespaces option defines the namespaces in which the consul catalog services will be discovered. When using the namespaces option, the discovered configuration object names will be suffixed as shown below:

  1. <resource-name>@consulcatalog-<namespace>

Warning

  • The namespaces option only works with Consul Enterprise, which provides the Namespaces feature.

  • One should only define either the namespaces option or the namespace option.

File (YAML)

  1. providers:
  2. consulCatalog:
  3. namespaces:
  4. - "ns1"
  5. - "ns2"
  6. # ...

File (TOML)

  1. [providers.consulCatalog]
  2. namespaces = ["ns1", "ns2"]
  3. # ...

CLI

  1. --providers.consulcatalog.namespaces=ns1,ns2
  2. # ...

Routing Configuration

See the dedicated section in routing.