Closed8

Lokiのログ永続化をMinIOに設定する

なおきなおき

Loki.yamlのストレージ設定をローカルからs3の設定にする必要がある。なぜS3かというと、minIOはs3と互換があるためs3を指定しても問題ない。

ログの保存先がローカル

storage_config:
  filesystem:
    directory: /tmp/loki/chunks

ログの保存先がminIO

storage_config:
 tsdb_shipper:
   active_index_directory: /loki/index
   cache_location: /loki/index_cache
 aws:
   s3: s3://access_key:secret_access_key@custom_endpoint/bucket_name
   s3forcepathstyle: true

storage_configaはTSDBが推奨。BoltDBは非推奨。
https://grafana.com/docs/loki/latest/configure/storage/

なおきなおき

以下のコマンドで、MinIO コンソールにアクセスキー/シークレットキーでログインとバケット作成をしてみる。

mc alias set local http://localhost:9000 minio supersecret
mc mb local/loki

http://127.0.0.1:9001/にアクセスしてバケットを確認すると、lokiというバケットが作成された!!

なおきなおき

なぜログデータが保存されていない?

  • loki.yamlの設定が不足
  • docker-compose.yaml内のminioの設定が不足

loki.yaml

docker-compose.yaml

  • lokiへのボリューム設定が不足していた?

どれを試しても未解決

なおきなおき

Dockerのコンテナ内のログを確認

level=info ts=2025-05-08T02:13:18.3601275Z caller=flush.go:304 component=ingester msg="flushing stream" user=fake fp=0b221e1c500a4ea5 immediate=false num_chunks=1 total_comp="635 B" avg_comp="635 B" total_uncomp="1.1 kB" avg_uncomp="1.1 kB" full=1 labels="{service_name=\"unknown_service:node\"}"
2025-05-08 11:13:18 level=error ts=2025-05-08T02:13:18.360804053Z caller=flush.go:261 component=ingester loop=5 org_id=fake msg="failed to flush" retries=8 err="failed to flush chunks: store put chunk: MissingEndpoint: 'Endpoint' configuration is required for this service, num_chunks: 1, labels: {service_name=\"unknown_service:node\"}"
2025-05-08 11:13:20 level=info ts=2025-05-08T02:13:20.603151047Z caller=recalculate_owned_streams.go:49 msg="starting recalculate owned streams job"
2025-05-08 11:13:20 level=info ts=2025-05-08T02:13:20.603189208Z caller=recalculate_owned_streams.go:52 msg="completed recalculate owned streams job"

エラーメッセージによると必須なエンドポイントの設定がないと言われたため、endpoint追加

storage_config:
  tsdb_shipper:
    active_index_directory: /loki/index
    cache_location: /loki/index_cache
  aws:
+   endpoint: http://minio:supersecret@minio:9000
    s3: http://minio:supersecret@minio:9000/loki
    s3forcepathstyle: true
    insecure: true
なおきなおき

コンテナを再起動してminioの画面を確認するとfakeフォルダとindexフォルダが作成されていることが確認できる!

このfakeフォルダ内にログファイルが保存される。
Lokiは設定された間隔でログをメモリに保持し、オブジェクトストレージに書き込まれる。(デフォルトは5分)

なおきなおき

おまけ

  • コマンドでバケットのポリシーを変更する場合のコマンド
mc anonymous set public local/loki

https://min.io/docs/minio/linux/reference/minio-mc/mc-anonymous-set.html?utm_source=chatgpt.com#syntax

  • 結果:ボリュームの設定は必要だった

docker-compose.yaml
lokiへのボリューム設定が不足していた?

Lokiのボリュームを設定しない場合、minIOにログデータが保存されなかった。おそらく、minIOに書き込む前の一時的なログ保管領域として使用しているのだろう。

このスクラップは4ヶ月前にクローズされました