RousselTMLEARNING

Observability

Installing Grafana Loki (Docker Compose, binary and Helm)

Install and validate Grafana Loki: minimal configuration, Docker Compose with Grafana and Alloy, binary installation, Helm chart, push API, logcli and checks.

1. A minimal configuration #

Monolithic with local storage.

loki.yaml #

Available in: ✓ OSS ✓ Enterprise ✕ Cloud In Grafana Cloud the service is managed by Grafana Labs: nothing to install

# loki.yaml
auth_enabled: false

server:
  http_listen_port: 3100
  grpc_listen_port: 9096

common:
  instance_addr: 127.0.0.1
  path_prefix: /loki
  storage:
    filesystem:
      chunks_directory: /loki/chunks
      rules_directory: /loki/rules
  replication_factor: 1
  ring:
    kvstore:
      store: inmemory

schema_config:
  configs:
    - from: 2024-04-01
      store: tsdb
      object_store: filesystem
      schema: v13
      index:
        prefix: index_
        period: 24h

limits_config:
  retention_period: 168h          # 7 jours
  allow_structured_metadata: true

compactor:
  working_directory: /loki/retention
  retention_enabled: true
  delete_request_store: filesystem
Good to know: The schema_config is immutable for data already written: to migrate, you add a new entry with a future from date.

2. Docker Compose: Loki, Grafana and Alloy #

A complete test stack.

docker-compose.yml #

Installation steps
Each step is validated with a command.
services:
  loki:
    image: grafana/loki:latest
    command: -config.file=/etc/loki/loki.yaml
    ports: ["3100:3100"]
    volumes:
      - ./loki.yaml:/etc/loki/loki.yaml
      - loki-data:/loki

  alloy:
    image: grafana/alloy:latest
    ports: ["12345:12345"]
    volumes:
      - ./config.alloy:/etc/alloy/config.alloy
      - /var/lib/docker/containers:/var/lib/docker/containers:ro
      - /var/run/docker.sock:/var/run/docker.sock
    command: run --server.http.listen-addr=0.0.0.0:12345 /etc/alloy/config.alloy

  grafana:
    image: grafana/grafana:latest
    ports: ["3000:3000"]
    environment:
      GF_AUTH_ANONYMOUS_ENABLED: "true"
      GF_AUTH_ANONYMOUS_ORG_ROLE: Admin
    volumes:
      - ./datasource.yml:/etc/grafana/provisioning/datasources/loki.yml

volumes:
  loki-data: {}
# datasource.yml
apiVersion: 1
datasources:
  - name: Loki
    type: loki
    url: http://loki:3100
    isDefault: true
docker compose up -d
docker compose logs -f loki | head -30

3. Validating with the API and logcli #

Sending and reading a log by hand.

HTTP push and query #

Basic Loki checks
/ready must answer ready before sending anything.
# Envoyer un log (timestamp en nanosecondes)
curl -s -X POST http://localhost:3100/loki/api/v1/push \
  -H 'Content-Type: application/json' \
  -d "{\"streams\":[{\"stream\":{\"job\":\"test\",\"env\":\"lab\"},\"values\":[[\"$(date +%s%N)\",\"Bonjour Loki\"]]}]}"

# Relire le log
curl -G -s http://localhost:3100/loki/api/v1/query_range \
  --data-urlencode 'query={job="test"}' --data-urlencode 'limit=10' | jq '.data.result'

logcli: the command-line client #

# Installation (binaire ou via Homebrew : brew install logcli)
export LOKI_ADDR=http://localhost:3100
logcli labels
logcli labels job
logcli query '{job="test"}' --since=1h --limit=20
logcli query '{job="test"} |= "Loki"' --tail          # suivre en direct
logcli series '{job="test"}'
logcli stats '{job="test"}' --since=24h

4. Installing on a server and on Kubernetes #

systemd binary and Helm.

Binary and systemd service #

curl -LO https://github.com/grafana/loki/releases/latest/download/loki-linux-amd64.zip
unzip loki-linux-amd64.zip && sudo install loki-linux-amd64 /usr/local/bin/loki
sudo useradd --system --no-create-home loki
sudo mkdir -p /etc/loki /var/lib/loki && sudo chown loki: /var/lib/loki
loki -config.file=/etc/loki/loki.yaml -verify-config      # valider la configuration

sudo tee /etc/systemd/system/loki.service >/dev/null <<'EOF'
[Unit]
Description=Grafana Loki
After=network-online.target
[Service]
User=loki
ExecStart=/usr/local/bin/loki -config.file=/etc/loki/loki.yaml
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload && sudo systemctl enable --now loki

Helm #

helm repo add grafana https://grafana.github.io/helm-charts && helm repo update
# Mode monolithique pour un test (stockage filesystem)
cat > values.yaml <<'EOF'
deploymentMode: SingleBinary
loki:
  auth_enabled: false
  commonConfig: { replication_factor: 1 }
  storage: { type: filesystem }
  schemaConfig:
    configs:
      - from: "2024-04-01"
        store: tsdb
        object_store: filesystem
        schema: v13
        index: { prefix: loki_index_, period: 24h }
singleBinary: { replicas: 1 }
read: { replicas: 0 }
write: { replicas: 0 }
backend: { replicas: 0 }
EOF
helm install loki grafana/loki -n logging --create-namespace -f values.yaml
kubectl -n logging port-forward svc/loki 3100:3100
Common mistake: filesystem storage is not suitable for multi-replica production: use object storage (S3, GCS, MinIO) as soon as you go beyond testing.

Related articles

The LGTM stack (Loki, Grafana, Tempo, Mimir): architecture and setup

Understand Grafana Labs' LGTM stack: Loki for logs, Grafana for visualization, Tempo for traces and Mimir for…

Grafana Loki: the Prometheus-inspired log system

Discover Grafana Loki: the label-indexing philosophy, architecture (distributor, ingester, querier, compactor…

Collecting logs into Loki: Alloy, Promtail, Docker and OpenTelemetry

Send logs to Loki: Grafana Alloy, Promtail, the Docker driver, OTLP, good labels, parsing and enrichment, Kub…

Keep learning

Support us

♥ SUPPORT US

Support RousselTM Learning

Courses, articles, labs and certificates stay free with no intrusive ads. A voluntary contribution, of any amount you choose, pays for hosting, lab environments and new content.

Contribute with PayPal Any amount · card or PayPal accountLearn more
QR code PayPal
Or scan this QR code with your phone

Thank you, every contribution counts!