Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to use static segment backend names with the Docker provider #4723

Closed
mback2k opened this issue Apr 4, 2019 · 15 comments
Closed

Comments

@mback2k
Copy link

mback2k commented Apr 4, 2019

Do you want to request a feature or report a bug?

Feature

What did you expect to see?

I expected the Docker container/service label "traefik.{segment-name}.backend" to behave the same way as "traefik.backend". Its raw value should have been used as the backend name instead of it being prefixed with the service name.

I understand that the default is to include the service name to avoid naming conflicts due to duplicates. But wouldn't this already be achieved by including the segment name inside the backend name?

The function used for "traefik.backend" is getDefaultBackendName which either uses just the provided value or the service name.

The function used for "traefik.{segment-name}.backend" is getSegmentBackendName which either uses the provided value prefixed with the service name or the segment name prefixed with the service name. So the segment name is already, but only used in case no value was given.

I think the behaviour of getSegmentBackendName is not consistent and causes trouble with random container names showing up, e.g. in Prometheus metrics. I would like to see it aligned with getDefaultBackendName by using the provided value or the service name, each suffixed with the segment name instead of always including the service name.

Basically this would mean to use the segment instead of the service name with the provided value, and make the service name the default value.

Examples of label to backend name conversion at the moment:

  • traefik.backend: "my-single-backend" --> "my-single-backend"
  • traefik.backend: unset --> "my-container/service-name"
  • traefik.my-segment.backend: "my-single-backend" --> "my-container/service-name-my-single-backend"
  • traefik.my-segment.backend: unset --> "my-container/service-name-my-segment"

Examples of label to backend name conversion with this feature request implemented:

  • traefik.backend: "my-single-backend" --> "my-single-backend"
  • traefik.backend: unset --> "my-container/service-name"
  • traefik.my-segment.backend: "my-single-backend" --> "my-single-backend-my-segment"
  • traefik.my-segment.backend: unset --> "my-container/service-name-my-segment"

This would make it possible to have static backend names by setting the .backend label. What do you think?

At the moment I am unable to really monitor the metrics produced by traefik from my docker services with prometheus and grafana due to random metric names.

mback2k added a commit to mback2k/traefik that referenced this issue Apr 5, 2019
This makes it possible to have constant backend names, see traefik#4723.
mback2k added a commit to mback2k/traefik that referenced this issue Apr 5, 2019
Use the user-provided value and the service name only as fallback
inside segment-based backend names to make them user-configurable.

This makes it possible to have constant backend names, see traefik#4723.
@ldez
Copy link
Member

ldez commented Apr 5, 2019

To understand the current behavior you need to read #3485, #3482, #3317, #3351

@mback2k
Copy link
Author

mback2k commented Apr 5, 2019

@ldez Thanks for the hints. Actually I already came across most of these. That is why I am open for discussion on how to make it possible to have both dynamic (scalable) and constant (fixed) backend names. Both PRs I just posted still allow dynamic backend names if the corresponding .backend label is not set.

@ldez
Copy link
Member

ldez commented Apr 5, 2019

Note that in branch v1.7, we only accept bug-fixes.

@mback2k
Copy link
Author

mback2k commented Apr 5, 2019 via email

@ldez
Copy link
Member

ldez commented Apr 5, 2019

You can try to use a custom template:

https://docs.traefik.io/configuration/backends/docker/#docker

[docker]
templateVersion = 2
filename = "my_docker.tmpl"

https://github.com/containous/traefik/blob/v1.7/templates/docker.tmpl

@mback2k
Copy link
Author

mback2k commented Apr 5, 2019

Sorry, but I don't understand how I can influence the internal backend name via the template. To me it looks like $backendName is already setup outside of this template. And in another place it for some reason uses getBackendName instead. Could you please give me another pointer in the right direction? Thanks in advance!

@dduportal dduportal added this to To do in v2 via automation Apr 5, 2019
@mback2k
Copy link
Author

mback2k commented Apr 5, 2019

@dduportal thanks for considering it for v2, please see #4731 and #4732 for possible implementation variants.

@ldez ldez removed this from To do in v2 Apr 5, 2019
@ldez
Copy link
Member

ldez commented Apr 5, 2019

@mback2k sorry but @dduportal made a mistake: in the v2 the segments don't exist at all. it's a completely different approach.

@ldez
Copy link
Member

ldez commented Apr 5, 2019

@ldez ldez added status/0-needs-triage kind/enhancement a new or improved feature. priority/P3 maybe and removed kind/enhancement a new or improved feature. priority/P3 maybe status/0-needs-triage labels Apr 6, 2019
@mback2k
Copy link
Author

mback2k commented Apr 7, 2019

