Prometheus 存储优化和性能调优策略:
数据保留策略:
yamlstorage: tsdb: retention.time: 15d retention.size: 10GB
- 根据磁盘空间和查询需求设置保留时间
- 使用
retention.size限制磁盘使用
采集优化:
- 合理设置
scrape_interval(推荐 15s-60s) - 使用
scrape_timeout避免慢查询 - 对不重要的指标设置更长的采集间隔
- 使用
metric_relabel_configs过滤不需要的指标
查询优化:
- 避免全量查询,使用标签过滤
- 合理选择时间窗口大小
- 使用 Recording Rules 预计算常用查询
- 分散查询时间,避免高峰期
内存优化:
- 调整
--storage.tsdb.retention.time - 使用
--storage.tsdb.head-chunks.write-queue-size控制写入队列 - 监控内存使用,及时清理旧数据
- 考虑使用 Thanos 或 VictoriaMetrics 进行长期存储
Recording Rules 示例:
yamlgroups: - name: api_rules rules: - record: job:http_requests:rate5m expr: sum by (job) (rate(http_requests_total[5m]))
监控 Prometheus 自身:
prometheus_tsdb_compaction_durationprometheus_tsdb_head_samples_appended_totalprometheus_target_interval_length_seconds
最佳实践:
- 定期清理不需要的指标
- 使用联邦架构分散负载
- 考虑使用 remote write 分离热冷数据