Home DevOpsCentralized Logging with Loki and Promtail: A Practical Setup Guide

Centralized Logging with Loki and Promtail: A Practical Setup Guide

by Knowledge Base Linux
Centralized Logging with Loki and Promtail — Centralized Logging with Loki and Promtail: A Practical Setup Guide

When an incident spans five servers, SSH-ing into each machine and grepping individual log files wastes the minutes you need most. Centralized logging ships every log line to one searchable place. This guide builds centralized logging with Loki, Grafana’s log aggregation system, using Promtail as the shipping agent and Docker Compose to run the stack.

Why Loki Instead of a Traditional Log Platform? – Centralized Logging with Loki and Promtail

Loki takes a different approach from full-text systems such as Elasticsearch: it indexes only the labels attached to each log stream (server, job, environment) and stores the raw lines compressed, unindexed. Consequences worth knowing:

  • Much lower cost: no giant full-text index to store and feed with RAM.
  • Native Grafana integration: logs sit next to your Prometheus metrics, and you can jump from a CPU spike to the exact log lines of that minute.
  • LogQL query language: intentionally similar to PromQL, so the skills transfer.
  • Trade-off: free-text search across unindexed content is slower than in Elasticsearch. For typical infrastructure troubleshooting, label-first queries are fast and sufficient.

Prerequisites – Centralized Logging with Loki and Promtail

  • A Linux server with Docker and Docker Compose installed. Follow our guide to install Docker on Ubuntu 24.04 if needed.
  • At least 2 GB of RAM for a small single-node setup.

Step 1: Define the Stack with Docker Compose

Create a project directory with a docker-compose.yml:

services:
  loki:
    image: grafana/loki:3.0.0
    container_name: loki
    ports:
      - "3100:3100"
    command: -config.file=/etc/loki/local-config.yaml
    restart: unless-stopped

  promtail:
    image: grafana/promtail:3.0.0
    container_name: promtail
    volumes:
      - /var/log:/var/log:ro
      - ./promtail-config.yml:/etc/promtail/config.yml
    command: -config.file=/etc/promtail/config.yml
    restart: unless-stopped

  grafana:
    image: grafana/grafana:latest
    container_name: grafana
    ports:
      - "3000:3000"
    restart: unless-stopped

Loki’s bundled local-config.yaml runs a complete single-node instance with filesystem storage, which is ideal for learning and small teams. Start the stack:

docker compose up -d
curl http://localhost:3100/ready

A ready response means Loki accepts pushes and queries, the heart of centralized logging with Loki.

Step 2: Configure Promtail to Ship Logs

Promtail tails files, attaches labels, and pushes lines to Loki. Create promtail-config.yml:

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:
          - localhost
        labels:
          job: varlogs
          host: server1
          __path__: /var/log/*.log

  - job_name: syslog
    static_configs:
      - targets:
          - localhost
        labels:
          job: syslog
          host: server1
          __path__: /var/log/syslog

Restart Promtail after editing (docker compose restart promtail). The positions file remembers how far each file has been read, so restarts do not duplicate logs. Labels are your index, so design them deliberately: host, job, and env are a solid starting set. Never use high-cardinality values such as user IDs or request IDs as labels; put those in the log line instead.

Centralized logging with Loki - LogQL search and filtering illustration

Step 3: Search Logs in Grafana with LogQL

Open Grafana at http://YOUR_SERVER_IP:3000 (default login admin/admin), add Loki as a data source with URL http://loki:3100, then open Explore, select Loki, and run:

{job="varlogs"}

Refine with line filters, which do not need the text index:

# Lines containing "error" on one host
{job="varlogs", host="server1"} |= "error"

# Lines NOT containing a noisy word
{job="syslog"} != "healthcheck"

# Count errors per minute as a metric-style query
rate({job="varlogs"} |= "error" [5m])

That last query shows off Loki’s party trick: the same stream selector powers both raw log browsing and rate graphs you can alert on.

Step 4: Add More Servers

On additional machines you only need Promtail (or Grafana Alloy, its successor agent) with the client URL pointed at your central Loki address, for example http://LOKI_SERVER_IP:3100/loki/api/v1/push, and a distinct host label. In production, put Loki behind a reverse proxy with authentication, because the API itself has no built-in login.

Troubleshooting Common Problems

No logs appear in Grafana

Check Promtail’s own logs (docker logs promtail) for push errors, confirm the Loki URL from inside the Promtail container, and verify the mounted /var/log path is readable. Also widen the Grafana time range; a wrong server clock hides fresh logs outside the visible window.

“Out of order” or rejected entries

Loki expects roughly chronological lines per stream. Clock skew on a shipping host causes rejections, so keep NTP synchronization enabled everywhere.

Loki disk usage grows too fast

Set a retention period in Loki’s limits_config (for example retention_period: 720h for 30 days) and enable the compactor’s retention. Compressed chunks are cheap, but they are not free.

Operating Loki Beyond the Tutorial

The single-node setup in this guide comfortably handles small and medium fleets, but plan for growth. Loki stores chunks in object storage (S3, GCS, or MinIO) in production deployments, which makes retention cheap and scaling the read and write paths independent. Enable the compactor with retention configured so old logs expire automatically instead of filling disks during an incident. If you run Grafana already, consider Grafana Alloy, the successor to Promtail, which adds richer pipelines and metrics about the agent itself; Promtail configurations translate almost directly. Keep an eye on label discipline as more teams ship logs: every new label value combination creates a new stream, and stream explosions are the most common cause of Loki slowdowns. A short internal guideline, listing approved labels such as env, host, job, and app, prevents most problems before they start.

Conclusion

Centralized logging with Loki gives you one searchable home for every server’s logs at a fraction of the cost of full-text platforms, with Promtail doing the shipping and Grafana Explore plus LogQL doing the searching. Pair it with the metrics stack from our Prometheus and Grafana monitoring guide and you can move from an alert to the relevant log lines in two clicks. For production sizing, retention, and multi-tenancy, continue with the Grafana Loki official documentation.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.