RousselTMLEARNING

Observabilité

Installer Grafana Loki (Docker Compose, binaire et Helm)

Installer et valider Grafana Loki : configuration minimale, Docker Compose avec Grafana et Alloy, installation du binaire, chart Helm, API de push, logcli et vérifications.

1. Une configuration minimale #

Monolithique avec stockage local.

loki.yaml #

# 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
À savoir : Le schema_config est immuable pour les données déjà écrites : pour migrer, on ajoute une nouvelle entrée avec une date from future.

2. Docker Compose : Loki, Grafana et Alloy #

Une stack de test complète.

docker-compose.yml #

Étapes d'installation
Chaque étape se valide avec une commande.
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. Valider avec l'API et logcli #

Envoyer et lire un log à la main.

Push et requête HTTP #

Vérifications de base de Loki
/ready doit répondre ready avant tout envoi.
# 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 : le client en ligne de commande #

# 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. Installation sur serveur et sur Kubernetes #

Binaire systemd et Helm.

Binaire et service systemd #

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
Erreur Courante : Le stockage filesystem ne convient pas à la production multi-réplicas : utilisez un stockage objet (S3, GCS, MinIO) dès que vous dépassez le test.

Pour aller plus loin