Prometheus 存储与扩展
本文档涵盖 Prometheus 本地 TSDB 存储配置、远程存储(Remote Write/Read)、Pushgateway 推送网关以及 Federation 联邦集群架构。
符号说明
| 符号 | 含义 |
|---|---|
| 🚨 | 陷阱 / Pitfall |
| 💡 | 最佳实践 / Best practice |
| 🔬 | 深入原理 / Deep-dive |
| ⚡ | 性能提示 / Performance note |
1. 存储与保留策略
1.1 本地 TSDB 配置
prometheus \
--storage.tsdb.path=/var/lib/prometheus/data \
--storage.tsdb.retention.time=30d \
--storage.tsdb.retention.size=100GB \
--storage.tsdb.min-block-duration=2h \
--storage.tsdb.max-block-duration=36h| 参数 | 默认值 | 说明 |
|---|---|---|
--storage.tsdb.path |
data/ |
数据目录 |
--storage.tsdb.retention.time |
15d |
数据保留时长(如 30d、90d、1y) |
--storage.tsdb.retention.size |
0(禁用) |
最大占用空间(如 100GB),超限后删除最旧 block |
--storage.tsdb.min-block-duration |
2h |
头块持久化前的最小时间范围 |
--storage.tsdb.max-block-duration |
36h |
压缩块的最大时间范围 |
💡 同时设置
time和size时,先达到哪个上限就触发哪个清理。⚡ 磁盘容量估算公式:
needed_disk = retention_seconds × samples_per_second × 1.5 (bytes_per_sample)。每个样本压缩后大约 1-2 字节。
1.2 远程存储(Remote Write / Remote Read)
为克服本地存储无持久化、不能水平扩展、历史数据有限的限制,Prometheus 提供了远程存储接口:
┌────────────────┐ remote_write ┌─────────────────────┐
│ Prometheus │ ──────────────────▶ │ 长期存储后端 │
│ (本地 TSDB) │ │ (Thanos/Mimir/ │
│ 15-30 天 │ remote_read │ VictoriaMetrics │
│ │ ◀────────────────── │ /InfluxDB...) │
└────────────────┘ │ 1 年+ │
│ (降采样后) │remote_write 配置:
remote_write:
- url: "http://thanos-receive:19291/api/v1/write"
remote_timeout: 30s
name: "thanos"
write_relabel_configs:
- source_labels: [__name__]
regex: 'unwanted_.*'
action: drop
queue_config:
capacity: 100000
max_shards: 1000
max_samples_per_send: 100
batch_send_deadline: 5sremote_read 配置:
remote_read:
- url: "http://thanos-query:10902/api/v1/read"
remote_timeout: 1m
read_recent: false🚨 告警和 Recording Rules 只使用本地 TSDB(保障可靠性),remote_read 仅用于查询时的数据补充。
常见远程存储后端:
| 后端 | 特点 |
|---|---|
| Thanos | 最流行,支持全局视图 + 降采样 + 对象存储 |
| Cortex / Mimir | Grafana 官方,水平扩展,支持多租户 |
| VictoriaMetrics | 高性能,单二进制,兼容 PromQL |
| InfluxDB | 通过适配器接入 |
2. Pushgateway 推送网关
2.1 适用场景
Pushgateway 用于短生命周期任务(CronJob、Batch Job、Lambda 函数)的指标暴露——这些任务启动快、结束快,Prometheus 来不及抓取 /metrics。
🚨 Pushgateway 不是常规方案! 长运行的服务应用拉模式(Prometheus 主动抓取
/metrics),不要用 Pushgateway。Pushgateway 只适用于短任务。
2.2 基本使用
# 推送指标到 Pushgateway
echo "batch_job_duration_seconds 42" | \
curl --data-binary @- http://pushgateway:9091/metrics/job/batch_job/instance/server-1
# 带标签
echo "batch_job_duration_seconds{status=\"success\"} 42" | \
curl --data-binary @- http://pushgateway:9091/metrics/job/batch_job/instance/server-1/status/success2.3 Prometheus 抓取 Pushgateway
scrape_configs:
- job_name: 'pushgateway'
honor_labels: true # 保留推送时的原始标签
static_configs:
- targets: ['pushgateway:9091']2.4 关键注意事项
| 要点 | 说明 |
|---|---|
| 🚨 Pushgateway 中的数据是持久化的 | 任务结束后指标不会自动消失,需要客户端主动 DELETE |
| 🚨 不要用时间戳 | Pushgateway 忽略客户端发送的时间戳,以接收时间为准 |
| 💡 每个 batch job 唯一标识 | 用不同的 instance 标签区分不同的任务运行 |
| 💡 任务结束时清理 | curl -X DELETE http://pushgateway:9091/metrics/job/{job_name}/instance/{instance} |
3. Federation 联邦集群
Federation 用于跨数据中心/跨集群的 Prometheus 层级架构。
3.1 架构模式
┌─────────────────────┐
│ Global Prometheus │ ← 全局视图
└──────┬──────┬───────┘
│ │
federation │ │ federation
│ │
┌────────────────┴┐ ┌──┴─────────────────┐
│ DC1 Prometheus │ │ DC2 Prometheus │ ← 数据中心级
└────────┬────────┘ └────────┬────────────┘
│ │
scrape scrape
│ │
┌────────┴────┐ ┌─────────┴──────┐
│ targets │ │ targets │ ← 服务/节点
└─────────────┘ └────────────────┘3.2 Federation 配置
# 在 Global Prometheus 上配置
scrape_configs:
- job_name: 'federate-dc1'
honor_labels: true
metrics_path: '/federate'
params:
'match[]':
- '{__name__=~"node_.*"}' # 只同步节点指标
- '{__name__=~"up"}' # 目标健康状态
- '{__name__=~"workload_.*"}' # 自定义指标
static_configs:
- targets:
- 'dc1-prometheus:9090'
- job_name: 'federate-dc2'
honor_labels: true
metrics_path: '/federate'
params:
'match[]':
- '{__name__=~"node_.*"}'
- '{__name__=~"up"}'
- '{__name__=~"workload_.*"}'
static_configs:
- targets:
- 'dc2-prometheus:9090'💡 Federation 是选择性同步——通过
match[]参数只拉取需要的指标,避免全量同步。⚡ 对 Federation 不建议用 15s 的抓取间隔——用 30s 或 60s,减少全局节点的存储和网络压力。
参考资料: