⚙️ Traefik 2, Prometheus metrics

Traefik 2 can be configured to give some metrics for Prometheus. Learn how here.

logo traefik

When Traefik is up and running and your routers/services are configured, it is important to monitor them. To do it, I'm going to set up Traefik to be scraped by Prometheus, among other things.

Version Date Comment
1 05/2022 Post creation
1.1 08/2023 Update versions

Goal : Configure Traefik and Prometheus

Environment : Debian 12, Docker 24.x, docker compose (plugin) 2.20.x, Traefik 2.10.

Execution context :

jho@vmi866042:/opt/docker/dc$ tree
.
├── conf
│   ├── acme.json
│   ├── traefik.yml
│   ├── traefikdynamic
│   │   ├── general.yml
│   │   ├── routersservices.yml
├── docker-compose.yml
└── logs
    ├── traefikAccess.log
    ├── traefik.log
  • path where are every folder and files : /opt/docker/dc
  • path of the principal configuration file for Traefik : /opt/docker/dc/conf/traefik.yml
  • folder where are every dynamic configuration files : /opt/docker/dc/conf/traefikdynamic
  • path of the file which is used to store SSL certificates for let's encrypt (or other provider) : /opt/docker/dc/conf/acme.json
  • folder to store logs : /opt/docker/dc/logs/

This post is inspired by the official documentation. Traefik supports 4 monitoring backends : Datadog, Metricd, Prometheus et InfluxDB.
Metrics give you some information in real-time, like stats about configuration reloading, number of entry requests in HTTP or HTTPS, number of redirections, number of open connections...

Traefik configuration

I will use Prometheus to scrape Traefik metrics and store them locally. To do it, the main configuration file traefik.yml needs to be updated with few options. In the entry points block, insert these lines : (port number can be changed)

entryPoints:
  metrics:
    address: ":9090"

At the end of the file, add this bloc (YAML format) :

metrics:
  prometheus:
    entryPoint: metrics
    addEntryPointsLabels: true
    addServicesLabels: true
    addRoutersLabels: true
    buckets:
      - 0.1
      - 0.3
      - 1.2
      - 5.0

Don't forget to restart Traefik to take into account. Beware of, do not expose this entry point to the web!

Complete traefik.yml file :

---
global:
  sendAnonymousUsage: false
  checkNewVersion: false

api:
  dashboard: true

log:
  filePath: "/etc/traefik/applog.log"
  format: json
  level: "ERROR"

providers:
  docker:
    endpoint: unix:///var/run/docker.sock
    exposedByDefault: false
    watch: true
    swarmMode: false
  file:
    directory: "/dynamic"
    watch: true

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

metrics:
  prometheus:
    entryPoint: metrics
    addEntryPointsLabels: true
    addServicesLabels: true
    addRoutersLabels: true
    buckets:
      - 0.1
      - 0.3
      - 1.2
      - 5.0

Prometheus configuration

Simply, add this bloc in the prometheus.yml file:

...

scrape_configs:
  - job_name: 'traefik'
    static_configs:
      - targets: ['traefik:9090']

Don't forget to change the port number if you changed it before in the traefik.yml file. Now, Prometheus can scrape Traefik by the specific metrics entry point. Every Traefik metrics in Prometheus are entitled traefik_.

Source