Skip to content

Conversation

@ignaciovaquero
Copy link

@ignaciovaquero ignaciovaquero commented Jul 21, 2016

Hi,

We encountered a little issue when working with traefik, in a special use case we had, when using the Docker backend provider. The scenario is better explained by the means of an example, so below is a docker-compose.yml we created to test this scenario:

version: '2'
services:
  traefik:
    image: traefik:v1.0.0
    command: --docker --docker.domain=docker.localhost --logLevel=DEBUG
    ports:
      - "80:80"
    volumes:
      - /dev/null:/traefik.toml
      - /var/run/docker.sock:/var/run/docker.sock
  whoami1:
    image: emilevauge/whoami
    labels:
      - "traefik.backend=whoami"
      - "traefik.frontend.rule=Host:whoami.docker.localhost"
  whoami2:
    image: emilevauge/whoami
    labels:
      - "traefik.backend=whoami"
      - "traefik.frontend.rule=Host:whoami.docker.localhost"

As you can see we have two whoami containers which are part of the same backend. We wanted these containers to have a frontend rule that matches the host whoami.docker.localhost, but we also wanted these containers to have another frontend rule that matches the host proxy.docker.localhost and all paths prefixed by /whoami (which must be stripped from the URL). So, using the file backend, this were resulting in a traefik.toml file like the one below:

# traefik.toml
logLevel = "DEBUG"
defaultEntryPoints = ["http"]
[entryPoints]
  [entryPoints.http]
  address = ":80"

[file]

# rules
[backends]
  [backends.backend1]
    [backends.backend1.servers.server1]
    url = "http://172.18.0.2:80"
    [backends.backend1.servers.server2]
    url = "http://172.18.0.3:80"

[frontends]
  [frontends.frontend1]
  backend = "backend1"
    [frontends.frontend1.routes.test_1]
    rule = "Host:whoami.docker.localhost"
  [frontends.frontend2]
  backend = "backend1"
    [frontends.frontend2.routes.test_1]
    rule = "Host:proxy.docker.localhost;PathPrefixStrip:/whoami"

This worked fine, so we could have the following answers:

$ curl -H "Host:whoami.docker.localhost" localhost
Hostname: 43945723dd85
IP: 127.0.0.1
IP: ::1
IP: 172.18.0.2
IP: fe80::42:acff:fe12:2
GET / HTTP/1.1
Host: whoami.docker.localhost
User-Agent: curl/7.35.0
Accept: */*
Accept-Encoding: gzip
X-Forwarded-For: 172.18.0.1
X-Forwarded-Host: whoami.docker.localhost
X-Forwarded-Proto: http
X-Forwarded-Server: 2c5a48d8ccdb


$ curl -H "Host:proxy.docker.localhost" localhost/whoami/version
Hostname: 0e6df3415a89
IP: 127.0.0.1
IP: ::1
IP: 172.18.0.3
IP: fe80::42:acff:fe12:3
GET /version HTTP/1.1
Host: proxy.docker.localhost
User-Agent: curl/7.35.0
Accept: */*
Accept-Encoding: gzip
X-Forwarded-For: 172.18.0.1
X-Forwarded-Host: proxy.docker.localhost
X-Forwarded-Proto: http
X-Forwarded-Server: 2c5a48d8ccdb

(Notice the path stripped in the second request).

So we deeped into the documentation and traefik's code in order to see if we could translate this configuration inside the Docker backend provider, but all of our tries didn't work because in any case we got two separated frontends created. We tried configurations like the one below:

version: '2'
services:
  traefik:
    image: traefik:v1.0.0
    command: --docker --docker.domain=docker.localhost --logLevel=DEBUG
    ports:
      - "80:80"
    volumes:
      - /dev/null:/traefik.toml
      - /var/run/docker.sock:/var/run/docker.sock
  whoami1:
    image: emilevauge/whoami
    labels:
      - "traefik.backend=whoami"
      - "traefik.frontend.rule=Host:whoami.docker.localhost,proxy.docker.localhost;PathPrefixStrip:/resources,/"
  whoami2:
    image: emilevauge/whoami
    labels:
      - "traefik.backend=whoami"
      - "traefik.frontend.rule=Host:whoami.docker.localhost,proxy.docker.localhost;PathPrefixStrip:/resources,/"

None worked. Finally, we decided to modify traefik's docker provider source code and docker.tmpl file to meet our needs. This PR allows us to create separated frontends which matches the same backend, with different rules. This can be tested using the following docker-compose.yml file:

version: '2'
services:
  traefik:
    image: devopsbq/traefik:v1.0.0
    command: --docker --docker.domain=docker.localhost --logLevel=DEBUG
    ports:
      - "80:80"
    volumes:
      - /dev/null:/traefik.toml
      - /var/run/docker.sock:/var/run/docker.sock
  whoami1:
    image: emilevauge/whoami
    labels:
      - "traefik.backend=whoami"
      - "traefik.frontend.rule=Host:whoami.docker.localhost&&Host:proxy.docker.localhost;PathPrefixStrip:/resources"
  whoami2:
    image: emilevauge/whoami
    labels:
      - "traefik.backend=whoami"
      - "traefik.frontend.rule=Host:whoami.docker.localhost&&Host:proxy.docker.localhost;PathPrefixStrip:/resources"

We used the && separator in order to specify more than one frontend, because it worked for us, but we could change it to something else. Everything now worked fine:

$ curl -H "Host:whoami.docker.localhost" localhost
Hostname: 43945723dd85
IP: 127.0.0.1
IP: ::1
IP: 172.18.0.2
IP: fe80::42:acff:fe12:2
GET / HTTP/1.1
Host: whoami.docker.localhost
User-Agent: curl/7.35.0
Accept: */*
Accept-Encoding: gzip
X-Forwarded-For: 172.18.0.1
X-Forwarded-Host: whoami.docker.localhost
X-Forwarded-Proto: http
X-Forwarded-Server: 2c5a48d8ccdb


$ curl -H "Host:proxy.docker.localhost" localhost/whoami/version
Hostname: 0e6df3415a89
IP: 127.0.0.1
IP: ::1
IP: 172.18.0.3
IP: fe80::42:acff:fe12:3
GET /version HTTP/1.1
Host: proxy.docker.localhost
User-Agent: curl/7.35.0
Accept: */*
Accept-Encoding: gzip
X-Forwarded-For: 172.18.0.1
X-Forwarded-Host: proxy.docker.localhost
X-Forwarded-Proto: http
X-Forwarded-Server: 2c5a48d8ccdb

@emilevauge
Copy link
Member

Hi @igvaquero18, thanks for your contribution :)
There is already a proposal to refactor the labels that will solve this issue: #444 (comment).
It would be more generic to adopt this format instead of adding a && separator and then I prefer to implement this one instead.
Sorry for that :'(

@ignaciovaquero
Copy link
Author

Oh, great! We didn't see that.

Thank you!

@emilevauge
Copy link
Member

@igvaquero18, thanks again for your contribution. I hope you will understand why I close this PR, this has nothing to do with the quality of your code :)
I hope to see you soon around!

@emilevauge emilevauge closed this Jul 28, 2016
@ignaciovaquero
Copy link
Author

Hi! Yes, I understand :) If I find a moment, I'll be glad to help in implementing the refactoring labels method, which clearly meets our needs and is a great solution 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants