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:
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: {}| Block | Role |
|---|---|
entryPoints.web | Port 80, redirects everything to HTTPS |
entryPoints.websecure | Port 443 |
certificatesResolvers.letsencrypt | Requests certificates from , proving domain control via port 80 (httpChallenge) |
providers.file | Reads routes in /dynamic, and reloads them as soon as a file changes (watch: true) |
accessLog | One log line per request |
Routes
One route = one file per application and per server. Example, the web in prod:
---
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"| Server | Domain | Sends to |
|---|---|---|
| dev | dev.trinity.jostud.com | http://trinity_web:3000 |
| dev | api.dev.trinity.jostud.com | http://trinity_api:8000 |
| prod | trinity.jostud.com | http://trinity_web:3000 |
| prod | api.trinity.jostud.com | http://trinity_api:8000 |
| prod | docs.trinity.jostud.com | http://trinity_docs:8080 |
The names trinity_web and trinity_api are the same everywhere:
- in dev, they are the
container_namevalues fromdocker-compose.dev.yml; - in prod, they are the Swarm service names: stack
trinity+ serviceweb.
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
---
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: trueacme.jsonstores the certificates. Ansible creates it with0600permissions, otherwise Traefik refuses to use it.- Traefik is on the
trinitynetwork, 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.