Traefik & Consul Catalog

A Story of Tags, Services & Instances

Consul Catalog

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

Configuration Examples

Configuring Consul Catalog & Deploying / Exposing Services

Enabling the consul catalog provider

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

Routing Configuration

See the dedicated section in routing.

Provider Configuration

refreshInterval

Optional, Default=15s

Defines the polling interval.

File (YAML)

  1. providers:
  2. consulCatalog:
  3. refreshInterval: 30s
  4. # ...

File (TOML)

  1. [providers.consulCatalog]
  2. refreshInterval = "30s"
  3. # ...

CLI

  1. --providers.consulcatalog.refreshInterval=30s
  2. # ...

prefix

required, Default=”traefik”

The prefix for Consul Catalog tags defining Traefik labels.

File (YAML)

  1. providers:
  2. consulCatalog:
  3. prefix: test
  4. # ...

File (TOML)

  1. [providers.consulCatalog]
  2. prefix = "test"
  3. # ...

CLI

  1. --providers.consulcatalog.prefix=test
  2. # ...

requireConsistent

Optional, Default=false

Forces the read to be fully consistent.

It is more expensive due to an extra round-trip but prevents ever performing a stale read.

For more information, see the consul documentation on consistency.

File (YAML)

  1. providers:
  2. consulCatalog:
  3. requireConsistent: true
  4. # ...

File (TOML)

  1. [providers.consulCatalog]
  2. requireConsistent = true
  3. # ...

CLI

  1. --providers.consulcatalog.requireConsistent=true
  2. # ...

stale

Optional, Default=false

Use stale consistency for catalog reads.

This makes reads very fast and scalable at the cost of a higher likelihood of stale values.

For more information, see the consul documentation on consistency.

File (YAML)

  1. providers:
  2. consulCatalog:
  3. stale: true
  4. # ...

File (TOML)

  1. [providers.consulCatalog]
  2. stale = true
  3. # ...

CLI

  1. --providers.consulcatalog.stale=true
  2. # ...

cache

Optional, Default=false

Use local agent caching for catalog reads.

File (YAML)

  1. providers:
  2. consulCatalog:
  3. cache: true
  4. # ...

File (TOML)

  1. [providers.consulCatalog]
  2. cache = true
  3. # ...

CLI

  1. --providers.consulcatalog.cache=true
  2. # ...

endpoint

Defines the Consul server endpoint.

address

Defines the address of the Consul server.

Optional, Default=”127.0.0.1:8500”

File (YAML)

  1. providers:
  2. consulCatalog:
  3. endpoint:
  4. address: 127.0.0.1:8500
  5. # ...

File (TOML)

  1. [providers.consulCatalog]
  2. [providers.consulCatalog.endpoint]
  3. address = "127.0.0.1:8500"
  4. # ...

CLI

  1. --providers.consulcatalog.endpoint.address=127.0.0.1:8500
  2. # ...

scheme

Optional, Default=””

Defines the URI scheme for the Consul server.

File (YAML)

  1. providers:
  2. consulCatalog:
  3. endpoint:
  4. scheme: https
  5. # ...

File (TOML)

  1. [providers.consulCatalog]
  2. [providers.consulCatalog.endpoint]
  3. scheme = "https"
  4. # ...

CLI

  1. --providers.consulcatalog.endpoint.scheme=https
  2. # ...

datacenter

Optional, Default=””

Defines the datacenter to use. If not provided in Traefik, Consul uses the default agent datacenter.

File (YAML)

  1. providers:
  2. consulCatalog:
  3. endpoint:
  4. datacenter: test
  5. # ...

File (TOML)

  1. [providers.consulCatalog]
  2. [providers.consulCatalog.endpoint]
  3. datacenter = "test"
  4. # ...

CLI

  1. --providers.consulcatalog.endpoint.datacenter=test
  2. # ...

token

Optional, Default=””

Token is used to provide a per-request ACL token which overwrites the agent’s default token.

File (YAML)

  1. providers:
  2. consulCatalog:
  3. endpoint:
  4. token: test
  5. # ...

File (TOML)

  1. [providers.consulCatalog]
  2. [providers.consulCatalog.endpoint]
  3. token = "test"
  4. # ...

CLI

  1. --providers.consulcatalog.endpoint.token=test
  2. # ...

endpointWaitTime

Optional, Default=””

Limits the duration for which a Watch can block. If not provided, the agent default values will be used.

File (YAML)

  1. providers:
  2. consulCatalog:
  3. endpoint:
  4. endpointWaitTime: 15s
  5. # ...

File (TOML)

  1. [providers.consulCatalog]
  2. [providers.consulCatalog.endpoint]
  3. endpointWaitTime = "15s"
  4. # ...

CLI

  1. --providers.consulcatalog.endpoint.endpointwaittime=15s
  2. # ...

httpAuth

Optional

Used to authenticate the HTTP client using HTTP Basic Authentication.

username

Optional, Default=””

Username to use for HTTP Basic Authentication.

File (YAML)

  1. providers:
  2. consulCatalog:
  3. endpoint:
  4. httpAuth:
  5. username: test

File (TOML)

  1. [providers.consulCatalog.endpoint.httpAuth]
  2. username = "test"

CLI

  1. --providers.consulcatalog.endpoint.httpauth.username=test
password

Optional, Default=””

