用途
loki-config-generator 用于生成 Grafana Loki 日志系统配置,目标产出是“生产可用”的 Loki 服务端配置。资料说明其调用场景包括:部署 Loki、从零创建配置、迁移到 Loki、实现多租户日志、配置存储后端,以及优化既有部署。
主要能力
- 支持三种部署模式:monolithic(单体,适用于 <100GB/天 的测试与开发)、simple-scalable(简单可扩展,适用于 100GB-1TB/天 的生产场景)、microservices(微服务,适用于 >1TB/天 的大规模多租户场景)。
- 存储后端支持 S3、GCS、Azure Blob、Filesystem、MinIO。
- 生成方式有两种。推荐方式是脚本生成:使用 scripts/generate_config.py,可通过参数指定 mode、storage、bucket、region、retention-days,并可启用 OTLP、Thanos 存储、时间分片、ruler、水平 compactor、zone awareness、limits dry run 等。另一种是手动配置流程。
- 提供分阶段工作流:收集需求、配置 Schema、配置存储、配置组件(ingester、querier、compactor)、配置 limits、配置 server 与 auth、配置 OTLP 接入、配置缓存、高级特性、ruler、Loki 3.6 特性、校验配置。
- 对 Kubernetes 部署,建议同时生成原生 Loki 配置(loki-config.yaml,用于 ConfigMap 或直接使用)与 Helm values(values.yaml)。
- 资料列出了完整示例文件,包括 monolithic-filesystem.yaml、simple-scalable-s3.yaml、microservices-s3.yaml、multi-tenant.yaml、production-tls.yaml、grafana-alloy.alloy、grafana-alloy-daemonset.yaml、kubernetes-helm-values.yaml。
使用方式
脚本生成命令示例(资料原文):
python3 scripts/generate_config.py \
--mode simple-scalable \
--storage s3 \
--bucket my-loki-bucket \
--region us-east-1 \
--retention-days 30 \
--otlp-enabled \
--output loki-config.yaml
python3 scripts/generate_config.py \
--mode monolithic \
--storage filesystem \
--no-auth-enabled \
--output loki-dev.yaml
python3 scripts/generate_config.py \
--mode simple-scalable \
--storage s3 \
--thanos-storage \
--otlp-enabled \
--time-sharding \
--output loki-thanos.yaml
脚本选项包括:--mode(monolithic、simple-scalable、microservices)、--storage(filesystem、s3、gcs、azure)、--auth-enabled / --no-auth-enabled、--otlp-enabled、--thanos-storage(3.4+,云后端)、--time-sharding(simple-scalable 的乱序写入)、--ruler(非 monolithic)、--horizontal-compactor(simple-scalable,3.6+)、--zone-awareness、--limits-dry-run。
配置校验命令(资料原文):
loki -config.file=loki-config.yaml -verify-config
loki -config.file=loki-config.yaml -print-config-stderr 2>&1 | head -100
docker run --rm -v $(pwd)/loki-config.yaml:/etc/loki/config.yaml \
grafana/loki:3.6.2 -config.file=/etc/loki/config.yaml -verify-config
Helm 部署命令(资料原文):
helm repo add grafana https://grafana.github.io/helm-charts
helm install loki grafana/loki -f values.yaml
关键配置要点
- Schema:Loki 2.9+ 新部署使用 TSDB 与 v13 schema,且 schema 部署后不经迁移不能更改。
- 生产要求:replication_factor 为 3;多可用区部署必须开启 zone_awareness_enabled;多租户需 auth_enabled: true;生产需启用 TLS;云存储应使用 IAM 角色而非硬编码密钥。
- OTLP 接入(Loki 3.0+):使用 otlphttp exporter,不使用已弃用的 lokiexporter;低基数属性用 index_label,高基数属性(如 k8s.pod.name)用 structured_metadata,资料明确不应将 k8s.pod.name 作为 index_label。
- 日志采集:Promtail 在 3.4 已弃用(支持到 2026 年 2 月),新部署应使用 Grafana Alloy;资料提供了 Alloy 配置示例与从 Promtail 迁移的转换命令:
alloy convert --source-format=promtail --output=alloy-config.alloy promtail.yaml
- 版本特性:Pattern Ingester(3.0+)、Bloom Filters(实验性,3.3+,仅用于 >75TB/月 且仅作用于 structured metadata)、Time Sharding(3.4+)、Thanos Storage(3.4+,当前为可选、后续默认)、水平扩展 compactor 与策略化 enforced_labels(3.6)。
适用场景与限制
适用于部署或迁移 Grafana Loki、构建多租户日志、配置各类对象存储后端以及优化现有 Loki 部署。需要注意的限制与前提:schema 不可在部署后随意变更;Bloom Filters 为实验性且面向超大流量;horizontal-compactor、time sharding、Thanosh 存储等属于特定版本特性;Promtail 已弃用需改用 Alloy。关于该技能的收费、价格、平台与具体运行效果,资料中暂未提供。