Traefik
This reverse proxy sits in front of applications and sends each incoming request to the right one. Traefik can update its routing rules from sources such as Docker, Kubernetes, or configuration files without restarting.
What Traefik does
Suppose your storefront and API run on different internal ports, and the API has three replicas. Clients don't need to know any of those addresses: they connect to Traefik. A routing rule sends API requests to the API and storefront requests to the storefront. Once the destination application has been chosen, Traefik can spread requests across its replicas. That's the difference between routing and load balancing.
How a request reaches the backend
- An entry point is a listener on an address and port. A name such as
websecureis a configuration convention, not a setting that enables HTTPS by itself. - A router matches the request against a rule, such as a hostname and path. If several HTTP routers match, their priorities determine which one handles the request.
- A middleware can act on the request before it reaches the backend, for example by stripping a path prefix or applying a rate limit.
- A service tells Traefik where to forward the request. It can point to one backend or distribute traffic across several.
Traefik reads its entry points and provider settings at startup. Providers supply the dynamic configuration: routers, middlewares, and services. The Docker provider can build that configuration from container labels and update it as containers change. A file provider can watch YAML files instead. These routing updates don't require a process restart.
Example: send catalog traffic to one service
This dynamic YAML snippet assumes the file provider is enabled, the web entry point already exists, and Traefik can reach catalog:8080 on its internal network:
http:
routers:
catalog:
entryPoints: [web]
rule: 'Host(`shop.example.com`) && PathPrefix(`/catalog/`)'
service: catalog
services:
catalog:
loadBalancer:
servers:
- url: 'http://catalog:8080'
A request for shop.example.com/catalog/items goes to the catalog backend with /catalog/items intact. PathPrefix matches a path; it doesn't rewrite it. If the backend serves /items instead, attach StripPrefix middleware to the router.
Where it fits
On a Docker host, Traefik can give several containers one public entry point. In Kubernetes, it can run as an Ingress controller: an Ingress object declares HTTP routing rules, while the controller puts those rules into effect. Traefik can also terminate TLS at the proxy so the backend doesn't have to handle the client connection. With a configured ACME certificate resolver and successful domain validation, it can obtain and renew certificates automatically.
What comes up in interviews
A common troubleshooting question is: “The backend responds directly, but the public URL returns 404. Where do you look?” First use the logs to tell whether Traefik or the backend returned the response. If Traefik didn't match a router, check the entry point, hostname, and path rule. If the backend returned the 404, check the path it actually received. Adding replicas won't fix either kind of mismatch.