Visualizing Traefik Metrics and HTTP Logs in Grafana
In my previous blog posts I outlined the necessary steps in order to create a monitoring dashboard with Grafana and various other software components such as Telegraf and InfluxDB. Moreover, I provided you guidance on how to use Grafana Loki as well as Promtail to visualize HTTP logs from Nginx Proxy Manager.
My older blog posts can be found here:
As I've changed my IT infrastructure to run Traefik as reverse proxy, the need to change my Grafana monitoring dashboard slightly arised. Especially, as Traefik natively supports pushing metrics directly into a database like InfluxDB.
Introduction
In today's blog post, I will provide you with detailed steps on how to activate metrics in Traefik and push them into a containerized InfluxDB database.
Moreover, we will enable logging of HTTP requests for Traefik and use Promtail to push them into Grafana Loki. Finally, we will configure the InfluxDB and Loki data sources in Grafana and import a pre-built monitoring dashboard by me to inspect Traefik metrics and HTTP logs.
The dashboard will look something like this in the end:
Configuring Container Services
This section contains the mandatory configuration steps for Traefik, InfluxDB as well as Promtail and Grafana Loki.
The general docker-compose.yml
file we will be using is the following:
As can be seen in the above YML file, we have configured various volume mounts holding our configuration files for the services. In my case, all volume mounts are located at /mnt/docker-volumes/<container-name>
. Adjust to your infrastructure setup if needed.
In the following we will discuss the necessary configuration steps for all container services.
Traefik
For this blog post I will not go into detail on how to configure and setup a full-blown, dockerized Traefik instance. If you do not have a Traefik instance yet, may head over to my GitHub repository, which holds all necessary things.
However, in order to push Traefik metrics into an InfluxDB database, you must adjust your static configuration file of Traefik. Add the following code snippet:
Furthermore, we have to enable access logs for Traefik. To enable access logs for Traefik, adjust your static configuration file again and add the following lines:
That's basically it for configuring the Traefik reverse proxy.
InfluxDB
As we have enabled metrics for Traefik, which will be later pushed into an InfluxDB database, we must setup such database of course. The above docker-compose.yml
already holds our necessary InfluxDB container service.
However, besides that, we also have to configure two additional configuration files:
- influxdb.conf
- /init/create-traefik.iql
The influxdb.conf
file will be stored in the root directory of the volume mount for InfluxDB. The configuration file holds the following entries (nothing to adjust):
Besides that we have to create an initialization file, which will be run when the container starts. We will utilize this startup run feature to initialize our database. The init configuration file will be stored within the init
volume mount directory.
It can be named randomly but must have the extension .iql
. I've named my configuration file create-traefik.iql
and it holds the following data:
Promtail
In order for our Promtail container to access, read and parse log data, we must specify where our log data is available and which of them should be parsed. The log data must be mounted inside the Promtail container, which we defined in the docker-compose.yml
file above.
Specifying which log file to parse is done through a promtail-config.yml
configuration file, located at /mnt/docker-volumes/promtail/promtail-config.yml
in my case.
For our use case, I'll force Promtail to parse the following two logs:
- Auth logs: We've successfully bind mounted the logs of my Linux server at
/var/log
into the Promtail container. Therefore, let's use it! I want to parse the well-knownauth.log
log file that holds many interersting things such as SSH logins etc. - Traefik logs: Additionally, I want to parse the log files of Traefik. Access logging was enabled previously in the static configuration file of Traefik. The log file will be stored in the directory
/logs
of your Traefik container volume.
The configuration file should look like this (nothing to adjust):
/var/log/traefik
is dependent on the used docker volume mount. In the above defined docker-compose.yml
, we mounted our Traefik logs as well as auth logs into the Promtail container.Loki
Finally, we must also define a configuration file for Loki. The file is called loki-config.yml
and defined in the above docker-compose.yml
. In my case, it is located at /mnt/docker-volumes/loki/loki-config.yml
.
The config file should contain the following (nothing to adjust):
Spawning Our Docker Containers
If you successfully adjusted the above docker-compose.yml
file to your needs and ensured that all configuration files for InfluxDB, Promtail and Loki exist, we will now be able to proceed booting up our Docker stack of multiple containers.
A single Linux command from the directory your docker-compose.yml
is located and your containers should start to see daylight:
sudo docker compose up -d
If everything went well, you should now be able to log into your Grafana instance at http://<your-servers-ip-address>:3000
via a web browser.
admin
.Configuring Grafana
As soon as the Grafana instance is ready, we must complete our last step of adding data sources as well as creating a new dashboard.
Defining Data Sources
Upon logging into your fresh Grafana instance via a web browser, we must specify our data sources.
docker-compose.yml
file, the containers will be put inside the same Docker network. Therefore, we can easily use the container names instead of IP addresses.Head over to the URL /datasources
and choose Loki as data source. Configure it as follows:
Additionally, configure another datasource for InfluxDB. Head over to the URL /datasources
again and now choose InfluxDB as data source. You must define an InfluxDB URL as well as the database, user and password for authentication.
traefikuser
as well as the password MyVeryStrongInfluxdbPassword
. Use these credentials, together with the defined database name traefik
, when adding InfluxDB as data source.If you changed those credentials, use the new ones! Importing a Dashboard
Finally, we have everything in place to start graphing. Grafana is up and running and we successfully configured InfluxDB and Loki as data sources to pull our data points from. Since creating a Grafana dashboard is kinda time consuming, I have uploaded my template here. Feel free to use it as a starting point:
Browse to the Grafana URL /dashboard/import
and upload the above JSON file. Select the configured InfluxDB and Loki sources as default data sources. You should then be redirected to your newly added dashboard automatically, which hopefully displays all statistics correctly.
Member discussion