Traefik: a modern reverse proxy, installation and configuration
Introduction
Traefik is an open-source Application Proxy that makes publishing your services a fun and easy experience. It receives requests on behalf of your system, identifies which components are responsible for handling them, and routes them securely.
What sets Traefik apart, besides its many features, is that it automatically discovers the right configuration for your services. The magic happens when Traefik inspects your infrastructure, where it finds relevant information and discovers which service serves which request.
Traefik is natively compliant with every major cluster technology, such as Kubernetes, Docker Swarm, AWS, and the list goes on; and can handle many at the same time. (It even works for legacy software running on bare metal.)
With Traefik, there is no need to maintain and synchronize a separate configuration file: everything happens automatically, in real time (no restarts, no connection interruptions). With Traefik, you spend time developing and deploying new features to your system, not on configuring and maintaining its working state.
Prerequisites
In this tutorial, we will use Docker to run Traefik. You need to have Docker and Docker Compose installed on your system.
We’ll also use Cloudflare as a DNS provider for automatic SSL certificate generation and renewal. You need to have a Cloudflare account and an API key.
Prepare your Traefik configuration
Create a directory for your Traefik configuration files. You can name it traefik
or any other name you prefer.
|
|
Docker-compose configuration
With docker-compose, here is a simple example of how to install Traefik (docker-compose.yml
):
|
|
Here is a breakdown of the important configuration part:
volumes
: Mount the Docker socket and Traefik configuration files./var/run/docker.sock:/var/run/docker.sock
: This allows Traefik to communicate with the Docker daemon and discover services../traefik/config:/etc/traefik
: This is where you can put your Traefik configuration files../traefik/logs:/logs
: This is where Traefik will store its logs.
labels
: These are Docker labels that Traefik uses to configure routing.traefik.enable: true
: This enables Traefik for this service.traefik.domain
: This is the domain name for your service.traefik.tags
: These are tags that can be used to group services.traefik.frontend.rule
: This is the rule that Traefik will use to route requests to this service.traefik.docker.network
: This is the Docker network that Traefik will use to communicate with this service.
environment
: These are environment variables that Traefik will use.TZ
: This sets the timezone for Traefik.CLOUDFLARE_EMAIL
: This is your Cloudflare email address.CLOUDFLARE_API_KEY
: This is your Cloudflare API key.
Then you can start Traefik with the following command:
Traefik Configuration
Basics
Here is a simple example of a Traefik configuration file (traefik.yml
) to store in the config
directory:
|
|
Dynamic Docker Configuration
You can use Docker labels to configure Traefik dynamically. Here is an example of how to configure a service with Docker labels in your docker-compose.yml
file:
|
|
Once the service is started, Traefik will automatically discover it and route requests to blog.mycompany.com
to the WordPress container with a TLS certificate issued by Let’s Encrypt.
Proxy to a service
You can also use Traefik to proxy requests to a service running on a different host. For example, if you have a service running on a host with IP 192.168.0.2
on port 80
, you can configure Traefik this way in your rules.yml
file:
|
|
Redirect
You can add a redirect rule to your Traefik configuration file (rules.yml
) to redirect domain requests to a new domain. This is useful if you want to redirect traffic from an old domain to a new one.
|
|
Middlewares
You can use Traefik middleware to modify requests and responses. There are two middlewares I like to use:
- sablier: An free and open-source software to start workloads on demand and stop them after a period of inactivity.
- crowdsec: CrowdSec provides open source solution for detecting and blocking malicious IPs, safeguarding both infrastructure and application security.
Sablier
First of all, you need to install Sablier. You can do this by adding the following to your docker-compose.yml
file, next to your Traefik service:
|
|
Create the configuration file for Sablier in the sablier/config
directory (to match the config file name in the docker-compose config). You can name it sablier.yaml
. Here is an example configuration:
|
|
You now have to run it:
|
|
Then, you need to add the Sablier plugin to your Traefik configuration. You can do this by adding the following to your traefik.yml
file:
|
|
traefik.yml
fileFrom the Docker labels, you can add the following to your service configuration in the docker-compose.yml
file. Here is an example for pgAdmin:
|
|
Add in the rules.yml
file, add the following middleware configuration (this configuration can be mutualized for all your services):
The options are:
dynamic
: The dynamic configuration for the Sablier plugin.displayName
: The name of the service to display in Sablier during loading.group
: The group name for the service.sablierUrl
: The URL of the Sablier service (same as the one in the docker-compose file).sessionDuration
: The duration of the session in Sablier before it stops the service.
References
Last updated 03 May 2025, 23:40 CEST.