The domain name configuration

In my personal development environment, WHEN USING Traefik, I directly configured the docker connected by Traefik, so I was curious about how Traefik assigns domain names to containers, so I specially studied it.

Traefik supports setting labels as a domain name, for example, adding a container labels configuration:

labels:
  traefik.frontend.rule: Host:test.developer.io
Copy the code

Traefik assigns the container the domain name test.developer. IO.

However, the use of the time is still a little confused, always feel not understand the principle of uncomfortable, want to see the code in the end how to handle.

As introduced before, the core code is in the Provider of each service, and the logical code of docker domain name allocation is as follows:

// traefik/provider/docker/config.go func (p *Provider) getFrontendRule(container dockerData, segmentLabels map[string]string) string { if value := label.GetStringValue(segmentLabels, label.TraefikFrontendRule, ""); len(value) ! = 0 { return value } domain := label.GetStringValue(segmentLabels, label.TraefikDomain, p.Domain) if values, err := label.GetStringMultipleStrict(container.Labels, labelDockerComposeProject, labelDockerComposeService); err == nil { return "Host:" + getSubDomain(values[labelDockerComposeService]+"."+values[labelDockerComposeProject]) + "." + domain } if len(domain) > 0 { return "Host:" + getSubDomain(container.ServiceName) + "." + domain } return "" }Copy the code

There are three scenarios for assigning a domain name:

  1. First, read container labels directly to see if there are anytraefik.frontend.ruleThis label, if any, directly uses the configuration in this label
  2. And then read labels to see if there’s anycom.docker.compose.projectandcom.docker.compose.serviceThe values of the two labels, if any, are used to concatenate the container’s access domain name
  3. If both methods fail, use the name of the container as the access domain for the container

The second method, treafik, is largely undocumented and works when docker-compose is used or daoCloud is used. If traefik does not set the desired labels, check the labels setting of the container to see if the labels setting is the problem.

Other configuration

Finally, there are some other reverse proxy configurations for Traefik. Because Traefik is relatively new, there are many default configurations that fit current development requirements. For example, Host headers are automatically passed to back-end services, X-forwards are automatically added, and so on.

In addition, traefik supports rateLimit, retry, Circuit breaker, etc., which is pretty good :), traefik uses decorator mode a lot. Traefik can freely and flexibly piece together various HTTP request processing logic, while keeping the code clean and concise, high cohesion and low coupling. Take a look at this section of the code.