Skip to content

Traefik ​

is the project's . It runs on each server, listens on ports 80 and 443, handles HTTPS and sends each domain name to the right container.

Static configuration ​

infra/traefik/traefik.yml, read at startup:

yml
entryPoints:
  web:
    address: ":80"
    http:
      redirections:
        entryPoint:
          to: websecure
          scheme: https
  websecure:
    address: ":443"

certificatesResolvers:
  letsencrypt:
    acme:
      email: "admin@trinitydevops.com"
      storage: /acme.json
      httpChallenge:
        entryPoint: web

providers:
  file:
    directory: /dynamic
    watch: true

log:
  level: INFO

accessLog: {}
BlockRole
entryPoints.webPort 80, redirects everything to HTTPS
entryPoints.websecurePort 443
certificatesResolvers.letsencryptRequests certificates from , proving domain control via port 80 (httpChallenge)
providers.fileReads routes in /dynamic, and reloads them as soon as a file changes (watch: true)
accessLogOne log line per request

Routes ​

One route = one file per application and per server. Example, the web in prod:

yml
---
http:
  routers:
    prod-web:
      rule: "Host(`trinity.jostud.com`)"
      entryPoints:
        - websecure
      tls:
        certResolver: letsencrypt
      service: prod-web

  services:
    prod-web:
      loadBalancer:
        servers:
          - url: "http://trinity_web:3000"
ServerDomainSends to
devdev.trinity.jostud.comhttp://trinity_web:3000
devapi.dev.trinity.jostud.comhttp://trinity_api:8000
prodtrinity.jostud.comhttp://trinity_web:3000
prodapi.trinity.jostud.comhttp://trinity_api:8000
proddocs.trinity.jostud.comhttp://trinity_docs:8080

The names trinity_web and trinity_api are the same everywhere:

  • in dev, they are the container_name values from docker-compose.dev.yml;
  • in prod, they are the Swarm service names: stack trinity + service web.

Why files rather than Docker labels?

Traefik can also read its configuration from container labels. With files, the dev and prod routes are readable side by side in the repository, and they do not depend on how the applications are started (Compose or Swarm).

The container ​

yml
---
services:
  traefik:
    image: traefik:v3.6
    container_name: trinity-traefik
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./traefik.yml:/traefik.yml:ro
      - ./dynamic:/dynamic:ro
      - ./acme.json:/acme.json
    networks:
      - trinity

networks:
  trinity:
    external: true
  • acme.json stores the certificates. Ansible creates it with 0600 permissions, otherwise Traefik refuses to use it.
  • Traefik is on the trinity network, shared with the applications. See The path of a request.

Certificates ​

On the first call to a domain, Traefik requests a certificate from Let's Encrypt and stores it in acme.json. It renews it on its own before expiry. Nothing to do by hand.

Port 80 must stay open

The httpChallenge goes through port 80. If you close it in the firewall, certificates are no longer renewed, and the site becomes unreachable when they expire.