简体   繁体   English

Traefik:按子域动态路由

[英]Traefik: Dynamic routing by subdomain

Problem,问题,

I have multiple services that are deployed on Docker containers.我有多个服务部署在 Docker 容器上。 Each set of our clients should be routed to a specific service.我们的每一组客户都应该被路由到一个特定的服务。 Example:例子:

  • Client1 -> service1客户端1->服务1
  • Client2 -> service1客户端 2 -> 服务 1
  • client3 -> service2客户 3 -> 服务 2

This should done using subdomain: client1= client1.example.com client2 = client2.example.com这应该使用子域完成:client1= client1.example.com client2 = client2.example.com

Question:问题:

I would like this config to be dynamic meaning that Traefik needs to get these routing rules dynamically from another service:我希望这个配置是动态的,这意味着 Traefik 需要从另一个服务动态获取这些路由规则:

  • I can for example create a service that giving a hostname return the name of service that Traefik should route to.例如,我可以创建一个服务,给主机名返回 Traefik 应该路由到的服务的名称。
  • Or maybe when a client is added I can push this rule to Traefik.或者,当添加客户端时,我可以将此规则推送到 Traefik。

Details:细节:

This feature is useful when I create a saas application.当我创建一个 saas 应用程序时,此功能很有用。

Thank you very much.非常感谢。

One easy way to achieve this is to use regex for the defined routers for each of the services and update the regex once you onboarded a new client实现此目的的一种简单方法是将正则表达式用于为每个服务定义的路由器,并在您加入新客户端后更新正则表达式

For instance, add this label to the service 1例如,将此 label 添加到服务 1

 - traefik.http.routers.app-backend.rule=HostRegexp(`{subdomain:(clinet1)}.example.com`)

and when you add another client that should be served by service 1, just update the label to be当您添加另一个应由服务 1 提供服务的客户端时,只需将 label 更新为

 - traefik.http.routers.app-backend.rule=HostRegexp(`{subdomain:(clinet1|clientX)}.example.com`)

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

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