Geavanceerd
Zoeken inInstallatiegidsen
Zo zoek je
  • Plak een foutmelding tussen aanhalingstekens, bijv. "permission denied", om de oplossingstabel te vinden.
  • Kies een gebied en een deel van de gids (installeren, eerste instellingen, dagelijks gebruik, beveiliging, oplossingen) om direct naar de juiste stap te gaan.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

Gids 40Traefik · Web

Install and set up Traefik (cloud-native proxy)

Traefik is a reverse proxy that configures itself: it watches Docker (or Kubernetes) and creates routes from labels on your containers, and it gets Let's Encrypt certificates automatically. Add a container with two labels and it is online with HTTPS. This guide runs Traefik v3 with Docker Compose (the most common way), adds an app with labels, secures the dashboard and the Docker socket, shows the binary + systemd way, and covers Traefik in Kubernetes and the common fixes.

  • 10 Lessen
  • 11 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

40.1

Know it before you install

Entrypoints are the ports (80, 443). Routers match requests (Host(...), PathPrefix(...)) and send them to services (your containers). Middlewares change requests on the way (redirects, auth, headers, rate limits). Providers (Docker, Kubernetes, files) tell Traefik what exists. Static config (entrypoints, providers, ACME) is read at start; dynamic config (routers, services, middlewares) changes live.

ItemValue
Image / binarytraefik:v3.5 (Docker) or the release file from github.com/traefik/traefik/releases
Static configflags, or /etc/traefik/traefik.yml
Dynamic configDocker labels, Kubernetes objects, or files in /etc/traefik/dynamic/
Certificatesacme.json (holds private keys — mode 600)
Ports80, 443 (and 8080 for the dashboard only if you enable the insecure API — do not)

40.2

Run Traefik with Docker Compose (RHEL and Ubuntu)

Install Docker Engine first (see the Docker guide). Make a network for the proxy and a folder:

docker network create proxy
mkdir -p ~/traefik/letsencrypt && cd ~/traefik
touch letsencrypt/acme.json && chmod 600 letsencrypt/acme.json
~/traefik/compose.yaml
services:
  traefik:
    image: traefik:v3.5
    restart: unless-stopped
    command:
      - --providers.docker=true
      - --providers.docker.exposedbydefault=false
      - --providers.docker.network=proxy
      - --entrypoints.web.address=:80
      - --entrypoints.web.http.redirections.entrypoint.to=websecure
      - --entrypoints.web.http.redirections.entrypoint.scheme=https
      - --entrypoints.websecure.address=:443
      - --certificatesresolvers.le.acme.email=ops@example.com
      - --certificatesresolvers.le.acme.storage=/letsencrypt/acme.json
      - --certificatesresolvers.le.acme.httpchallenge.entrypoint=web
      - --api.dashboard=true
      - --log.level=INFO
      - --accesslog=true
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./letsencrypt:/letsencrypt
    networks: [proxy]
    labels:
      - traefik.enable=true
      - traefik.http.routers.dashboard.rule=Host(`traefik.example.com`)
      - traefik.http.routers.dashboard.entrypoints=websecure
      - traefik.http.routers.dashboard.tls.certresolver=le
      - traefik.http.routers.dashboard.service=api@internal
      - traefik.http.routers.dashboard.middlewares=dash-auth
      - traefik.http.middlewares.dash-auth.basicauth.users=admin:$$2y$$12$$Zq…
 
networks:
  proxy:
    external: true

Make the password hash with htpasswd -nBC 12 admin and write every $ as $$ in the Compose file.

docker compose up -d
docker compose logs -f traefik
SettingWhy
exposedbydefault=falseOnly containers with traefik.enable=true are published — nothing by accident.
Redirect web → websecureHTTP always goes to HTTPS.
certificatesresolvers.le…Automatic Let's Encrypt certificates (HTTP-01 on port 80).
docker.sock:roTraefik reads container labels; read-only (see "Make it safe").
Dashboard on its own HTTPS host with basic authNever the insecure :8080 dashboard.

Test against Let's Encrypt staging first: add --certificatesresolvers.le.acme.caserver=https://acme-staging-v02.api.letsencrypt.org/directory, then remove it and empty acme.json.

40.3

Publish an app with labels

~/shop/compose.yaml
services:
  shop:
    image: registry.example.com/shop:1.4.0
    restart: unless-stopped
    networks: [proxy]
    labels:
      - traefik.enable=true
      - traefik.http.routers.shop.rule=Host(`shop.example.com`)
      - traefik.http.routers.shop.entrypoints=websecure
      - traefik.http.routers.shop.tls.certresolver=le
      - traefik.http.services.shop.loadbalancer.server.port=3000
      - traefik.http.routers.shop.middlewares=sec-headers
      - traefik.http.middlewares.sec-headers.headers.stsSeconds=31536000
      - traefik.http.middlewares.sec-headers.headers.contentTypeNosniff=true
      - traefik.http.middlewares.sec-headers.headers.frameDeny=true
 
