Prometheus 故障排查和常见问题解决:
Prometheus 无法启动:
- 检查配置文件语法:
bashpromtool check config /etc/prometheus/prometheus.yml
- 检查端口占用:
bashlsof -i :9090
- 查看日志:
bashjournalctl -u prometheus -f
数据采集失败:
- 检查目标健康状态:
promqlup{job="your-job"}
- 检查网络连通性:
bashcurl http://target:port/metrics
- 检查认证配置:
- Basic Auth 用户名密码
- TLS 证书有效性
- Bearer Token 是否过期
查询性能问题:
- 优化查询语句:
- 减少时间范围
- 使用标签过滤
- 避免全量扫描
- 使用 Recording Rules:
yamlgroups: - name: performance rules: - record: job:requests:rate5m expr: sum by (job) (rate(http_requests_total[5m]))
- 监控查询性能:
promqlprometheus_query_duration_seconds_sum prometheus_query_duration_seconds_count
内存使用过高:
- 调整数据保留时间:
yamlstorage: tsdb: retention.time: 7d
- 过滤不必要的指标:
yamlmetric_relabel_configs: - source_labels: [__name__] regex: 'expensive_.*' action: drop
- 监控内存指标:
promqlprocess_resident_memory_bytes prometheus_tsdb_memory_series
磁盘空间不足:
- 检查数据大小:
bashdu -sh /var/lib/prometheus/
- 配置数据保留策略:
yamlstorage: tsdb: retention.time: 15d retention.size: 10GB
- 清理旧数据:
bashpromtool tsdb delete-blocks /var/lib/prometheus/ --min-time=2024-01-01T00:00:00Z
告警不触发:
- 检查告警规则:
bashpromtool check rules /etc/prometheus/rules/*.yml
- 检查告警状态:
promqlALERTS{alertname="your-alert"}
- 检查 Alertmanager 连接:
promqlprometheus_notifications_queue_length
TSDB 损坏:
- 检查 TSDB 健康状态:
bashpromtool tsdb analyze /var/lib/prometheus/
- 尝试恢复:
bashpromtool tsdb repair /var/lib/prometheus/
- 备份和恢复:
bash# 备份 promtool tsdb snapshot /var/lib/prometheus/ /backup/ # 恢复 cp -r /backup/* /var/lib/prometheus/
常见错误代码:
context deadline exceeded:查询超时invalid parameter:参数错误out of order sample:样本乱序duplicate series:重复序列
最佳实践:
- 定期备份配置和数据
- 监控 Prometheus 自身指标
- 设置合理的资源限制
- 使用日志聚合工具
- 建立故障处理流程