Prometheus Like なログ収集ツール Loki を試してみた。

こんにちは k-jun です。今回は ログ収集ツール Loki を試してみます。

https://github.com/grafana/loki

見る限りは、

  • Prometheus が メトリクスを収集することに対して、Loki はログを収集するツールである。
  • Prometheus が Pull ベースの収集機構を持つのに対して、Loki は Push ベースの収集機構を持つ。
  • Kubernetes のログ収集によくマッチし、Grafana の Native レベルのサポートをしている。

ぐらいでしょうか。

Loki は Prometheus と同様に、ログの収集基盤であり、ログの転送は promtail と呼ばれるものを利用して行うようです。

Docker でも提供されているみたいなので、サクッと手元で試してみます。

# loki
$ wget https://raw.githubusercontent.com/grafana/loki/v2.3.0/cmd/loki/loki-local-config.yaml -O /tmp/loki-config.yaml
$ docker run -v /tmp/loki-config.yaml:/mnt/config/loki-config.yaml -p 3100:3100 grafana/loki:2.3.0 -config.file=/mnt/config/loki-config.yaml
# promtail
$ wget https://raw.githubusercontent.com/grafana/loki/v2.3.0/clients/cmd/promtail/promtail-docker-config.yaml -O /tmp/promtail-config.yaml
$ docker run -v /tmp/promtail-config.yaml:/mnt/config/promtail-config.yaml -v /var/log:/var/log grafana/promtail:2.3.0 -config.file=/mnt/config/promtail-config.yaml

動くことは動きますね。wget してきた Config の中身を見てみます。

# loki-config.yaml
auth_enabled: false

server:
  http_listen_port: 3100
  grpc_listen_port: 9096

ingester:
  wal:
    enabled: true
    dir: /tmp/wal
  lifecycler:
    address: 127.0.0.1
    ring:
      kvstore:
        store: inmemory
      replication_factor: 1
    final_sleep: 0s
  chunk_idle_period: 1h       # Any chunk not receiving new logs in this time will be flushed
  max_chunk_age: 1h           # All chunks will be flushed when they hit this age, default is 1h
  chunk_target_size: 1048576  # Loki will attempt to build chunks up to 1.5MB, flushing first if chunk_idle_period or max_chunk_age is reached first
  chunk_retain_period: 30s    # Must be greater than index read cache TTL if using an index cache (Default index read cache TTL is 5m)
  max_transfer_retries: 0     # Chunk transfers disabled

schema_config:
  configs:
    - from: 2020-10-24
      store: boltdb-shipper
      object_store: filesystem
      schema: v11
      index:
        prefix: index_
        period: 24h

storage_config:
  boltdb_shipper:
    active_index_directory: /tmp/loki/boltdb-shipper-active
    cache_location: /tmp/loki/boltdb-shipper-cache
    cache_ttl: 24h         # Can be increased for faster performance over longer query periods, uses more disk space
    shared_store: filesystem
  filesystem:
    directory: /tmp/loki/chunks

compactor:
  working_directory: /tmp/loki/boltdb-shipper-compactor
  shared_store: filesystem

limits_config:
  reject_old_samples: true
  reject_old_samples_max_age: 168h

chunk_store_config:
  max_look_back_period: 0s

table_manager:
  retention_deletes_enabled: false
  retention_period: 0s

ruler:
  storage:
    type: local
    local:
      directory: /tmp/loki/rules
  rule_path: /tmp/loki/rules-temp
  alertmanager_url: http://localhost:9093
  ring:
    kvstore:
      store: inmemory
  enable_api: true
# 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

なるほど。なんとなく /var/log/*log にて生成されているログを Loki に送っていそうですね。 Loki の方も /metrics でログの情報を提供しているみたいなので、一応見てみます。

$ curl http://localhost:3100/metrics | head
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0# HELP cortex_cache_corrupt_chunks_total Total count of corrupt chunks found in cache.
# TYPE cortex_cache_corrupt_chunks_total counter
cortex_cache_corrupt_chunks_total 0
# HELP cortex_chunk_store_chunks_per_query Distribution of #chunks per query.
# TYPE cortex_chunk_store_chunks_per_query histogram
cortex_chunk_store_chunks_per_query_bucket{le="10"} 0
cortex_chunk_store_chunks_per_query_bucket{le="80"} 0
cortex_chunk_store_chunks_per_query_bucket{le="640"} 0
cortex_chunk_store_chunks_per_query_bucket{le="5120"} 0
cortex_chunk_store_chunks_per_query_bucket{le="40960"} 0
100 44764    0 44764    0     0  4371k      0 --:--:-- --:--:-- --:--:-- 4371k
curl: (23) Failed writing body (0 != 2048)

なんか取れることは取れますね 笑。ではでは、Nginx と組み合わせてみて、Nginx のログを Loki に送信。 それを Grafana で閲覧して見るところまでやってみます。

docker-compose.yml を用意。

version: '3'
services:
  nginx:
    image: nginx
    ports:
      - "8080:80"
    volumes:
      - /tmp/access.log:/var/log/nginx/access.log
  primtail:
    image: grafana/promtail:2.3.0 
    command: -config.file=/mnt/config/promtail-config.yaml
    volumes:
      - /tmp/promtail-config.yaml:/mnt/config/promtail-config.yaml
      - /tmp/access.log:/var/log/nginx/access.log
  loki:
    image: grafana/loki:2.3.0 
    ports:
      - 3100:3100
    command: -config.file=/mnt/config/loki-config.yaml
    volumes:
      - /tmp/loki-config.yaml:/mnt/config/loki-config.yaml
  grafana:
    image: grafana/grafana
    ports:
      - 3000:3000

Nginx のログを生成するべく適当に watch で 継続にアクセスさせます。

$ watch curl localhost:8080

Grafana で Data Source の設定を済ませて、Explore から検索をしてみると。

f:id:K-jun1221:20210826025022p:plain

Nginx のログが見れました!結構いい感じでは無いでしょうか...? Grafana 公式が出しているという点や Livemode をサポートしている点など。 Watch する価値のある面白そうなプロジェクトだと思います。

それでは今日はこのへんで。