Overview

What's Happening to the Requests?

Let's zoom in on Traefik's architecture and talk about the components that enable the routes to be created.

First, when you start Traefik, you define entrypoints (in their most basic forms, they are port numbers).Then, connected to these entrypoints, routers analyze the incoming requests to see if they match a set of rules.If they do, the router might transform the request using pieces of middleware before forwarding them to your services.

Architecture

Clear Responsibilities

  • Providers discover the services that live on your infrastructure (their IP, health, …)
  • Entrypoints listen for incoming traffic (ports, …)
  • Routers analyse the requests (host, path, headers, SSL, …)
  • Services forward the request to your services (load balancing, …)
  • Middlewares may update the request or make decisions based on the request (authentication, rate limiting, headers, …)

Example with a File Provider

Below is an example of a full configuration file for the file provider that forwards http://domain/whoami/ requests to a service reachable on http://private/whoami-service/.In the process, Traefik will make sure that the user is authenticated (using the BasicAuth middleware).

Static configuration:

  1. [entryPoints]
  2. [entryPoints.web]
  3. # Listen on port 8081 for incoming requests
  4. address = ":8081"
  5. [providers]
  6. # Enable the file provider to define routers / middlewares / services in a file
  7. [providers.file]
  8. filename = "dynamic_conf.toml"
  1. entryPoints:
  2. web:
  3. # Listen on port 8081 for incoming requests
  4. address: :8081
  5. providers:
  6. # Enable the file provider to define routers / middlewares / services in a file
  7. file:
  8. filename: dynamic_conf.yml
  1. # Listen on port 8081 for incoming requests
  2. --entryPoints.web.address=:8081
  3. # Enable the file provider to define routers / middlewares / services in a file
  4. --providers.file.filename=dynamic_conf.toml

Dynamic configuration:

  1. # http routing section
  2. [http]
  3. [http.routers]
  4. # Define a connection between requests and services
  5. [http.routers.to-whoami]
  6. rule = "Host(`domain`) && PathPrefix(`/whoami/`)"
  7. # If the rule matches, applies the middleware
  8. middlewares = ["test-user"]
  9. # If the rule matches, forward to the whoami service (declared below)
  10. service = "whoami"
  11. [http.middlewares]
  12. # Define an authentication mechanism
  13. [http.middlewares.test-user.basicAuth]
  14. users = ["test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/"]
  15. [http.services]
  16. # Define how to reach an existing service on our infrastructure
  17. [http.services.whoami.loadBalancer]
  18. [[http.services.whoami.loadBalancer.servers]]
  19. url = "http://private/whoami-service"
  1. # http routing section
  2. http:
  3. routers:
  4. # Define a connection between requests and services
  5. to-whoami:
  6. rule: "Host(`domain`) && PathPrefix(`/whoami/`)"
  7. # If the rule matches, applies the middleware
  8. middlewares:
  9. - test-user
  10. # If the rule matches, forward to the whoami service (declared below)
  11. service: whoami
  12. middlewares:
  13. # Define an authentication mechanism
  14. test-user:
  15. basicAuth:
  16. users:
  17. - test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/
  18. services:
  19. # Define how to reach an existing service on our infrastructure
  20. whoami:
  21. loadBalancer:
  22. servers:
  23. - url: http://private/whoami-service

The File Provider

In this example, we use the file provider.Even if it is one of the least magical way of configuring Traefik, it explicitly describes every available notion.

HTTP / TCP

In this example, we've defined routing rules for http requests only.Traefik also supports TCP requests. To add TCP routers and TCP services, declare them in a TCP section like in the following.Adding a TCP route for TLS requests on whoami.traefik.ioStatic configuration:

  1. [entryPoints]
  2. [entryPoints.web]
  3. # Listen on port 8081 for incoming requests
  4. address = ":8081"
  5. [providers]
  6. # Enable the file provider to define routers / middlewares / services in a file
  7. [providers.file]
  8. filename = "dynamic_conf.toml"
  1. entryPoints:
  2. web:
  3. # Listen on port 8081 for incoming requests
  4. address: :8081
  5. providers:
  6. # Enable the file provider to define routers / middlewares / services in a file
  7. file:
  8. filename: dynamic_conf.yml
  1. # Listen on port 8081 for incoming requests
  2. --entryPoints.web.address=":8081"
  3. # Enable the file provider to define routers / middlewares / services in a file
  4. --providers.file.filename=dynamic_conf.toml

Dynamic configuration:

  1. # http routing section
  2. [http]
  3. [http.routers]
  4. # Define a connection between requests and services
  5. [http.routers.to-whoami]
  6. rule = "Host(`domain`) && PathPrefix(`/whoami/`)"
  7. # If the rule matches, applies the middleware
  8. middlewares = ["test-user"]
  9. # If the rule matches, forward to the whoami service (declared below)
  10. service = "whoami"
  11. [http.middlewares]
  12. # Define an authentication mechanism
  13. [http.middlewares.test-user.basicAuth]
  14. users = ["test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/"]
  15. [http.services]
  16. # Define how to reach an existing service on our infrastructure
  17. [http.services.whoami.loadBalancer]
  18. [[http.services.whoami.loadBalancer.servers]]
  19. url = "http://private/whoami-service"
  20. [tcp]
  21. [tcp.routers]
  22. [tcp.routers.to-whoami-tcp]
  23. rule = "HostSNI(`whoami-tcp.traefik.io`)"
  24. service = "whoami-tcp"
  25. [tcp.routers.to-whoami-tcp.tls]
  26. [tcp.services]
  27. [tcp.services.whoami-tcp.loadBalancer]
  28. [[tcp.services.whoami-tcp.loadBalancer.servers]]
  29. address = "xx.xx.xx.xx:xx"
  1. # http routing section
  2. http:
  3. routers:
  4. # Define a connection between requests and services
  5. to-whoami:
  6. rule: Host(`domain`) && PathPrefix(`/whoami/`)
  7. # If the rule matches, applies the middleware
  8. middlewares:
  9. - test-user
  10. # If the rule matches, forward to the whoami service (declared below)
  11. service: whoami
  12. middlewares:
  13. # Define an authentication mechanism
  14. test-user:
  15. basicAuth:
  16. users:
  17. - test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/
  18. services:
  19. # Define how to reach an existing service on our infrastructure
  20. whoami:
  21. loadBalancer:
  22. servers:
  23. - url: http://private/whoami-service
  24. tcp:
  25. routers:
  26. to-whoami-tcp:
  27. service: whoami-tcp
  28. rule: HostSNI(`whoami-tcp.traefik.io`)
  29. services:
  30. whoami-tcp:
  31. loadBalancer:
  32. servers:
  33. - address: xx.xx.xx.xx:xx