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 (TOML)

  1. [providers.consulCatalog]

File (YAML)

  1. providers:
  2. 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

File (TOML)

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

File (YAML)

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

CLI

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

Defines the polling interval.

prefix

required, Default=”traefik”

File (TOML)

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

File (YAML)

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

CLI

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

The prefix for Consul Catalog tags defining traefik labels.

requireConsistent

Optional, Default=false

File (TOML)

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

File (YAML)

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

CLI

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

Forces the read to be fully consistent.

stale

Optional, Default=false

File (TOML)

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

File (YAML)

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

CLI

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

Use stale consistency for catalog reads.

cache

Optional, Default=false

File (TOML)

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

File (YAML)

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

CLI

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

Use local agent caching for catalog reads.

endpoint

Defines the Consul server endpoint.

address

Optional, Default=”http://127.0.0.1:8500

File (TOML)

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

File (YAML)

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

CLI

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

Defines the address of the Consul server.

scheme

Optional, Default=””

File (TOML)

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

File (YAML)

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

CLI

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

Defines the URI scheme for the Consul server.

datacenter

Optional, Default=””

File (TOML)

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

File (YAML)

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

CLI

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

Defines the Data center to use. If not provided, the default agent data center is used.

token

Optional, Default=””

File (TOML)

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

File (YAML)

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

CLI

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

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

endpointWaitTime

Optional, Default=””

File (TOML)

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

File (YAML)

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

CLI

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

WaitTime limits how long a Watch will block. If not provided, the agent default values will be used

httpAuth

Optional

Used to authenticate http client with HTTP Basic Authentication.

username

Optional

File (TOML)

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

File (YAML)

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

CLI

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

Username to use for HTTP Basic Authentication

password

Optional

File (TOML)

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

File (YAML)

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

CLI

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

Password to use for HTTP Basic Authentication

tls

Optional

Defines TLS options for Consul server endpoint.

ca

Optional

File (TOML)

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

File (YAML)

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

CLI

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

ca is the path to the CA certificate used for Consul communication, defaults to the system bundle if not specified.

caOptional

Optional

File (TOML)

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

File (YAML)

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

CLI

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

Policy followed for the secured connection with TLS Client Authentication to Consul. Requires tls.ca to be defined.

  • true: VerifyClientCertIfGiven
  • false: RequireAndVerifyClientCert
  • if tls.ca is undefined NoClientCert
cert

Optional

File (TOML)

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

File (YAML)

  1. providers:
  2. consulCatalog:
  3. endpoint:
  4. tls:
  5. cert: path/to/foo.cert
  6. 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

cert is the path to the public certificate for Consul communication. If this is set then you need to also set `key.

key

Optional

File (TOML)

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

File (YAML)

  1. providers:
  2. consulCatalog:
  3. endpoint:
  4. tls:
  5. cert: path/to/foo.cert
  6. 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 is the path to the private key for Consul communication. If this is set then you need to also set cert.

insecureSkipVerify

Optional

File (TOML)

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

File (YAML)

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

CLI

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

If insecureSkipVerify is true, TLS for the connection to Consul server accepts any certificate presented by the server and any host name in that certificate.

exposedByDefault

Optional, Default=true

File (TOML)

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

File (YAML)

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

CLI

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

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.

See also Restrict the Scope of Service Discovery.

defaultRule

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

File (TOML)

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

File (YAML)

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

CLI

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

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

constraints

Optional, Default=””

File (TOML)

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

File (YAML)

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

CLI

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

Constraints is an expression that Traefik matches against the service’s tags to determine whether to create any route for that service. That is to say, if none of the service’s 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.+`)"

See also Restrict the Scope of Service Discovery.