You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为Loki配置文件添加retention字段报错:field retention not found

Grafana Loki v2.8.0 配置日志保留周期报错解决

问题

用以下命令下载Loki v2.8.0的默认配置文件:

wget https://raw.githubusercontent.com/grafana/loki/v2.8.0/cmd/loki/loki-local-config.yaml -O loki-config.yaml

原本通过Docker命令能正常启动容器:

docker run --name loki -v :/mnt/config -p 3100:3100 grafana/loki:2.8.0 --config.file=/mnt/config/loki-config.yaml

但在schema_config.configs节点下添加retention字段设置30天日志保留后,启动报错:

failed parsing config: /mnt/config/loki-config.yaml: yaml: unmarshal errors:
line 35: field retention not found in type config.plain. Use -config.expand-env=true flag if you want to expand environment variables in your config file

原配置文件如下:

auth_enabled: false

server:
  http_listen_port: 3100
  grpc_listen_port: 9096

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

query_range:
  results_cache:
    cache:
      embedded_cache:
        enabled: true
        max_size_mb: 100

schema_config:
  configs:
    - from: 2022-01-01
      store: boltdb-shipper
      object_store: filesystem
      schema: v11
      index:
        prefix: index_
        period: 24h
      retention:
        enabled: true
        period: 30d

ruler:
  alertmanager_url: http://localhost:9093

原因

Loki v2.8.0的配置结构中,schema_config.configs节点下不存在retention字段,错误是因为配置项放错了位置。

解决方法

日志保留需要分索引文件和**日志块(chunks)**两部分配置:

1. 配置索引文件保留周期

在schema_config.configs节点下添加index_retention_period字段,设置索引的保留时长:

schema_config:
  configs:
    - from: 2022-01-01
      store: boltdb-shipper
      object_store: filesystem
      schema: v11
      index:
        prefix: index_
        period: 24h
      index_retention_period: 30d

2. 配置日志块保留周期

在配置文件根节点添加retention_config节点,设置日志块的保留时长:

retention_config:
  enabled: true
  period: 30d

修正后的完整配置

auth_enabled: false

server:
  http_listen_port: 3100
  grpc_listen_port: 9096

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

query_range:
  results_cache:
    cache:
      embedded_cache:
        enabled: true
        max_size_mb: 100

schema_config:
  configs:
    - from: 2022-01-01
      store: boltdb-shipper
      object_store: filesystem
      schema: v11
      index:
        prefix: index_
        period: 24h
      index_retention_period: 30d

retention_config:
  enabled: true
  period: 30d

ruler:
  alertmanager_url: http://localhost:9093

重新启动容器即可生效。

内容的提问来源于stack exchange,提问作者Short Linker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 07:17:16