Password to use for HTTP Basic Authentication.

File (YAML)

  1. providers:
  2. consulCatalog:
  3. endpoint:
  4. httpAuth:
  5. password: test

File (TOML)

  1. [providers.consulCatalog.endpoint.httpAuth]
  2. password = "test"

CLI

  1. --providers.consulcatalog.endpoint.httpauth.password=test

tls

Optional

Defines the TLS configuration used for the secure connection to Consul Catalog.

ca

Optional

ca is the path to the certificate authority used for the secure connection to Consul Catalog, it defaults to the system bundle.

File (YAML)

  1. providers:
  2. consulCatalog:
  3. endpoint:
  4. tls:
  5. ca: path/to/ca.crt

File (TOML)

  1. [providers.consulCatalog.endpoint.tls]
  2. ca = "path/to/ca.crt"

CLI

  1. --providers.consulcatalog.endpoint.tls.ca=path/to/ca.crt
cert

Optional

cert is the path to the public certificate used for the secure connection to Consul Catalog. When using this option, setting the key option is required.

File (YAML)

  1. providers:
  2. consulCatalog:
  3. endpoint:
  4. tls:
  5. cert: path/to/foo.cert
  6. key: path/to/foo.key

File (TOML)

  1. [providers.consulCatalog.endpoint.tls]
  2. cert = "path/to/foo.cert"
  3. key = "path/to/foo.key"

CLI

  1. --providers.consulcatalog.endpoint.tls.cert=path/to/foo.cert
  2. --providers.consulcatalog.endpoint.tls.key=path/to/foo.key
key

Optional

key is the path to the private key used for the secure connection to Consul Catalog. When using this option, setting the cert option is required.

File (YAML)

  1. providers:
  2. consulCatalog:
  3. endpoint:
  4. tls:
  5. cert: path/to/foo.cert
  6. key: path/to/foo.key

File (TOML)

  1. [providers.consulCatalog.endpoint.tls]
  2. cert = "path/to/foo.cert"
  3. key = "path/to/foo.key"

CLI

  1. --providers.consulcatalog.endpoint.tls.cert=path/to/foo.cert
  2. --providers.consulcatalog.endpoint.tls.key=path/to/foo.key
insecureSkipVerify

Optional, Default=false

If insecureSkipVerify is true, the TLS connection to Consul accepts any certificate presented by the server regardless of the hostnames it covers.

File (YAML)

  1. providers:
  2. consulCatalog:
  3. endpoint:
  4. tls:
  5. insecureSkipVerify: true

File (TOML)

  1. [providers.consulCatalog.endpoint.tls]
  2. insecureSkipVerify = true

CLI

  1. --providers.consulcatalog.endpoint.tls.insecureskipverify=true

exposedByDefault

Optional, Default=true

Expose Consul Catalog services by default in Traefik. If set to false, services that don’t have a traefik.enable=true tag will be ignored from the resulting routing configuration.

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

File (YAML)

  1. providers:
  2. consulCatalog:
  3. exposedByDefault: false
  4. # ...

File (TOML)

  1. [providers.consulCatalog]
  2. exposedByDefault = false
  3. # ...

CLI

  1. --providers.consulcatalog.exposedByDefault=false
  2. # ...

defaultRule

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

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\"}}`)
  2. # ...

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.

connectAware

Optional, Default=false

Enable Consul Connect support. If set to true, Traefik will be enabled to communicate with Connect services.

File (TOML)

  1. [providers.consulCatalog]
  2. connectAware = true
  3. # ...

File (YAML)

  1. providers:
  2. consulCatalog:
  3. connectAware: true
  4. # ...

CLI

  1. --providers.consulcatalog.connectAware=true
  2. # ...

connectByDefault

Optional, Default=false

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

File (TOML)

  1. [providers.consulCatalog]
  2. connectByDefault = true
  3. # ...

File (YAML)

  1. providers:
  2. consulCatalog:
  3. connectByDefault: true
  4. # ...

CLI

  1. --providers.consulcatalog.connectByDefault=true
  2. # ...

serviceName

Optional, Default=”traefik”

Name of the Traefik service in Consul Catalog.

File (TOML)

  1. [providers.consulCatalog]
  2. serviceName = "test"
  3. # ...

File (YAML)

  1. providers:
  2. consulCatalog:
  3. serviceName: test
  4. # ...

CLI

  1. --providers.consulcatalog.serviceName=test
  2. # ...

constraints

Optional, Default=””

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.

namespace

Deprecated in favor of the namespaces option.

Optional, Default=””

The namespace option defines the namespace in which the consul catalog services will be discovered.

Warning

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

Warning

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

File (YAML)

  1. providers:
  2. consulCatalog:
  3. namespace: "production"
  4. # ...

File (TOML)

  1. [providers.consulCatalog]
  2. namespace = "production"
  3. # ...

CLI

  1. --providers.consulcatalog.namespace=production
  2. # ...

namespaces

Optional, Default=””

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.

Warning

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

watch

Optional, Default=false

When set to true, watches for Consul changes (Consul watches checks).

File (YAML)

  1. providers:
  2. consulCatalog:
  3. watch: true
  4. # ...

File (TOML)

  1. [providers.consulCatalog]
  2. watch = true
  3. # ...

CLI

  1. --providers.consulcatalog.watch=true
  2. # ...