简体   繁体   English

带有docker后端的traefik反向代理 - 在traefik的配置文件中配置前端规则而不是通过容器标签

[英]traefik reverse-proxy with docker backend - configure frontend rules in traefik's config file and not via container label

For using traefik as a reverse-proxy in front of a Docker container whose dynamic IP address might change over time, traefik comes with a docker backend . 为了在一个Docker容器前面使用traefik作为反向代理,其动态IP地址可能会随着时间而改变,traefik附带一个docker 后端 All examples that I could find for setting this up follow the same pattern: 我设置的所有示例都遵循相同的模式:

First, start traefik in docker mode without an extra configuration file, activate host network mode (optional, so that traefik can see all Docker networks on the host if required) and mount the Docker unix socket so that traefik can listen to container starts and stops. 首先,启动traefik在docker模式,而无需额外的配置文件,启动主机网络模式(可选,让traefik可以看到,如果需要在主机上的所有泊坞窗网),并安装多克尔Unix套接字使traefik可以听容器启动和停止。

docker run --rm -p 80:80 --net=host --name traefik-reverse-proxy -v /dev/null/traefik.toml:/etc/traefik/traefik.toml -v /var/run/docker.sock:/var/run/docker.sock traefik --docker --loglevel debug

Then, start another container and set at least the following labels : 然后,启动另一个容器并至少设置以下标签

  • traefik.backend: "some-backend-name" traefik.backend:“some-backend-name”
  • traefik.frontend.rule: "Host: localhost; Method: GET" (or whatever your rules are) traefik.frontend.rule:“主持人:localhost;方法:GET”(或者你的规则是什么)
  • traefik.port: 80 (or whatever port your container exposes internally) traefik.port:80(或容器在内部暴露的任何端口)

Example: 例:

docker run --rm --name nginx -l traefik.backend="some-backend-name" -l traefik.frontend.rule="Host: localhost; Method: GET" -l traefik.port="80 nginx

Then, doing a curl localhost , one can see in the logs of the traefik container that it took the request and routed it to the NGINX container. 然后,执行curl localhost ,可以在traefik容器的日志中看到它接收了请求并将其路由到NGINX容器。

So far, so good... however, I do not like the fact that I have to configure my reverse-proxy forwarding rules (eg forward Host: some.host.name to container xxx) within the application itself (where my docker-compose files setting up the containers, labels etc. are usually located). 到目前为止,这么好......但是,我不喜欢我必须在应用程序本身(我的docker-中)配置我的反向代理转发规则(例如转发主机:some.host.name到容器xxx)这一事实。撰写文件设置容器,标签等通常位于)。 Rather, I would like to separate this from the application and configure it as part of traefik's configuration instead. 相反,我想将其与应用程序分开,并将其配置为traefik配置的一部分。

Is this possible somehow? 这有可能吗? What I tried is leaving out the traefik.frontend.rule label from the example nginx container and instead mount the following configuration file for traefik : 我尝试的是从示例nginx容器中traefik.frontend.rule标签,而是为traefik安装以下配置文件:

[frontends]
  [frontends.frontend1]
  backend = "some-backend-name"
    [frontends.frontend1.routes.test_1]
    rule = "Host: localhost; Method: GET"

The startup command for traefik thus becomes: 因此, traefik的启动命令变为:

docker run --rm -p 80:80 --net=host --name traefik-reverse-proxy -v $PWD/traefik.toml:/etc/traefik/traefik.toml -v /var/run/docker.sock:/var/run/docker.sock traefik --docker --loglevel debug

However, this does not seem to attach the frontend rule from the config file with the backend label from the nginx container. 但是,这似乎没有使用来自nginx容器的后端标签附加配置文件中的前端规则。 curl localhost now returns a 404 / Not found error. curl localhost现在返回404 / Not found错误。

the watch flag seems only works under the condition of rule.toml changed first time. watch flag似乎只能在rule.toml第一次更改的条件下工作。

In your case, i suggest you write a service to update your rule in etcd or zookeeper. 在您的情况下,我建议您编写一个服务来更新您在etcd或zookeeper中的规则。 the service read etcd changes and update traefik configure in etcd. 服务读取etcd更改并更新etcd中的traefik配置。

This is likely an order of operations issue. 这可能是一个操作顺序问题。 Enabling debug logging in config ( debug = true ) shows that traefik is parsing the config file frontend rules first, and only later generating frontends and backends based on what's running in docker. 在config( debug = true )中启用调试日志记录显示traefik首先解析配置文件前端规则,然后才根据docker中运行的内容生成前端和后端。

This means that the docker backends don't exist when the frontends from config are created, and it throws and error. 这意味着当创建配置的前端时,docker后端不存在,并且它会抛出并出错。

One solution is to put your rules config in a seperate file (eg rules.toml as shown in the docs ) and add the watch = true directive to your config. 一种解决方案是将规则配置放在一个单独的文件中(例如,docs中显示的 rules.toml ),并将watch = true指令添加到您的配置中。 This means that the frontend rules you define there will be updated after the backends from docker are generated. 这意味着您定义的前端规则将在生成docker的后端后更新。

We should probably submit a bug for this, because it's not exactly desirable functionality. 我们应该为此提交一个错误,因为它并不是完全合乎需要的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM