In this article, we’ll walk through the process of monitoring Docker containers using Grafana's Loki data source. I’m sharing this because I faced several challenges when setting up container monitoring with Grafana on my server, and I thought it might help others. Let's get started!
What is Grafana Loki?
Grafana Loki is a log aggregation system designed to store and query logs in a cost-effective, user-friendly way. It is commonly used with Grafana to visualize logs and troubleshoot issues across containerized applications. To use the Grafana data source with Loki, we first need to install Grafana on our host. We’ll do this by pulling the Grafana Docker container from Docker Hub and then add Grafana Loki and Promtail by pulling images from a container registry.
Setting Up Loki and Promtail Configuration Files
Before pulling the containers, we need to create configuration files for Loki and Promtail on the host. These files contain the settings for how Loki and Promtail will run, where they will store data, and what they will monitor.
Step 1: Create the Loki Configuration File
The Loki configuration file specifies how Loki will store and manage logs. Here’s how to set it up:
- Create a directory for Grafana to keep related files organized:
mkdir grafana
Open a new file for the Loki configuration:
vi loki-local-config.yaml
Paste the following configuration code:
auth_enabled: false
server:
http_listen_port: 3100
grpc_listen_port: 9096
common:
instance_addr: 127.0.0.1
path_prefix: /tmp/loki
storage:
filesystem:
chunks_directory: /tmp/loki/chunks
rules_directory: /tmp/loki/rules
replication_factor: 1
ring:
kvstore:
store: inmemory
query_range:
results_cache:
cache:
embedded_cache:
enabled: true
max_size_mb: 100
schema_config:
configs:
- from: 2020-10-24
store: tsdb
object_store: filesystem
schema: v13
index:
prefix: index_
period: 24h
ruler:
alertmanager_url:
http://localhost:9093
Explanation:
auth_enabled: false
disables authentication, simplifying access.server
specifies the ports for HTTP and gRPC communication, enabling Loki to listen on port 3100 for queries and on 9096 for gRPC requests.storage
specifies that logs will be stored in the local filesystem, defining directories for chunks and rules.schema_config
defines how logs are indexed and stored in Loki over time.ruler
provides settings for alerting, though you can modify it as needed.
- Save and close the file.
Step 2: Create the Promtail Configuration File
Promtail acts as a log collector, scraping log data and sending it to Loki. Here’s how to set up its configuration file:
Open a new file for the Promtail configuration:
vi promtail-config.yaml
Paste the following configuration code:
server:
http_listen_port: 9080
grpc_listen_port: 0
positions: filename: /tmp/positions.yaml
clients:
- url:
http://loki:3100/loki/api/v1/push
scrape_configs:
- job_name: system
static_configs:
- targets:
labels:
job: varlogs
path: /var/log/*log
Explanation:
server
defines the ports for HTTP access.positions
keeps track of how far Promtail has read in the log files, preventing it from re-sending logs on restart.clients
specifies where Promtail should send the logs; here, it’s configured to push logs to Loki on port 3100.scrape_configs
defines the targets and file paths for Promtail to monitor—in this case, it’s set to monitor/var/log
for any log files.
Save and close the file.
Creating a Docker Compose File for Grafana, Loki, and Promtail
Now that we have the configuration files, we need to create a Docker Compose file to define and manage the Grafana, Loki, and Promtail services. This file will allow us to easily start, stop, and manage the services.
Open or create a
docker-compose.yml
file:vi docker-compose.yml
Paste the following configuration code:
version: '3'
services:
grafana:
image: grafana/grafana
ports: - "3000:3000"
volumes:
- grafana-storage:/var/lib/grafana
loki:
image: grafana/loki
ports: - "3100:3100"
volumes:
- ./loki-local-config.yaml:/etc/loki/local-config.yaml
command: -config.file=/etc/loki/local-config.yaml
promtail:
image: grafana/promtail
volumes:
- /var/log:/var/log
- ./promtail-config.yaml:/etc/promtail/config.yaml
command: -config.file=/etc/promtail/config.yaml
Explanation:
The
grafana
service pulls the Grafana image, exposing it on port 3000 for the web interface.The
loki
service uses theloki-local-config.yaml
file to configure its settings and makes it accessible on port 3100.The
promtail
service mounts the/var/log
directory to read system logs and uses the configuration file we created earlier to define log collection and push settings.
Save and close the file.
Configuring Docker to Send Logs to Loki
We now need to set up Docker to use the Loki logging driver, enabling it to send container logs directly to Loki.
Open the Docker daemon configuration file:
vi /etc/docker/daemon.json
Add the following configuration code:
{ "debug": true,
"log-driver": "loki",
"log-opts": {
"loki-url": "
http://localhost:3100/loki/api/v1/push
",
"loki-batch-size": "400"
}
}
Explanation:
"log-driver": "loki"
configures Docker to use Loki as the log driver."loki-url"
specifies the URL where Loki will receive the log data."loki-batch-size"
controls the size of log batches sent to Loki, allowing for better performance by reducing the frequency of network requests.
Save and close the file.
Installing the Docker Driver for Loki
Now, let’s install the Docker driver to send logs to Loki. This step enables Docker to directly forward logs from all containers to Loki.
docker plugin install grafana/loki-docker-driver:latest --alias loki --grant-all-permissions
After installation, restart Docker to apply the changes:
systemctl restart docker
Once the services are up, open your browser and navigate to your server's IP address with port 3000 (e.g., http://<your-ip>:3000
). The default username and password for Grafana is admin
.
Adding Loki as a Data Source in Grafana
With Grafana running, let’s add Loki as a data source so we can view logs from our containers.
Go to Connections in Grafana.
Click on Add Data Source.
Search for Loki, click on it, and enter the following URL:
Save and set up the data source
Finally, go to Explore in Grafana, select Loki, and start viewing your logs.
In this article, we configured Loki and Promtail to monitor Docker container logs. Each step contributes to capturing and visualizing logs in Grafana:
Loki serves as the central log storage and retrieval system.
Promtail collects logs from the host and pushes them to Loki.
Docker Configuration allows all container logs to be directly sent to Loki.
Grafana Integration provides a user-friendly dashboard for visualizing and exploring logs.
This setup will help you monitor container logs in real-time, enabling effective troubleshooting and system oversight.