Traefik 2.0:如何配置`traefik.frontend.rule=Host:example.com`?
Traefik 2.0: How to configure `traefik.frontend.rule=Host:example.com`?
我只是尝试将我现有的非常简单的 Traefik 设置升级到 2.0。
我正在使用 Docker 作为提供商。
我是 运行 具有 docker-compose 的容器。这是我的 docker-compose 配置:
version: "3.5"
services:
traefik:
image: traefik:v2.0-alpine
read_only: yes
ports:
- 80:80
- 443:443
volumes:
- type: bind
source: /var/run/docker.sock
target: /var/run/docker.sock
- type: bind
source: ./traefik.toml
target: /traefik.toml
- type: bind
source: ./acme.json
target: /acme.json
networks:
- traefik
example:
image: example.com/example/example
labels:
- "traefik.enable=true"
- "traefik.frontend.rule=Host:example.com"
- "traefik.docker.network=traefik"
networks:
- traefik
这是我的 traefik.toml
:
defaultEntryPoints = ["http", "https"]
[Global]
sendAnonymousUsage = true
[entryPoints]
[entryPoints.http]
address = ":80"
[entryPoints.http.redirect]
entryPoint = "https"
[entryPoints.https]
address = ":443"
[entryPoints.https.tls]
[acme]
email = "ssl@example.com"
storage = "acme.json"
entryPoint = "https"
onHostRule = true
onDemand = false
acmeLogging = true
[acme.tlsChallenge]
[docker]
exposedByDefault = false
此设置适用于 Traefik 1.7。
使用 Traefik 2.0,我观察到两个问题,查看 ACME 日志:
Unable to obtain ACME certificate for domains traefik-example-deployment
Unable to obtain ACME certificate for domains example-example-deployment
- Traefik 显然暴露了两个容器。它应该只公开示例容器。
- Traefik 正在忽略
traefik.frontend.rule=Host:example.com
标签。
我怀疑这可能是由于 Traefik 2.0 中的配置格式更改所致。但是,虽然我查看了 2.0 文档,但我无法正确配置。
路由器¶
要更新自动附加到容器的路由器的配置,请添加以 traefik.routers.{name-of-your-choice}.
开头的标签,然后是您要更改的选项。例如,要更改规则,您可以添加标签 traefik.http.routers.my-container.rule=Host(my-domain).
每个路由器参数都可以通过这种方式更新。
如文档中所述 - https://docs.traefik.io/v2.0/providers/docker/
将 labels:
更改为
services:
my-container:
# ...
labels:
- traefik.http.routers.my-container.rule=Host(`my-domain`)
告诉 Traefik 不要暴露 Container - 添加 - traefik.enable=false
As traefik
默认情况下通过 Traefik 公开容器。如果设置为 false,没有 traefik.enable=true
标签的容器将从生成的路由配置中忽略。
exposedByDefault (Optional, Default=true)¶
我只是尝试将我现有的非常简单的 Traefik 设置升级到 2.0。
我正在使用 Docker 作为提供商。
我是 运行 具有 docker-compose 的容器。这是我的 docker-compose 配置:
version: "3.5"
services:
traefik:
image: traefik:v2.0-alpine
read_only: yes
ports:
- 80:80
- 443:443
volumes:
- type: bind
source: /var/run/docker.sock
target: /var/run/docker.sock
- type: bind
source: ./traefik.toml
target: /traefik.toml
- type: bind
source: ./acme.json
target: /acme.json
networks:
- traefik
example:
image: example.com/example/example
labels:
- "traefik.enable=true"
- "traefik.frontend.rule=Host:example.com"
- "traefik.docker.network=traefik"
networks:
- traefik
这是我的 traefik.toml
:
defaultEntryPoints = ["http", "https"]
[Global]
sendAnonymousUsage = true
[entryPoints]
[entryPoints.http]
address = ":80"
[entryPoints.http.redirect]
entryPoint = "https"
[entryPoints.https]
address = ":443"
[entryPoints.https.tls]
[acme]
email = "ssl@example.com"
storage = "acme.json"
entryPoint = "https"
onHostRule = true
onDemand = false
acmeLogging = true
[acme.tlsChallenge]
[docker]
exposedByDefault = false
此设置适用于 Traefik 1.7。
使用 Traefik 2.0,我观察到两个问题,查看 ACME 日志:
Unable to obtain ACME certificate for domains traefik-example-deployment
Unable to obtain ACME certificate for domains example-example-deployment
- Traefik 显然暴露了两个容器。它应该只公开示例容器。
- Traefik 正在忽略
traefik.frontend.rule=Host:example.com
标签。
我怀疑这可能是由于 Traefik 2.0 中的配置格式更改所致。但是,虽然我查看了 2.0 文档,但我无法正确配置。
路由器¶
要更新自动附加到容器的路由器的配置,请添加以 traefik.routers.{name-of-your-choice}.
开头的标签,然后是您要更改的选项。例如,要更改规则,您可以添加标签 traefik.http.routers.my-container.rule=Host(my-domain).
每个路由器参数都可以通过这种方式更新。 如文档中所述 - https://docs.traefik.io/v2.0/providers/docker/
将 labels:
更改为
services:
my-container:
# ...
labels:
- traefik.http.routers.my-container.rule=Host(`my-domain`)
告诉 Traefik 不要暴露 Container - 添加 - traefik.enable=false
As traefik
默认情况下通过 Traefik 公开容器。如果设置为 false,没有 traefik.enable=true
标签的容器将从生成的路由配置中忽略。
exposedByDefault (Optional, Default=true)¶