๐Ÿณ Sending Docker Logs to Grafana: Step-by-Step Guide

Day 74+75+76 : #90DaysOfDevOps Challange

ยท

4 min read

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:

  1. 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
  1. Update the list of available packages:
sudo apt-get update
  1. 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:

  1. Download Loki Config ๐Ÿ“‚
wget https://raw.githubusercontent.com/grafana/loki/v2.8.0/cmd/loki/loki-local-config.yaml -O loki-config.yaml
  1. 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
  1. Download Promtail Config ๐Ÿ“‚
wget https://raw.githubusercontent.com/grafana/loki/v2.8.0/clients/cmd/promtail/promtail-docker-config.yaml -O promtail-config.yaml
  1. 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:

  1. Go to http://<instance_ip>:3000 in your web browser and login with the username "admin" and password "admin" ๐Ÿ‘ค๏ฟฝ

  2. In the navigation drawer on the left, hover over the gear icon (second last one) and click on "Data Sources".

  3. Click on "Add data source" and search for "Loki". Click on it.

  4. 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)

  5. 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

  1. In the Grafana interface, click on the "+" icon in the left drawer and select "Dashboard".

  2. 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.

  1. Enter the following query in the query field under the panel window:

     count_over_time({job="my-container"} |~ "INFO" [5m])
    
  2. Add queries for "DEBUG" and "ERROR" level logs using similar syntax.

  3. 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.

  1. Create a new panel in the same dashboard.

  2. Use the following query to fetch the 10-minute rate of the count of total logs:

     rate({job="my-container"} [1m]) * 10 * 60
    
  3. 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! ๐Ÿ˜Š๐Ÿ”

Enjoy Monitoring with Beautiful Dashboards! ๐Ÿš€๐Ÿ“Š

Did you find this article valuable?

Support Dhananjay Kulkarni by becoming a sponsor. Any amount is appreciated!

ย