Multiple frontend rules in Traefik Consul integration

Viewed 4412

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?

3 Answers

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).

Related