Multiple routers and multiple services for swarm service

Hello!
I am trying to implement easy exporting of metrics (Prometheus and cAdvisor) of all of my swarm nodes.
So I've created a new stack with services (global) nodeexporter and cadvisor and now I want to be able to auto-create routers and services automatically with some ruleset.
I've tried labeling my services with something like examples from docker swarm docs:

  • "traefik.http.routers.nodeexporter.rule=Host(nodeexporter-{{.Node.Hostname}})"
    - "traefik.http.services.nodeexporter_service-{{.Node.Hostname}}.loadBalancer.server.port=9100"

But it doesn't seem to work...
So is there any way to create multiple frontend and multiple backend rules with some rule or the only way to achieve that is to bring up something else like file or etcd and create all rules manually?
Thanks!

Hi @vbessonov, could you elaborate the use case or provide a more detailed reproduction case please? Not sure to understand what you want to achieve: which request to route where, following which kind of rule? (a domain name load balancing to the services replicas? something else)?

Can you also confirm you are using Traefik v2.0? As you're mentioning "frontend" and "backend" which does not exist in v2.0 (see https://docs.traefik.io/v2.0/routing/overview/), I want to double check :slight_smile:

Of course! Just used v1 terminology for clarity (as I thought).I am using traefik v.2 with Docker Swarm.
Now I have several isolated environments (let's say vdc's) and I want to export all metrics from all swarm nodes to the outside world. So, of course I can expose them all on separate ports on all machines and then collect them all somehow, but I think Trarfik seems to be really great tool for cases like mine.

I want to create routers dynamically-let's say I am adding a new machine to my swarm, now Swarm manager automatically deploys new exporter pods on it (the service is global). Now I want to route metrics outside somehow. For every node I want a new Traefik router rule. Something like cadvisor.mynode-1.mycompany.com, cadvisor.mynode-2.mycompany.com and so on. Of course this router should route (he-he, obviously) to corresponding container on corresponding node (mynode-1, mynode-2).

Right now Traefik seems to group all instances of corresponding swarm service into one traefik service, and creates a single frontend rule for them all, thus making loadbalancing possible. I want to "separate" it all back...

Hope I was able to make myself clear =)
Thanks!

I think I understand better, thanks.

Please note that "frontend" is a "router", and "backend" is a "service" with v2.0 (for clarity).

Traefik uses the "Services" to map to a Docker Swarm service in your case: you understood correctly how it works. But you cannot use the "automatic" system as the mapping is 1 Traefik (router + service) <-> 1 Swarm Service.

So you'll have to use another solution (with file provider, or the Traefik REST API).

That's sad =) Anyway, thank you!

1 Like