为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=trueflag 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
相关产品推荐
相关产品推荐

