Project Phoenyx – Part 6: The Traffic Manager (Traefik Deep Dive)
Introduction: Understand Your Bouncer
Today we install the heart of our infrastructure: Traefik. If you have only worked with Apache or Nginx so far, Traefik might confuse you. It works differently. But once the penny drops, you will love it.
Imagine your server as a huge hotel.
- The Containers (WordPress, Nextcloud): These are the hotel rooms.
- The Domain (cloud.your-domain.com): That is the guest's name.
- Traefik: That is the receptionist at the front desk.
When a guest (a request from the internet) arrives, they don't ask for "Room 402". They say: "Hello, I want to see Mr. Nextcloud." Traefik looks at its current occupancy list, sees that Mr. Nextcloud is currently staying in Room 402 (Container ID xyz), and sends the guest there. If Mr. Nextcloud moves to another room (container restart), Traefik updates its list automatically.
The Core Concept: Static vs. Dynamic
This is where 90% of beginners fail. Traefik has two types of configurations. We must separate them strictly.
1. The Static Configuration (The Blueprint)
These are things that almost never change. They are set when Traefik starts.
- Which ports do I listen on? (80 and 443)
- Where do I write my logs?
- Who gives me SSL certificates? (Let's Encrypt)
- Where should I look for containers? (Docker Socket)
We define this in the file /opt/containers/traefik/traefik.yml.
2. The Dynamic Configuration (The Shout-outs)
These are things that can change constantly. Running containers come and go.
- "I am WordPress and I listen on blog.com"
- "I am Authentik and need extra security"
We do not write this info into a file. We write it as Labels directly onto the respective Docker containers. Traefik reads these labels live.
Step 1: The Static Configuration (traefik.yml)
Let's look at your traefik.yml in detail. I'll explain the most important blocks.
The EntryPoints (Front Doors)
entryPoints:
web:
address: ":80"
http:
redirections:
entryPoint:
to: websecure
scheme: https
websecure:
address: ":443"
Here we open port 80 (HTTP) and 443 (HTTPS). The genius trick here: We tell port 80 immediately: "Redirect everything automatically to websecure (HTTPS)." This means every service on your server is automatically accessible encrypted. No more manual redirect setup.
Cloudflare & Trusted IPs
You will find a long list under trustedIPs in your config. Why?
Since we use Cloudflare as DNS and Proxy, all visitors technically come from Cloudflare servers. Without this list, Traefik would think: "All visitors have Cloudflare's IP". If we want to use Crowdsec to block attackers, we need to know the real IP. With trustedIPs, we tell Traefik: "Trust Cloudflare when they say the request actually comes from IP 1.2.3.4".
Certificates (Let's Encrypt)
certificatesResolvers:
letsencrypt:
acme:
dnsChallenge:
provider: cloudflare
This is the "magic part". We use the DNS Challenge. Traefik proves domain ownership not by uploading a file, but by briefly setting a DNS record via the Cloudflare API. This allows us to get SSL certificates for servers that are not even publicly on the internet (e.g., in the intranet).
The Provider (Connection to Docker)
providers:
docker:
endpoint: "tcp://socket-proxy:2375" # Security!
exposedByDefault: false
Here our security concept from Part 5 kicks in. We don't connect to the dangerous socket, but to our proxy. exposedByDefault: false is extremely important: It means that no container is automatically exposed to the internet unless we explicitly allow it.
Step 2: The Traefik Container (docker-compose.yml)
Now we start Traefik itself. In the traefik-docker-compose.yml, you see:
services:
traefik:
image: traefik:latest
ports:
- "80:80"
- "443:443"
environment:
- CLOUDFLARE_DNS_API_TOKEN
volumes:
- ./traefik.yml:/etc/traefik/traefik.yml:ro
- ./acme/acme.json:/letsencrypt/acme.json
Important: The file acme.json stores your private SSL keys. If you lose it, you have to request all certificates again. Backups are mandatory here!
Step 3: Traefik Configures Itself (Labels)
Traefik is just a container too. How do we tell Traefik to display its own dashboard? With labels!
Let's look at the labels in traefik-docker-compose.yml. This is the "language" you have to speak for every new service (WordPress, Wiki, etc.) from now on.
labels: # 1. Turn on - "traefik.enable=true" # 2. Define Router (The Rule) - "traefik.http.routers.traefik.rule=Host(`traefik.your-domain.com`)" # 3. Assign Service (Where should it go?) - "traefik.http.routers.traefik.service=api@internal" # 4. Enable SSL - "traefik.http.routers.traefik.tls.certresolver=letsencrypt" # 5. Middleware (Security) - "traefik.http.routers.traefik.middlewares=auth-traefik,crowdsec"
The Anatomy of a Label:
traefik.http.routers.MY-NAME.rule: "MY-NAME" is a freely selectable name for this connection. It must be unique within Traefik.service=api@internal: This is a special Traefik service that provides the dashboard. Usually, this would state which internal port the container listens on (e.g., port 80 for nginx).middlewares: Here we chain security modules together. Before you see the dashboard, you have to pass through "auth-traefik" (Login) and "crowdsec" (IP Check).
Summary
We now have a system that:
- Listens on ports 80/443.
- Recognizes requests based on the domain (host).
- Automatically fetches certificates via Cloudflare.
- Dynamically pulls its configuration from the Docker containers.
If you run docker compose up -d now, the dashboard should be accessible at traefik.your-domain.com shortly (provided DNS is set). But beware: It is still wide open! In the next part, we install Crowdsec to activate the bouncer function.