Traefik & File

好的旧配置文件

文件提供程序使您可以在TOML或YAML文件中定义动态配置 . 您可以编写以下相互排斥的配置元素之一:

Info

文件提供程序是整个文档中使用的默认格式,用于显示许多功能的配置示例.

Tip

文件提供者可以是您希望从其他提供者重用的常见元素的理想位置; 例如,声明白名单中间件,基本身份验证,...

Configuration Examples

声明路由器,中间件和服务

启用文件提供程序:

[providers.file]
  directory = "/path/to/dynamic/conf"
providers:
  file:
    directory: "/path/to/dynamic/conf"
--providers.file.directory=/path/to/dynamic/conf

声明路由器,中间件和服务:

[http]
  # Add the router
  [http.routers]
    [http.routers.router0]
      entryPoints = ["web"]
      middlewares = ["my-basic-auth"]
      service = "service-foo"
      rule = "Path(`/foo`)"

    # Add the middleware
    [http.middlewares]    
      [http.middlewares.my-basic-auth.basicAuth]
        users = ["test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/", 
                  "test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"]
        usersFile = "etc/traefik/.htpasswd"

    # Add the service
    [http.services]
      [http.services.service-foo]
        [http.services.service-foo.loadBalancer]
          [[http.services.service-foo.loadBalancer.servers]]
            url = "http://foo/"
          [[http.services.service-foo.loadBalancer.servers]]
            url = "http://bar/"
http:
  # Add the router
  routers:
    router0:
      entryPoints:
      - web
      middlewares:
      - my-basic-auth
      service: service-foo
      rule: Path(`/foo`)

  # Add the middleware
  middlewares:
    my-basic-auth:
      basicAuth:
        users:
        - test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/
        - test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0
        usersFile: etc/traefik/.htpasswd

  # Add the service
  services:
    service-foo:
      loadBalancer:
        servers:
        - url: http://foo/
        - url: http://bar/
        passHostHeader: false

Provider Configuration

如果您很着急,也许您宁愿浏览一下动态配置参考和静态配置 .

Limitations

通过文件提供程序,Traefik可以监听文件系统通知以更新动态配置.

如果您在协调器中使用已安装/绑定的文件系统(例如docker或kubernetes),则文件的链接方式可能是错误的来源. 如果文件系统之间的链接断开,则当更改/重命名源文件/目录时,不会向链接的文件/目录报告任何内容,因此不会触发或捕获文件系统通知.

例如,在Docker中,如果主机文件被重命名,则指向已挂载文件的链接将断开,并且容器的文件将不会更新. 为避免此类问题,一个好的做法是:

  • 用父目录设置Traefik 目录配置
  • 挂载/绑定父目录

由于很难收听所有文件系统通知,因此Traefik使用fsnotify . 如果使用带有已挂载目录的目录不能解决问题,请使用fsnotify检查文件系统兼容性.

filename

定义配置文件的路径.

[providers]
  [providers.file]
    filename = "dynamic_conf.toml"
providers:
  file:
    filename: dynamic_conf.yml
--providers.file.filename=dynamic_conf.toml

directory

定义包含配置文件的目录.

[providers]
  [providers.file]
    directory = "/path/to/config"
providers:
  file:
    directory: /path/to/config
--providers.file.directory=/path/to/config

watch

watch选项设置为true以允许Traefik自动监视文件更改.
它与filenamedirectory选项一起使用.

[providers]
  [providers.file]
    directory = "/path/to/dynamic/conf"
    watch = true
providers:
  file:
    directory: /path/to/dynamic/conf
    watch: true
--providers.file.directory=/my/path/to/dynamic/conf
--providers.file.watch=true

Go Templating

Warning

Go模板仅与专用动态配置文件一起使用. 模板在Traefik主静态配置文件中不起作用.

Traefik允许使用Go模板.
因此,可以如文件template-rules.toml中所述轻松定义许多路由器,服务和TLS证书:

使用模板配置
# template-rules.toml
[http]

  [http.routers]
  {{ range $i, $e := until 100 }}
    [http.routers.router{{ $e }}]
    # ...
  {{ end }}  


  [http.services]
  {{ range $i, $e := until 100 }}
      [http.services.service{{ $e }}]
      # ...
  {{ end }}  

[tcp]

  [tcp.routers]
  {{ range $i, $e := until 100 }}
    [tcp.routers.router{{ $e }}]
    # ...
  {{ end }}  


  [tcp.services]
  {{ range $i, $e := until 100 }}
      [http.services.service{{ $e }}]
      # ...
  {{ end }}  

{{ range $i, $e := until 10 }}
[[tls.certificates]]
  certFile = "/etc/traefik/cert-{{ $e }}.pem"
  keyFile = "/etc/traefik/cert-{{ $e }}.key"
  store = ["my-store-foo-{{ $e }}", "my-store-bar-{{ $e }}"]
{{ end }}

[tls.config]
{{ range $i, $e := until 10 }}
  [tls.config.TLS{{ $e }}]
  # ...
{{ end }}
http:

{{range $i, $e := until 100 }}
  routers:
    router{{ $e }:
      # ...
{{end}}

{{range $i, $e := until 100 }}
  services:
    application{{ $e }}:
      # ...
{{end}}

tcp:

{{range $i, $e := until 100 }}
  routers:
    router{{ $e }:
      # ...
{{end}}

{{range $i, $e := until 100 }}
  services:
    service{{ $e }}:
      # ...
{{end}}

{{ range $i, $e := until 10 }}
tls:
  certificates:
  - certFile: "/etc/traefik/cert-{{ $e }}.pem"
    keyFile: "/etc/traefik/cert-{{ $e }}.key"
    store:
    - "my-store-foo-{{ $e }}"
    - "my-store-bar-{{ $e }}"
{{end}}