Traefik & Nomad Service Discovery

A Story of Tags, Services & Nomads

Nomad Service Discovery

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

Configuration Examples

Configuring Nomad & Deploying Services

Enabling the nomad provider

File (YAML)

  1. providers:
  2. nomad: {}

File (TOML)

  1. [providers.nomad]

CLI

  1. --providers.nomad=true

Attaching tags to services:

  1. ...
  2. service {
  3. name = "myService"
  4. tags = [
  5. "traefik.http.routers.my-router.rule=Host(`example.com`)",
  6. ]
  7. }
  8. ...

Routing Configuration

See the dedicated section in routing.

Provider Configuration

refreshInterval

Optional, Default=15s

Defines the polling interval.

This option is ignored when the watch mode is enabled.

File (YAML)

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

File (TOML)

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

CLI

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

watch

Optional, Default=false

Enables the watch mode to refresh the configuration on a per-event basis.

File (YAML)

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

File (TOML)

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

CLI

  1. --providers.nomad.watch
  2. # ...

throttleDuration

Optional, Default=0s

The throttleDuration option defines how often the provider is allowed to handle service events from Nomad. This prevents a Nomad cluster that updates many times per second from continuously changing your Traefik configuration.

If left empty, the provider does not apply any throttling and does not drop any Nomad service events.

The value of throttleDuration should be provided in seconds or as a valid duration format, see time.ParseDuration.

This option is only compatible with the watch mode.

File (YAML)

  1. providers:
  2. nomad:
  3. throttleDuration: 2s
  4. # ...

File (TOML)

  1. [providers.nomad]
  2. throttleDuration = "2s"
  3. # ...

CLI

  1. --providers.nomad.throttleDuration=2s
  2. # ...

prefix

required, Default=”traefik”

The prefix for Nomad service tags defining Traefik labels.

File (YAML)

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

File (TOML)

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

CLI

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

stale

Optional, Default=false

Use stale consistency for Nomad service API reads.

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

For more information, see the Nomad documentation on consistency.

File (YAML)

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

File (TOML)

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

CLI

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

endpoint

Defines the Nomad server endpoint.

address

Defines the address of the Nomad server.

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

File (YAML)

  1. providers:
  2. nomad:
  3. endpoint:
  4. address: http://127.0.0.1:4646
  5. # ...

File (TOML)

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

CLI

  1. --providers.nomad.endpoint.address=http://127.0.0.1:4646
  2. # ...

token

Optional, Default=””

Token is used to provide a per-request ACL token, if Nomad ACLs are enabled. The appropriate ACL privilege for this token is ‘read-job’, as outlined in the Nomad documentation on ACL.

File (YAML)

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

File (TOML)

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

CLI

  1. --providers.nomad.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. nomad:
  3. endpoint:
  4. endpointWaitTime: 15s
  5. # ...

File (TOML)

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

CLI

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

tls

Optional

Defines the TLS configuration used for the secure connection to the Nomad API.

ca

Optional

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

File (YAML)

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

File (TOML)

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

CLI

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

Optional

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

File (YAML)

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

File (TOML)

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

CLI

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

Optional

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

File (YAML)

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

File (TOML)

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

CLI

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

Optional, Default=false

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

File (YAML)

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

File (TOML)

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

CLI

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

exposedByDefault

Optional, Default=true

Expose Nomad 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.

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

File (YAML)

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

File (TOML)

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

CLI

  1. --providers.nomad.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. nomad:
  3. defaultRule: "Host(`{{ .Name }}.{{ index .Labels \"customLabel\"}}`)"
  4. # ...

File (TOML)

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

CLI

  1. --providers.nomad.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.

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. nomad:
  3. constraints: "Tag(`a.tag.name`)"
  4. # ...

File (TOML)

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

CLI

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

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

namespaces

Deprecated in favor of the namespaces option.

Optional, Default=””

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

Warning

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

File (YAML)

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

File (TOML)

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

CLI

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

namespaces

Optional, Default=””

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

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

Warning

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

File (YAML)

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

File (TOML)

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

CLI

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

allowEmptyServices

Optional, Default: false

If the parameter is set to true, it allows the creation of an empty servers load balancer if the targeted Nomad service has no endpoints available. This results in a 503 HTTP response instead of a 404.

File (YAML)

  1. providers:
  2. nomad:
  3. allowEmptyServices: true
  4. # ...

File (TOML)

  1. [providers.nomad]
  2. allowEmptyServices = true
  3. # ...

CLI

  1. --providers.nomad.allowEmptyServices=true