• Traefik & Rancher
    • Configuration Examples
    • Provider Configuration Options
      • exposedByDefault
      • defaultRule
      • enableServiceHealthFilter
      • refreshSeconds
      • intervalPoll
      • prefix
      • constraints
    • Routing Configuration Options
      • General
      • Routers
      • Services
      • Middleware
      • Specific Options
        • traefik.enable
        • Port Lookup

    Traefik & Rancher

    A Story of Labels, Services & Containers

    Rancher

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

    Important

    This provider is specific to Rancher 1.x.Rancher 2.x requires Kubernetes and does not have a metadata endpoint of its own for Traefik to query.As such, Rancher 2.x users should utilize the Kubernetes provider directly.

    Configuration Examples

    Configuring Rancher & Deploying / Exposing ServicesEnabling the rancher provider

    1. [providers.rancher]
    1. providers:
    2. rancher: {}
    1. --providers.rancher=true

    Attaching labels to services

    1. labels:
    2. - traefik.http.services.my-service.rule=Host(`my-domain`)

    Provider Configuration Options

    Browse the ReferenceIf you're in a hurry, maybe you'd rather go through the configuration reference:

    1. # Enable Rancher Provider.
    2. [providers.rancher]
    3. # Expose Rancher services by default in Traefik.
    4. exposedByDefault = true
    5. # Enable watch Rancher changes.
    6. watch = true
    7. # Filter services with unhealthy states and inactive states.
    8. enableServiceHealthFilter = true
    9. # Defines the polling interval (in seconds).
    10. refreshSeconds = 15
    11. # Poll the Rancher metadata service for changes every `rancher.refreshSeconds`, which is less accurate
    12. intervalPoll = false
    13. # Prefix used for accessing the Rancher metadata service
    14. prefix = "/latest"
    1. # Enable Rancher Provider.
    2. providers:
    3. rancher:
    4. # Expose Rancher services by default in Traefik.
    5. exposedByDefault: true
    6. # Enable watch Rancher changes.
    7. watch: true
    8. # Filter services with unhealthy states and inactive states.
    9. enableServiceHealthFilter: true
    10. # Defines the polling interval (in seconds).
    11. refreshSeconds: 15
    12. # Poll the Rancher metadata service for changes every `rancher.refreshSeconds`, which is less accurate
    13. intervalPoll: false
    14. # Prefix used for accessing the Rancher metadata service
    15. prefix: "/latest"
    1. # Enable Rancher Provider.
    2. --providers.rancher=true
    3. # Expose Rancher services by default in Traefik.
    4. --providers.rancher.exposedByDefault=true
    5. # Enable watch Rancher changes.
    6. --providers.rancher.watch=true
    7. # Filter services with unhealthy states and inactive states.
    8. --providers.rancher.enableServiceHealthFilter=true
    9. # Defines the polling interval (in seconds).
    10. --providers.rancher.refreshSeconds=15
    11. # Poll the Rancher metadata service for changes every `rancher.refreshSeconds`, which is less accurate
    12. --providers.rancher.intervalPoll=false
    13. # Prefix used for accessing the Rancher metadata service
    14. --providers.rancher.prefix="/latest"

    List of all available labels for the dynamic configuration references.

    exposedByDefault

    Optional, Default=true

    1. [providers.rancher]
    2. exposedByDefault = false
    3. # ...
    1. providers:
    2. rancher:
    3. exposedByDefault: false
    4. # ...
    1. --providers.rancher.exposedByDefault=false
    2. # ...

    Expose Rancher 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 }})

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

    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

    1. [providers.rancher]
    2. enableServiceHealthFilter = false
    3. # ...
    1. providers:
    2. rancher:
    3. enableServiceHealthFilter: false
    4. # ...
    1. --providers.rancher.enableServiceHealthFilter=false
    2. # ...

    Filter services with unhealthy states and inactive states.

    refreshSeconds

    Optional, Default=15

    1. [providers.rancher]
    2. refreshSeconds = 30
    3. # ...
    1. providers:
    2. rancher:
    3. refreshSeconds: 30
    4. # ...
    1. --providers.rancher.refreshSeconds=30
    2. # ...

    Defines the polling interval (in seconds).

    intervalPoll

    Optional, Default=false

    1. [providers.rancher]
    2. intervalPoll = true
    3. # ...
    1. providers:
    2. rancher:
    3. intervalPoll: true
    4. # ...
    1. --providers.rancher.intervalPoll=true
    2. # ...

    Poll the Rancher metadata service for changes every rancher.refreshSeconds,which is less accurate than the default long polling technique which will provide near instantaneous updates to Traefik.

    prefix

    Optional, Default=/latest

    1. [providers.rancher]
    2. prefix = "/test"
    3. # ...
    1. providers:
    2. rancher:
    3. prefix: "/test"
    4. # ...
    1. --providers.rancher.prefix="/test"
    2. # ...

    Prefix used for accessing the Rancher metadata service

    constraints

    Optional, Default=""

    1. [providers.rancher]
    2. constraints = "Label(`a.label.name`, `foo`)"
    3. # ...
    1. providers:
    2. rancher:
    3. constraints: "Label(`a.label.name`, `foo`)"
    4. # ...
    1. --providers.rancher.constraints="Label(`a.label.name`, `foo`)"
    2. # ...

    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.Constraints Expression Examples

    1. # Includes only containers having a label with key `a.label.name` and value `foo`
    2. constraints = "Label(`a.label.name`, `foo`)"
    1. # Excludes containers having any label with key `a.label.name` and value `foo`
    2. constraints = "!Label(`a.label.name`, `value`)"
    1. # With logical AND.
    2. constraints = "Label(`a.label.name`, `valueA`) && Label(`another.label.name`, `valueB`)"
    1. # With logical OR.
    2. constraints = "Label(`a.label.name`, `valueA`) || Label(`another.label.name`, `valueB`)"
    1. # With logical AND and OR, with precedence set by parentheses.
    2. constraints = "Label(`a.label.name`, `valueA`) && (Label(`another.label.name`, `valueB`) || Label(`yet.another.label.name`, `valueC`))"
    1. # Includes only containers having a label with key `a.label.name` and a value matching the `a.+` regular expression.
    2. constraints = "LabelRegex(`a.label.name`, `a.+`)"

    See also Restrict the Scope of Service Discovery.

    Routing Configuration Options

    General

    Traefik creates, for each rancher service, a corresponding service and router.

    The Service automatically gets a server per container in this rancher service, and the router gets a default rule attached to it, based on the service name.

    Routers

    To update the configuration of the Router automatically attached to the container, add labels starting with traefik.routers.{name-of-your-choice}. and followed by the option you want to change.For example, to change the rule, you could add the label traefik.http.routers.my-container.rule=Host(my-domain).

    Every Router parameter can be updated this way.

    Services

    To update the configuration of the Service automatically attached to the container, add labels starting with traefik.http.services.{name-of-your-choice}.,followed by the option you want to change. For example, to change the passhostheader behavior,you'd add the label traefik.http.services.{name-of-your-choice}.loadbalancer.passhostheader=false.

    Every Service parameter can be updated this way.

    Middleware

    You can declare pieces of middleware using labels starting with traefik.http.middlewares.{name-of-your-choice}., followed by the middleware type/options.For example, to declare a middleware redirectscheme named my-redirect, you'd write traefik.http.middlewares.my-redirect.redirectscheme.scheme: https.Declaring and Referencing a Middleware

    1. # ...
    2. labels:
    3. - traefik.http.middlewares.my-redirect.redirectscheme.scheme=https
    4. - traefik.http.routers.my-container.middlewares=my-redirect

    Conflicts in Declaration

    If you declare multiple middleware with the same name but with different parameters, the middleware fails to be declared.

    More information about available middlewares in the dedicated middlewares section.

    Specific Options

    traefik.enable

    You can tell Traefik to consider (or not) the container by setting traefik.enable to true or false.

    This option overrides the value of exposedByDefault.

    Port Lookup

    Traefik is now capable of detecting the port to use, by following the default rancher flow.That means, if you just expose lets say port :1337 on the rancher ui, traefik will pick up this port and use it.