文件¶
老的配置文件方式
文件 provider 可以让我们通过一个 TOML 或者 YAML 文件来定义 动态配置,我们可以编写如下的一些配置元素:
Info
文件 provider 是整个文档中用于演示大部分功能的配置示例的默认格式。
Tip
文件 provider 或许是你希望从其他 provider 中重用常见元素的一个理想位置;例如,声明白名单中间件,basic auth 认证...
配置示例¶
声明路由器,中间件和服务
在文件 provider 中启用:
[providers.file]
filename = "/my/path/to/dynamic-conf.toml"
providers:
file:
filename: "/my/path/to/dynamic-conf.yml"
--providers.file.filename=/my/path/to/dynamic_conf.toml
声明路由,中间件和服务:
[http]
# 添加路由
[http.routers]
[http.routers.router0]
entryPoints = ["web"]
middlewares = ["my-basic-auth"]
service = "service-foo"
rule = "Path(`/foo`)"
# 添加中间件
[http.middlewares]
[http.middlewares.my-basic-auth.basicAuth]
users = ["test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/",
"test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"]
usersFile = "etc/traefik/.htpasswd"
# 添加服务
[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:
# 添加路由
routers:
router0:
entryPoints:
- web
middlewares:
- my-basic-auth
service: service-foo
rule: Path(`/foo`)
# 添加中间件
middlewares:
my-basic-auth:
basicAuth:
users:
- test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/
- test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0
usersFile: etc/traefik/.htpasswd
# 添加服务
services:
service-foo:
loadBalancer:
servers:
- url: http://foo/
- url: http://bar/
passHostHeader: false
Provider 配置¶
如果你心情比较急切的话,建议你可以先去浏览下 动态配置文档 和 静态配置的文档。
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 自动 watch 配置文件的变化。 它与 filename
和 directory
选项一起使用。
[providers]
[providers.file]
filename = "dynamic_conf.toml"
watch = true
providers:
file:
filename: dynamic_conf.yml
watch: true
--providers.file.filename=dynamic_conf.toml
--providers.file.watch=true
Go 模板¶
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}}