今天测试环境loki的日志存太多发现磁盘已经不太够用了,然后去清理,发现还比较麻烦,因为不想直接删除所有的日志,所以还做了保留策略。
loki版本:
./loki-linux-amd64 --version
loki, version 3.5.2 (branch: release-3.5.x, revision: 257d2f62)
build user: root@67bdab5ad0d6
build date: 2025-07-10T19:26:46Z
go version: go1.24.1
platform: linux/amd64
tags: netgo到今天为止,最新版本是3.5.8应该不会相差很多。
实操阶段:
1、 修改配置文件:
主配置文件里添加带注释部分都是新加的项。
compactor:
working_directory: /usr/local/loki/compactor
compaction_interval: 10m0s
apply_retention_interval: 15m0s # 每15分钟处理一次删除请求
retention_enabled: true # 启用保留功能
retention_delete_delay: 2m0s # 删除延迟2分钟
retention_delete_worker_count: 150 # 删除工作线程数
delete_request_store: filesystem # 删除请求存储类型
delete_request_cancel_period: 1m0s # 🚨 关键:取消期1分钟(默认24小时太长!)
limits_config:
metric_aggregation_enabled: true
reject_old_samples_max_age: 168h
max_streams_per_user: 1000000
ingestion_rate_mb: 100
max_query_length: 72h
reject_old_samples: true
retention_period: 360h
allow_deletes: true # 允许删除
deletion_mode: filter-and-delete # 删除索引并清理文件。
table_manager:
retention_deletes_enabled: true # 允许自动清理
retention_period: 360h # 保留策略2、 重启loki
sudo systemctl restart loki
sudo systemctl status loki
loki.service - Loki Server Service daemon
Loaded: loaded (/usr/lib/systemd/system/loki.service; enabled; preset: ena>
Active: active (running) since Wed 2025-11-12 09:45:10 UTC; 16h ago
Main PID: 2005088 (loki-linux-amd6)
Tasks: 48 (limit: 9501)
Memory: 1.2G (peak: 1.3G)
CPU: 13min 33.663s
CGroup: /system.slice/loki.service
└─2005088 /usr/local/loki/loki-linux-amd64 --config.file=/usr/loca>
Nov 13 02:40:10 ip-172-31-38-51 loki-linux-amd64[2005088]: level=info ts=2025-1>
Nov 13 02:40:10 ip-172-31-38-51 loki-linux-amd64[2005088]: level=info ts=2025-1>
Nov 13 02:40:10 ip-172-31-38-51 loki-linux-amd64[2005088]: level=info ts=2025-1>
Nov 13 02:40:10 ip-172-31-38-51 loki-linux-amd64[2005088]: level=info ts=2025-1>
Nov 13 02:40:10 ip-172-31-38-51 loki-linux-amd64[2005088]: level=info ts=2025-1>
Nov 13 02:40:10 ip-172-31-38-51 loki-linux-amd64[2005088]: level=info ts=2025-1>
Nov 13 02:40:11 ip-172-31-38-51 loki-linux-amd64[2005088]: level=info ts=2025-1>
Nov 13 02:40:11 ip-172-31-38-51 loki-linux-amd64[2005088]: level=info ts=2025-1>
Nov 13 02:40:11 ip-172-31-38-51 loki-linux-amd64[2005088]: level=info ts=2025-1>
Nov 13 02:40:11 ip-172-31-38-51 loki-linux-amd64[2005088]: level=info ts=2025-1>如果出现配置错误,请参照日志排查,直接使用我的配置是没有问题的。
日志查看:
sudo journalctl -u loki -f #或者指定时间 sudo journalctl -u loki --since "5 min ago" #查看5分钟内的日志。
3、 手动使用api触发清理策略
我不知道如果是自动清理,它会在什么时间触发,虽然配置文件里面已经配置了相差的参数。所以我选择手动触发清理策略。
curl -g -X POST \
"http://localhost:3100/loki/api/v1/delete?query={host=\"test\"}&start=1760349967&end=1761645967" \
-H "X-Scope-OrgID: authuser"这个格式是在官方文档里面找到的:
1. 命令各部分解析
curl- 命令行工具,用于传输数据
-g- 禁用 URL 转义
作用:允许在 URL 中使用特殊字符
{}和"没有
-g时:{host=\"test\"}会被转义,导致查询失败有
-g时:保持原样发送
-X POST- 指定 HTTP 方法为 POSTURL 部分:
text
http://localhost:3100/loki/api/v1/delete?query={host=\"test\"}&start=1760349967&end=1761645967查询参数:
query={host=\"test\"}- 删除条件:匹配 host 标签为 "test" 的日志
start=1760349967- 开始时间戳(对应:2024-10-13 13:06:07)
end=1761645967- 结束时间戳(对应:2024-11-12 13:06:07)
-H "X-Scope-OrgID:authuser"- 设置 HTTP 头
指定租户 ID 为 "authuser"
Loki 多租户环境下必需
2. 时间范围分析
bash
# 转换时间戳为可读格式 echo "开始时间: $(date -d @1760349967)" # 2024-10-13 13:06:07 echo "结束时间: $(date -d @1761645967)" # 2024-11-12 13:06:07 echo "时间跨度: 30天"删除范围: 2024年10月13日 到 2024年11月12日(30天)的所有 test-coplay 主机日志
4、 验证清理状态:
当上在的curl命令结束后,loki会将清理策略添加到delete的队列里面。使用api命令可以看到状态:
curl -s -H "X-Scope-OrgID: authuser" "http://localhost:3100/loki/api/v1/delete"
[{"request_id":"a7a6a987","start_time":1761557463,"end_time":1761643863,"query":"{host=\"test\"}","status":"received","created_at":1762940068.508},
{"request_id":"c0e7f6cc","start_time":1761557463,"end_time":1761643863,"query":"{host=\"pre\"}","status":"received","created_at":1762940212.394},
{"request_id":"d7f9f8e8","start_time":1761557463,"end_time":1761643863,"query":"{host=\"prod\"}","status":"received","created_at":1762940230.444},可以看到目前的状态为“received” 因为配置里面有一个取消的策略, delete_request_cancel_period: 1m0s 等这个时间达到,它就会开始清理,但是在我实际等待中感觉时间要大于这个时间。它有个默认时间是24h,所以这个参数一定要配置上去。
在这之间,你可以去查看loki的日志去查看有没有正常处理你的删除策略:
sudo journalctl -u loki -f | grep -i compactor #正常你会看到: Nov 12 09:55:16 ip-172-31-38-51 loki-linux-amd64[2005088]: level=info ts=2025-11-12T09:55:15.908941509Z caller=compactor.go:556 msg="compactor startup delay completed" Nov 12 09:55:16 ip-172-31-38-51 loki-linux-amd64[2005088]: level=info ts=2025-11-12T09:55:15.909851094Z caller=compactor.go:798 msg="compacting table" table-name=index_20404 Nov 12 09:55:16 ip-172-31-38-51 loki-linux-amd64[2005088]: level=info ts=2025-11-12T09:55:15.920319815Z caller=util.go:77 table-name=index_20404 user-id=coplayuser user-id=coplayuser file-name=1762940303809363807-compactor-1762899079286-1762939800620-e55cfc0f.tsdb.gz msg="downloaded file" total_time=121.626µs Nov 12 09:55:16 ip-172-31-38-51 loki-linux-amd64[2005088]: level=info ts=2025-11-12T09:55:15.920731066Z caller=util.go:124 table-name=index_20404 user-id=coplayuser user-id=coplayuser file-name=1762940303809363807-compactor-1762899079286-1762939800620-e55cfc0f.tsdb.gz size="21 kB" msg="downloaded and extracted file" downloadtime=121.626µs extracttime=365.174µs Nov 12 09:55:16 ip-172-31-38-51 loki-linux-amd64[2005088]: level=info ts=2025-11-12T09:55:15.950548747Z caller=compactor.go:803 msg="finished compacting table" table-name=index_20404 Nov 12 09:55:16 ip-172-31-38-51 loki-linux-amd64[2005088]: level=info ts=2025-11-12T09:55:15.950575542Z caller=compactor.go:798 msg="compacting table" table-name=index_20403 ... ...
多等一会,我就发现磁盘空间告警恢复了。然后再去看删除状态:
curl -s -H "X-Scope-OrgID: authuser" "http://localhost:3100/loki/api/v1/delete"
[{"request_id":"a7a6a987","start_time":1761557463,"end_time":1761643863,"query":"{host=\"test\"}","status":"processed","created_at":1762940068.508},
{"request_id":"c0e7f6cc","start_time":1761557463,"end_time":1761643863,"query":"{host=\"pre\"}","status":"processed","created_at":1762940212.394},
{"request_id":"d7f9f8e8","start_time":1761557463,"end_time":1761643863,"query":"{host=\"prod\"}","status":"processed","created_at":1762940230.444},已经看到删除状态已经从received变成了processed
这意味着:
✅ 删除请求已成功处理完成!
最后完整配置文件(备忘)
loki-local-config.yaml
auth_enabled: true #开启多租户认证
server:
http_listen_port: 3100
grpc_listen_port: 9096
log_level: info
grpc_server_max_concurrent_streams: 1000
common:
instance_addr: localhost #本机ip地址
path_prefix: /usr/local/loki
storage:
filesystem:
chunks_directory: /usr/local/loki/chunks
rules_directory: /usr/local/loki/rules/persistent_rules
replication_factor: 1
ring:
kvstore:
store: inmemory
query_range:
results_cache:
cache:
embedded_cache:
enabled: true
max_size_mb: 100
limits_config:
metric_aggregation_enabled: true
reject_old_samples_max_age: 168h
max_streams_per_user: 1000000
ingestion_rate_mb: 100
max_query_length: 72h
reject_old_samples: true
retention_period: 360h
allow_deletes: true
deletion_mode: filter-and-delete
schema_config:
configs:
- from: 2020-10-24
store: tsdb
object_store: filesystem
schema: v13
index:
prefix: index_
period: 24h
storage_config:
tsdb_shipper:
active_index_directory: /usr/local/loki/tsdb-index
cache_location: /usr/local/loki/tsdb-cache
table_manager:
retention_deletes_enabled: true
retention_period: 360h
compactor:
working_directory: /usr/local/loki/compactor
retention_enabled: true
retention_delete_delay: 120s
retention_delete_worker_count: 150
delete_request_store: filesystem
delete_request_cancel_period: 60s
ruler:
alertmanager_url: http://localhost:9093 #告警配置
rule_path: /usr/local/loki/rules/runtime_rules
storage:
type: local
local:
directory: /usr/local/loki/rules/persistent_rules
ring:
kvstore:
store: inmemory
frontend:
encoding: protobuf
评论区