乐闻世界logo
搜索文章和话题

如何优化 Prometheus 的存储和性能?

2月21日 15:40

Prometheus 存储优化和性能调优策略:

数据保留策略

yaml
storage: 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 示例

yaml
groups: - name: api_rules rules: - record: job:http_requests:rate5m expr: sum by (job) (rate(http_requests_total[5m]))

监控 Prometheus 自身

  • prometheus_tsdb_compaction_duration
  • prometheus_tsdb_head_samples_appended_total
  • prometheus_target_interval_length_seconds

最佳实践

  • 定期清理不需要的指标
  • 使用联邦架构分散负载
  • 考虑使用 remote write 分离热冷数据
标签:Prometheus