Project Phoenyx – Part 6: The Traffic Manager (Traefik Deep Dive)

Category: Core Stack / Proxy | Read Time: approx. 20-25 minutes

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.

The Hotel Analogy

Imagine your server as a huge hotel.

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.

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.

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:


Summary

We now have a system that:

  1. Listens on ports 80/443.
  2. Recognizes requests based on the domain (host).
  3. Automatically fetches certificates via Cloudflare.
  4. 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.