如何控制 Prometheus 保留记录的时长?
prometheus 共有三个标志:
- –storage.tsdb.retention.size:要保留的存储块的最大字节数。 最旧的数据将首先被删除。 默认为 0 或禁用。 支持的单位:B、KB、MB、GB、TB、PB、EB。 例如:“512MB”
- –storage.tsdb.retention.time:配置何时删除旧数据。 默认为 15 天。 如果此标志设置为默认值以外的任何值,则覆盖 storage.tsdb.retention。
- –storage.tsdb.retention:已弃用,由 –storage.tsdb.retention.time 取代。
下表总结了它们如何协同工作:
–storage.tsdb.retention.size | –storage.tsdb.retention.time | –storage.tsdb.retention | Result |
---|---|---|---|
不设置 | 不设置 | 不设置 | 应用默认15d保留 |
不设置 | 20d | 不设置 | 20天保留 |
不设置 | 不设置 | 10d | 10天保留 |
不设置 | 20d | 10d | 20天保留 |
1TB | 不设置 | 不设置 | 1TB 大小保留,没有时间限制 |
1TB | 20d | 不设置 | 1TB 大小和 20d 时间保留 - 以先达到限制为准 |
1TB | 20d | 10d | 1TB 大小和 20d 时间保留 - 以先达到限制为准 |
可以看到,--storage.tsdb.retention.time
覆盖了已弃用的 --storage.tsdb.retention
,并且基于时间和大小的保留可以立即生效。
修改方式
下面提供了两种部署方式的修改方式
prometheus
采用prometheus直接部署的,在启动添加对应参数即可。
prometheus-operate
采用prometheus-operate方式部署的,会有一个类型为Prometheus
的crd资源,可以查看Prometheus定义文件:
关于retention描述的描述:
1 | Time duration Prometheus shall retain data for. Default is '24h', and must match the regular expression `[0-9]+(ms|s|m|h|d|w|y)` (milliseconds seconds minutes hours days weeks years). |
故可以修改Prometheus资源添加retention
字段:
1 | apiVersion: monitoring.coreos.com/v1 |