Loki Environment Setup
Introduction
Grafana Loki is a horizontally scalable, highly available log aggregation system designed to be cost-effective and easy to operate. It's often referred to as "Prometheus, but for logs," as it uses the same service discovery and label-based approach that made Prometheus popular for metrics.
In this tutorial, we'll walk through the process of setting up a Loki environment for local development and testing. By the end, you'll have a functional Loki setup that can collect, query, and visualize logs.
Prerequisites
Before proceeding, ensure you have the following installed:
- Docker and Docker Compose
- Git
- A terminal or command-line interface
- Basic knowledge of containerization concepts
Setting Up Loki: Options Overview
There are several ways to set up Loki:
- Docker: The simplest approach for local development
- Kubernetes with Helm: For production environments
- Binary installation: For custom deployments
- Grafana Cloud: Managed service option
We'll focus primarily on the Docker approach, which is ideal for beginners and local development.
Local Loki Setup Using Docker Compose
Let's set up a complete logging stack with Docker Compose, including:
- Loki (log aggregation)
- Promtail (log collector)
- Grafana (visualization)
Step 1: Create a Project Directory
First, create a directory for your Loki project:
mkdir loki-setup
cd loki-setup
Step 2: Create a Docker Compose Configuration
Create a file named docker-compose.yml
with the following content:
version: "3"
services:
loki:
image: grafana/loki:2.9.0
ports:
- "3100:3100"
volumes:
- ./loki-config.yaml:/etc/loki/local-config.yaml
command: -config.file=/etc/loki/local-config.yaml
promtail:
image: grafana/promtail:2.9.0
volumes:
- ./promtail-config.yaml:/etc/promtail/config.yaml
- /var/log:/var/log
command: -config.file=/etc/promtail/config.yaml
grafana:
image: grafana/grafana:10.0.0
ports:
- "3000:3000"
environment:
- GF_AUTH_ANONYMOUS_ENABLED=true
- GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
volumes:
- ./grafana-datasources.yaml:/etc/grafana/provisioning/datasources/datasources.yaml
Step 3: Configure Loki
Create a file named loki-config.yaml
with the following content:
auth_enabled: false
server:
http_listen_port: 3100
grpc_listen_port: 9096
common:
path_prefix: /tmp/loki
storage:
filesystem:
chunks_directory: /tmp/loki/chunks
rules_directory: /tmp/loki/rules
replication_factor: 1
ring:
instance_addr: 127.0.0.1
kvstore:
store: inmemory
schema_config:
configs:
- from: 2020-10-24
store: boltdb-shipper
object_store: filesystem
schema: v11
index:
prefix: index_
period: 24h
ruler:
alertmanager_url: http://localhost:9093
limits_config:
enforce_metric_name: false
reject_old_samples: true
reject_old_samples_max_age: 168h
Step 4: Configure Promtail
Create a file named promtail-config.yaml
:
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
__path__: /var/log/*log
- job_name: docker
static_configs:
- targets:
- localhost
labels:
job: docker
__path__: /var/lib/docker/containers/*/*log
Step 5: Configure Grafana Data Source
Create a file named grafana-datasources.yaml
:
apiVersion: 1
datasources:
- name: Loki
type: loki
access: proxy
url: http://loki:3100
isDefault: true
Step 6: Start the Stack
Launch the entire logging stack with:
docker-compose up -d
You should see the containers starting:
Creating loki-setup_loki_1 ... done
Creating loki-setup_promtail_1 ... done
Creating loki-setup_grafana_1 ... done
Step 7: Access Grafana
Open your browser and navigate to:
http://localhost:3000
You'll be automatically logged in as an admin (based on our configuration).
Verifying Your Loki Setup
Let's verify that everything is working correctly:
- In Grafana, go to "Explore" (compass icon in the left sidebar)
- Ensure "Loki" is selected as the data source
- Enter a simple LogQL query:
{job="varlogs"}
- Click "Run Query"
You should see logs from your system appearing in the results panel.
Alternative Setup: Using Tanka and Jsonnet
For more advanced users or production deployments, Grafana Labs recommends using Tanka and Jsonnet. Here's a brief overview:
- Install Tanka and Jsonnet:
go install github.com/grafana/tanka/cmd/tk@latest
go install github.com/jsonnet-bundler/jsonnet-bundler/cmd/jb@latest
- Create a new project:
mkdir loki-production && cd loki-production
tk init
- Install Loki Jsonnet libraries:
jb install github.com/grafana/loki/production/ksonnet/loki
- Create environments and configure as needed.
This approach offers more flexibility but is more complex for beginners.
Loki Architecture Overview
Understanding Loki's architecture helps visualize how logs flow through the system:
- Log Sources: Your applications, services, and system logs
- Promtail: Collects and labels logs before sending to Loki
- Loki Distributor: Receives and validates incoming logs
- Loki Ingester: Compresses logs into chunks and stores them
- Object Storage: Where log data is persisted (local filesystem in our setup)
- Loki Querier: Processes log queries from Grafana
- Grafana: Visualizes and explores logs
Basic Loki Configuration
Understanding the key components of Loki's configuration file helps with customization:
Component | Description |
---|---|
auth_enabled | Toggle authentication |
server | HTTP and gRPC API settings |
common | Shared settings across components |
schema_config | How logs are stored and indexed |
limits_config | Resource usage limitations |
ruler | Alert and rule management |
Troubleshooting Common Issues
Loki Container Not Starting
Check if port 3100 is already in use:
lsof -i :3100
If it shows existing processes, kill them or change Loki's port in the docker-compose.yml.
No Logs Appearing in Grafana
- Check if Promtail is running:
docker-compose ps promtail
- Verify Promtail's configuration targets valid log paths
- Check Loki logs for errors:
docker-compose logs loki
High Memory Usage
Loki can be memory-intensive. Adjust the limits_config
in your Loki configuration:
limits_config:
ingestion_rate_mb: 4
ingestion_burst_size_mb: 6
max_global_streams_per_user: 5000
Best Practices for Development
- Label Efficiently: Too many label values can degrade performance
- Use Dynamic Labels: Label with service names, not unique identifiers
- Structured Logging: Use JSON logging in applications for better querying
- Regular Pruning: Configure retention periods for logs
- Monitor Loki Itself: Set up metrics to watch Loki's performance
Summary
In this tutorial, you've learned how to:
- Set up Loki, Promtail, and Grafana using Docker Compose
- Configure each component of the logging stack
- Verify the setup is working
- Understand Loki's architecture
- Troubleshoot common issues
With this foundation, you're ready to start collecting and analyzing logs from your applications and systems.
Next Steps
- Explore LogQL, Loki's query language
- Configure Promtail to scrape logs from your applications
- Set up alerts for specific log patterns
- Integrate Loki with other observability tools
Additional Resources
If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)