@ldez thanks a lot, again. I managed to implement #4731 via the following template snippets:

Backends:

[backends]
{{range $backendName, $servers := .Servers}}
  {{ $backend := index $servers 0 }}

  {{ $composeService := getLabelValue $backend.Labels "com.docker.compose.service" "" }}
  {{ $composeProject := getLabelValue $backend.Labels "com.docker.compose.project" "" }}
  {{ $serviceName := $backend.ServiceName }}
  {{if and $composeService $composeProject }}
  {{ $serviceName = print $composeService "_" $composeProject }}
  {{end}}
  {{ $backendName := getLabelValue $backend.SegmentLabels "traefik.backend" $serviceName }}
  {{ $segmentName := $backend.SegmentName }}
  {{if $segmentName }}
  {{ $backendName = print $backendName "-" $segmentName }}
  {{end}}
  {{ $backendName = $backendName | replace "." "-" }}
...

Frontends:

[frontends]
{{range $frontendName, $containers := .Frontends }}
  {{ $container := index $containers 0 }}

  {{ $composeService := getLabelValue $container.Labels "com.docker.compose.service" "" }}
  {{ $composeProject := getLabelValue $container.Labels "com.docker.compose.project" "" }}
  {{ $serviceName := $container.ServiceName }}
  {{if and $composeService $composeProject }}
  {{ $serviceName = print $composeService "_" $composeProject }}
  {{end}}
  {{ $backendName := getLabelValue $container.SegmentLabels "traefik.backend" $serviceName }}
  {{ $segmentName := $container.SegmentName }}
  {{if $segmentName }}
  {{ $backendName = print $backendName "-" $segmentName }}
  {{end}}
  {{ $backendName = $backendName | replace "." "-" }}

  [frontends."frontend-{{ $frontendName }}"]
    backend = "backend-{{ $backendName }}"
...

@mback2k
Copy link
Author

mback2k commented Apr 7, 2019

Slightly updated version due to the fact that the backend label value must already be unique and it is therefore not required to always append the segment name.

Backends:

[backends]
{{range $backendName, $servers := .Servers}}
  {{ $backend := index $servers 0 }}

  {{ $composeService := getLabelValue $backend.Labels "com.docker.compose.service" "" }}
  {{ $composeProject := getLabelValue $backend.Labels "com.docker.compose.project" "" }}
  {{ $serviceName := $backend.ServiceName }}
  {{if and $composeService $composeProject }}
  {{ $serviceName = print $composeService "_" $composeProject }}
  {{end}}
  {{if $backend.SegmentName }}
  {{ $serviceName = print $serviceName "-" $backend.SegmentName }}
  {{end}}
  {{ $backendName := getLabelValue $backend.SegmentLabels "traefik.backend" $serviceName }}
  {{ $backendName = $backendName | replace "." "-" }}
...

Frontends:

[frontends]
{{range $frontendName, $containers := .Frontends }}
  {{ $container := index $containers 0 }}

  {{ $composeService := getLabelValue $container.Labels "com.docker.compose.service" "" }}
  {{ $composeProject := getLabelValue $container.Labels "com.docker.compose.project" "" }}
  {{ $serviceName := $container.ServiceName }}
  {{if and $composeService $composeProject }}
  {{ $serviceName = print $composeService "_" $composeProject }}
  {{end}}
  {{if $container.SegmentName }}
  {{ $serviceName = print $serviceName "-" $container.SegmentName }}
  {{end}}
  {{ $backendName := getLabelValue $container.SegmentLabels "traefik.backend" $serviceName }}
  {{ $backendName = $backendName | replace "." "-" }}

  [frontends."frontend-{{ $frontendName }}"]
    backend = "backend-{{ $backendName }}"
...

@traefiker traefiker added the kind/question a question label Apr 8, 2019
@traefiker
Copy link
Contributor

Hi! I'm Træfiker 🤖 the bot in charge of tidying up the issues.

Thanks again for your interest in Traefik!

I'm glad to close the issue because I think that the question has been answered (Yes, I'm quite an advanced AI 😄)

Join our Slack workspace for more community #support.

@mback2k
Copy link
Author

mback2k commented Apr 8, 2019

@ldez Is @traefiker right in this case? Or is this something that still needs to be considered for v2 even though segments are no longer in use?

@ldez
Copy link
Member

ldez commented Apr 8, 2019

yes Traefiker is right: v2 has a completely different approach, the label system has been rewritten.

@dduportal
Copy link
Contributor

Hi @mback2k , sorry for the misunderstanding.

THanks @ldez for taking care of this exchange ❤️

@traefik traefik locked and limited conversation to collaborators Sep 1, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants