Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple frontend rules in Traefik Consul integration

Tags:

traefik

consul

I'm using Traefik to load balance across different services registered in Consul.

I'm using the consul-catalog configuration and overriding a front end routing rule for one of the services by adding a tag when defining the service in consul:

tags=[“traefik.frontend.rule=PathPrefixStrip:/api,Host:api.service.consul”]

I'm expecting both /api and api.service.consul to resolve to my service, however only /api is successful, however api.service.consul returns a 404 error.

In other words, only the first rule is being considered. If I switch the tag around:

tags=[“traefik.frontend.rule=Host:api.service.consul,PathPrefixStrip:/api”]

Then api.servie.consul resolves and /api returns a 404 error.

I believe the docs suggest this configuration is supported. Has anyone else had success with defining multiple rules via consul tags?

like image 539
David Genn Avatar asked Jul 07 '17 06:07

David Genn


1 Answers

Edit: Since v1.7, for consul-catalog, you can use: multiple-frontends-for-a-single-service

tags=[
"traefik.frontends.foo.rule=Host:api.service.consul",
"traefik.frontends.bar.rule=PathPrefixStrip:/api",
]

The answer can be seen at https://github.com/containous/traefik/issues/2417:

  • , is the OR operator (works only inside a matcher, ex: Host:foo.com,bar.com)
  • ; is the AND operator (works only between matchers, ex: Host:foo.com;Path:/bar)

So, for your example, use:

tags=["traefik.frontend.rule=Host:api.service.consul;PathPrefixStrip:/api"]

Documentation links:

  • https://docs.traefik.io/v1.5/basics/#matchers
  • https://docs.traefik.io/v1.5/basics/#combining-multiple-rules
  • https://docs.traefik.io/v1.5/configuration/backends/consulcatalog/
like image 191
ldez Avatar answered Dec 26 '22 12:12

ldez