7

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?

David Genn
  • 709
  • 4
  • 11

3 Answers3

14

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:

ldez
  • 3,030
  • 19
  • 22
  • 1
    @Idez Your first sentence can be interpreted to imply that this was different in v1.3 of Traefik. This is incorrect as can be seen from [the documentation](//docs.traefik.io/v1.3/basics/#matchers). Given that, even if it is true that there have been a lot of changes since that version, that sentence is irrelevant to both the question an answer and should be removed. PS. Don't forget to use `@reply` when posting a comment, so the target user is notified. See [How do comment @replies work?](//meta.stackexchange.com/q/43019) for the full details. – robinCTS Feb 22 '18 at 05:33
  • 2
    This will match things that have *both* `Host:api.service.consul` and `PathPrefixStrip:/api`. The question is matching if *either* is true (so `http://api.service.consul/test/hello` and `http://some.other.host/api/test/hello` match). – Chris Feb 22 '18 at 16:26
  • @Chris I posted an answers that should match any of `/api` and `api.service.consul` (instead of requiring both at the same time), if you're interested. – KajMagnus Jan 01 '19 at 12:40
  • @Idez is it traefik.frontend.foo or traefik.frontends.foo ? the docs seem to indicate plural. – Greg Aug 26 '19 at 21:26
0

According to this, the right combination of Matcher Rules might be

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

See also official docs

https://docs.traefik.io/basics/

https://docs.traefik.io/configuration/backends/consulcatalog/

-1

The other answers answer a slightly different question [[edit: they did, until the idea in this answer got copied into another answer here, see comment below]], namely how to map api.server.consol/api to the backend — because they require both the hostname, and the URL path, to match at the same time. The question was, however, about how to make any of them map to the backend (both needn't match at the same time).

I think you can accomplish this by declaring two frontends, one for the Host rule, and one for the Path rule, which both use the same backend: (I haven't tested this)

[frontends.frontend_1.routes.rule_1]
  backend = "the_backend"
  rule = "PathPrefix:/api"

[frontends.frontend_2.routes.rule_1]
  backend = "the_backend"
  rule = "Host:api.service.consul"

That's with the File provider. Don't know how to do this with Consul — maybe you can add many tags? Like so?:

tags=[
   “traefik.frontends.frontend_1.rule=Host:api.service.consul",
   "traefik.frontends.frontend_2.rule=PathPrefixStrip:/api”]

Edit: The syntax above was previously slightly broken, and I've corrected it now. Documentation here: https://docs.traefik.io/v1.7/configuration/backends/consulcatalog/#multiple-frontends-for-a-single-service (I now see in ldez' recently edited answer).

KajMagnus
  • 11,308
  • 15
  • 79
  • 127
  • I downvoted because I consider the answer as a little off-topic and the proposed tag syntax is false. I updated my answer to centralize valid information. – ldez Jan 01 '19 at 23:09