Skip to main content
πŸŽ“ Claude Code Masterclass Learn AI-assisted development on Udemy β€” plus the companion book on Leanpub & Amazon. Start Learning
Grafana Loki: Cost-Effective Log Aggregation for Kubernetes
DevOps

Grafana Loki: Cost-Effective Log Aggregation for Kubernetes

Replace Elasticsearch with Loki for Kubernetes logging β€” 10x cheaper, label-based queries, LogQL, and seamless Grafana integration.

LB
Luca Berton
Β· 1 min read

Why Loki over Elasticsearch?

MetricElasticsearchLoki
Storage cost (1TB/day)~$3,000/mo~$300/mo
RAM required64GB+4-8GB
Index strategyFull-text (expensive)Labels only (cheap)
Query languageKQL/LuceneLogQL (PromQL-like)
Grafana integrationPluginNative (first-class)
Operational complexityHigh (shards, mappings)Low

Loki’s key insight: don’t index log content, only index metadata labels. Query by labels, grep for content.

Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Loki Cluster                       β”‚
β”‚                                                      β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚Distributorβ”‚  β”‚  Ingester β”‚  β”‚  Query Frontend  β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚        β”‚              β”‚                  β”‚           β”‚
β”‚        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜           β”‚
β”‚                       β”‚                              β”‚
β”‚              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”                     β”‚
β”‚              β”‚  Object Storage β”‚                     β”‚
β”‚              β”‚  (S3/MinIO/GCS) β”‚                     β”‚
β”‚              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β–²
         β”‚ Push logs
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Promtail /    β”‚  (DaemonSet on every node)
β”‚   Grafana Agent β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Installation

helm repo add grafana https://grafana.github.io/helm-charts

# Loki (Simple Scalable mode)
helm install loki grafana/loki \
  --namespace monitoring \
  --set loki.storage.type=s3 \
  --set loki.storage.s3.endpoint=minio.minio.svc:9000 \
  --set loki.storage.s3.bucketnames=loki-chunks \
  --set loki.storage.s3.access_key_id=minioadmin \
  --set loki.storage.s3.secret_access_key=minioadmin

# Promtail (log collector)
helm install promtail grafana/promtail \
  --namespace monitoring \
  --set config.clients[0].url=http://loki:3100/loki/api/v1/push

LogQL Queries

# All logs from payment service
{namespace="production", app="payment-service"}

# Filter for errors
{namespace="production", app="payment-service"} |= "error"

# Regex extract and filter
{namespace="production"} | regexp `status=(?P<status>\d+)` | status >= 500

# Count errors per minute
count_over_time({namespace="production"} |= "error" [1m])

# Top 10 error messages
topk(10, count by (msg)(
  {namespace="production"} | json | level="error"
))

# Latency percentiles from structured logs
quantile_over_time(0.95,
  {app="api-gateway"} | json | unwrap duration [5m]
) by (endpoint)

Structured Logging Best Practice

{"timestamp":"2026-06-05T07:00:00Z","level":"error","msg":"payment failed","service":"payment","user_id":"u123","amount":99.99,"error":"insufficient_funds","trace_id":"abc123"}
# Query structured logs efficiently
{app="payment-service"} | json | level="error" | error="insufficient_funds" | amount > 100

Retention and Cost

# Loki config
limits_config:
  retention_period: 30d        # Auto-delete after 30 days
  max_streams_per_user: 10000
  ingestion_rate_mb: 10
  ingestion_burst_size_mb: 20

compactor:
  retention_enabled: true
  delete_request_store: s3
RetentionDaily VolumeMonthly Storage Cost (S3)
7 days50GB/day~$8
30 days50GB/day~$35
90 days50GB/day~$100
365 days50GB/day~$400

Compare: Elasticsearch for the same volume would cost $3,000-10,000/month.

Alerting on Logs

# Loki ruler config
groups:
  - name: payment-alerts
    rules:
      - alert: HighErrorRate
        expr: |
          sum(count_over_time({app="payment-service"} |= "error" [5m])) > 50
        for: 5m
        labels:
          severity: critical
        annotations:
          summary: "Payment service error rate is high"

Free 30-min AI & Cloud consultation

Book Now