networks:
  proxy:
    external: true
cd ~/shop && docker compose up -d
curl -I https://shop.example.com
HTTP/2 200
strict-transport-security: max-age=31536000

No proxy config to edit — Traefik saw the new container and its labels. The app does not publish ports itself; only Traefik is public.

40.4

Without Docker: binary + systemd

For servers without containers, run the binary with a static file and a dynamic folder:

/etc/traefik/traefik.yml
entryPoints:
  web:
    address: ":80"
    http:
      redirections:
        entryPoint: { to: websecure, scheme: https }
  websecure:
    address: ":443"
providers:
  file:
    directory: /etc/traefik/dynamic
    watch: true
certificatesResolvers:
  le:
    acme:
      email: ops@example.com
      storage: /var/lib/traefik/acme.json
      httpChallenge: { entryPoint: web }
/etc/traefik/dynamic/shop.yml
http:
  routers:
    shop:
      rule: "Host(`shop.example.com`)"
      entryPoints: [websecure]
      service: shop
      tls: { certResolver: le }
  services:
    shop:
      loadBalancer:
        servers:
          - url: "http://10.0.1.11:3000"
          - url: "http://10.0.1.12:3000"

Run it with a systemd unit (ExecStart=/usr/local/bin/traefik --configFile=/etc/traefik/traefik.yml, a traefik user and AmbientCapabilities=CAP_NET_BIND_SERVICE for ports 80/443).

40.5

In Kubernetes

Traefik is the default ingress of k3s. Elsewhere install the Helm chart (helm repo add traefik https://traefik.github.io/charts, chart traefik/traefik). It reads normal Ingress objects and its own IngressRoute CRD:

kubectl get ingressroute -A
kubectl -n kube-system logs deploy/traefik --tail 20

40.6

Daily use: the commands and the real uses

docker compose -f ~/traefik/compose.yaml logs -f traefik
docker compose -f ~/traefik/compose.yaml pull && docker compose -f ~/traefik/compose.yaml up -d
docker inspect shop-shop-1 --format '{{json .Config.Labels}}' | jq .
curl -sI https://shop.example.com | head -3

Open the dashboard (https://traefik.example.com) to see every router, service and middleware, with errors in red.

Real uses at work:

  • One server with many Docker Compose apps, each with its own domain and automatic HTTPS.
  • Kubernetes ingress (k3s), canary releases with weighted services.
  • Middlewares for SSO (forward auth), IP allow lists, rate limits and headers.

40.7

Make it safe

Security checklist:

  • Access to docker.sock = root on the host. Mount it read-only, or better use a socket proxy (only the API calls Traefik needs).
  • exposedbydefault=false; publish only labelled containers.
  • Dashboard behind HTTPS + auth (or not exposed at all); never --api.insecure=true on a server.
  • acme.json mode 600 and in backups — it holds all private keys.
  • Security-header and rate-limit middlewares; pin the image version (traefik:v3.5) and update on purpose.

40.8

Update and remove

cd ~/traefik && docker compose pull && docker compose up -d
docker compose down

Read the migration notes for major versions (v2 → v3 changed some rule syntax and options).

40.9

Fix common problems

ProblemCauseFix
404 page not foundNo router matched: label missing (traefik.enable=true), wrong Host, or wrong entrypoint.Dashboard → HTTP routers; check the labels with docker inspect.
Bad Gateway (502)Wrong app port or the containers are not on the same network.loadbalancer.server.port; both on the proxy network (providers.docker.network).
Browser shows TRAEFIK DEFAULT CERTACME failed.docker compose logs traefik; port 80 open; DNS points here; acme.json mode 600.
permissions 644 for /letsencrypt/acme.json are too openMode not 600.chmod 600 letsencrypt/acme.json.
Dashboard 404No router to api@internal, or api.dashboard off.Use the dashboard labels above.
Error while parsing rule Host(...)Wrong quotes around the host name.Put the host name between backticks inside Host(…), as in the labels above.
Compose: $2y…: variable is not set$ in the bcrypt hash.Write every $ as $$.

40.10

Next steps

1Why set exposedbydefault=false?

Then only containers with traefik.enable=true are published, so nothing is exposed by accident.

2The browser shows "TRAEFIK DEFAULT CERT". What does it mean?

Getting the Let's Encrypt certificate failed; read the Traefik log (port 80, DNS, acme.json rights).

Alle commando's in dit hoofdstuk