Photo by Ibrahim Boran on Unsplash
๐ณ Sending Docker Logs to Grafana: Step-by-Step Guide
Day 74+75+76 : #90DaysOfDevOps Challange
Introduction
Monitoring and analyzing logs is crucial for maintaining the health and performance of your Docker containers. Grafana, a popular open-source monitoring and visualization tool, provides a powerful platform for aggregating and visualizing logs from various sources. In this guide, we will walk through the process of setting up Grafana, Loki, and Promtail to send Docker logs to Grafana for monitoring and visualization. Let's get started! ๐ป๐
Step 1: Install Grafana on Debian or Ubuntu
To begin, we need to install Grafana on our system. Follow these steps:
- Open a terminal and execute the following commands:
sudo apt-get install -y apt-transport-https
sudo apt-get install -y software-properties-common wget
sudo wget -q -O /usr/share/keyrings/grafana.key https://apt.grafana.com/gpg.key
Stable release ๐
echo "deb [signed-by=/usr/share/keyrings/grafana.key] https://apt.grafana.com stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
Beta release ๐งช
echo "deb [signed-by=/usr/share/keyrings/grafana.key] https://apt.grafana.com beta main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
- Update the list of available packages:
sudo apt-get update
- Install the latest OSS release:
sudo apt-get install grafana
Step 2: Install Loki and Promtail using Docker
Now, let's proceed with installing Loki and Promtail using Docker. Execute the following commands:
- Download Loki Config ๐
wget https://raw.githubusercontent.com/grafana/loki/v2.8.0/cmd/loki/loki-local-config.yaml -O loki-config.yaml
- Run Loki Docker container ๐ณ
docker run -d --name loki -v $(pwd):/mnt/config -p 3100:3100 grafana/loki:2.8.0 --config.file=/mnt/config/loki-config.yaml
- Download Promtail Config ๐
wget https://raw.githubusercontent.com/grafana/loki/v2.8.0/clients/cmd/promtail/promtail-docker-config.yaml -O promtail-config.yaml
- Run Promtail Docker container ๐ณ
docker run -d --name promtail -v $(pwd):/mnt/config -v /var/log:/var/log --link loki grafana/promtail:2.8.0 --config.file=/mnt/config/promtail-config.yaml
Step 3: Start Grafana
Start Grafana by entering the following command:
sudo systemctl start grafana-server
This will start Grafana at http://<instance_ip>:3000 ๐๐
Step 4: Configure Loki as a Data Source in Grafana
Now, let's configure Loki as a data source in Grafana:
Go to http://<instance_ip>:3000 in your web browser and login with the username "admin" and password "admin" ๐ค๏ฟฝ
In the navigation drawer on the left, hover over the gear icon (second last one) and click on "Data Sources".
Click on "Add data source" and search for "Loki". Click on it.
Fill in the following details in the form:
Name: "Loki" (or any name you prefer)
URL: "http://localhost:3100" (the address where Loki server is running)
Click on "Save and Test". You should see a success message indicating that the data source is working correctly. โ
Step 5: Creating Visualizations in Grafana
Now, let's create a dashboard with multiple graphs to visualize our Docker logs in Grafana:
Dashboard Setup
In the Grafana interface, click on the "+" icon in the left drawer and select "Dashboard".
Click on the "Add new panel" button to design a new panel.
Panel 1: Log Counts Visualization ๐
We will make this panel show a line chart denoting the trend of the count of "INFO", "DEBUG", and "ERROR" level logs of our container.
Enter the following query in the query field under the panel window:
count_over_time({job="my-container"} |~ "INFO" [5m])
Add queries for "DEBUG" and "ERROR" level logs using similar syntax.
Edit the panel title, customize the font size, and click on "Apply".
Panel 2: 10-Minute-Rate of Total Logs ๐
This panel will display the 10-minute rate of the count of total logs being generated by the container.
Create a new panel in the same dashboard.
Use the following query to fetch the 10-minute rate of the count of total logs:
rate({job="my-container"} [1m]) * 10 * 60
Edit the panel title, customize the font size, and apply the changes.
Conclusion
By following this step-by-step guide, you have successfully set up Grafana, Loki, and Promtail to send Docker logs to Grafana for monitoring and visualization. Grafana's powerful visualization capabilities allow you to gain valuable insights into your Docker containers' log data. Explore further and unleash the full potential of Grafana to monitor and optimize your applications. Happy monitoring! ๐๐