Traefik & Rancher

标签,服务和容器的故事

Rancher

在您的服务上贴上标签,让Traefik完成其余工作!

此提供程序特定于Rancher1.x.

Rancher 2.x需要Kubernetes,并且没有自己的元数据端点供Traefik查询. 因此,Rancher 2.x用户应直接利用Kubernetes提供程序 .

Configuration Examples

配置Rancher和部署/公开服务

启用牧场主提供商

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

在服务上贴标签

labels:
  - traefik.http.services.my-service.rule=Host(`mydomain.com`)

Routing Configuration

请参阅路由中的专用部分.

Provider Configuration

浏览参考

如果您很着急,也许您应该阅读配置参考:

# Enable Rancher Provider.
[providers.rancher]

  # Expose Rancher services by default in Traefik.
  exposedByDefault = true

  # Enable watch Rancher changes.
  watch = true

  # Filter services with unhealthy states and inactive states.
  enableServiceHealthFilter = true

  # Defines the polling interval (in seconds).
  refreshSeconds = 15

  # Poll the Rancher metadata service for changes every `rancher.refreshSeconds`, which is less accurate
  intervalPoll = false

  # Prefix used for accessing the Rancher metadata service
  prefix = "/latest"
# Enable Rancher Provider.
providers:
  rancher:

  # Expose Rancher services by default in Traefik.
  exposedByDefault: true

  # Enable watch Rancher changes.
  watch: true

  # Filter services with unhealthy states and inactive states.
  enableServiceHealthFilter: true

  # Defines the polling interval (in seconds).
  refreshSeconds: 15

  # Poll the Rancher metadata service for changes every `rancher.refreshSeconds`, which is less accurate
  intervalPoll: false

  # Prefix used for accessing the Rancher metadata service
  prefix: /latest
# Enable Rancher Provider.
--providers.rancher=true

# Expose Rancher services by default in Traefik.
--providers.rancher.exposedByDefault=true

# Enable watch Rancher changes.
--providers.rancher.watch=true

# Filter services with unhealthy states and inactive states.
--providers.rancher.enableServiceHealthFilter=true

# Defines the polling interval (in seconds).
--providers.rancher.refreshSeconds=15

# Poll the Rancher metadata service for changes every `rancher.refreshSeconds`, which is less accurate
--providers.rancher.intervalPoll=false

# Prefix used for accessing the Rancher metadata service
--providers.rancher.prefix=/latest

exposedByDefault

可选,默认= true

[providers.rancher]
  exposedByDefault = false
  # ...
providers:
  rancher:
    exposedByDefault: false
    # ...
--providers.rancher.exposedByDefault=false
# ...

默认情况下,在Traefik中公开Rancher服务. 如果设置为false,则没有traefik.enable=true标签的服务将从生成的路由配置中忽略.

另请参阅限制服务发现范围 .

defaultRule

可选,默认为Host(`{{ normalize .Name }}`)

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

所有服务的默认主机规则.

对于给定的容器,如果标签未定义任何路由规则,则由此defaultRule定义. 它必须是有效的Go模板 ,并带有sprig模板功能 . 可以将服务名称作为Name标识符进行访问,并且模板可以访问此容器上定义的所有标签.

可以基于容器使用traefik.http.routers.Router1.rule标签覆盖此选项.

enableServiceHealthFilter

可选,默认= true

[providers.rancher]
  enableServiceHealthFilter = false
  # ...
providers:
  rancher:
    enableServiceHealthFilter: false
    # ...
--providers.rancher.enableServiceHealthFilter=false
# ...

筛选状态为不健康和不活动的服务.

refreshSeconds

可选,默认= 15

[providers.rancher]
  refreshSeconds = 30
  # ...
providers:
  rancher:
    refreshSeconds: 30
    # ...
--providers.rancher.refreshSeconds=30
# ...

定义轮询间隔(以秒为单位).

intervalPoll

可选,默认= false

[providers.rancher]
  intervalPoll = true
  # ...
providers:
  rancher:
    intervalPoll: true
    # ...
--providers.rancher.intervalPoll=true
# ...

轮询Rancher元数据服务以更改每个rancher.refreshSeconds ,其准确性不如默认的长轮询技术,后者会提供对Traefik的近乎即时的更新.

prefix

可选,默认= /最新

[providers.rancher]
  prefix = "/test"
  # ...
providers:
  rancher:
    prefix: "/test"
    # ...
--providers.rancher.prefix=/test
# ...

用于访问Rancher元数据服务的前缀

constraints

可选,默认值=""

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

约束是Traefik与容器标签匹配的表达式,以确定是否为该容器创建任何路线. 也就是说,如果容器的标签都不匹配表达式,则不会创建容器的路由. 如果表达式为空,则包括所有检测到的容器.

表达式语法基于Label("key", "value")LabelRegex("key", "value")函数以及常用的布尔逻辑,如以下示例所示.

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

另请参阅限制服务发现范围 .