Prometheus 运维
内容:部署、配置、监控、存储优化、集群扩容、安全、备份恢复、排错、最佳实践
Prometheus 采用 拉模式 (Pull) 从 Exporter 或应用 /metrics 获取指标,存储到本地 TSDB,使用 PromQL 查询,并通过 Alertmanager 发出告警。
核心组件:
| 组件 | 作用 |
|---|---|
| Prometheus Server | 抓取、存储、查询、告警计算 |
| Alertmanager | 告警分组、去重、抑制、通知 |
| Exporter | 暴露监控指标(node、redis、mysql 等) |
| Pushgateway | 用于短周期任务推送指标 |
| Grafana | 可视化 |
二进制部署(强烈推荐)
操作系统建议:Linux x86/arm,建议使用 systemd 管理
下载:
wget https://github.com/prometheus/prometheus/releases/latest/download/prometheus-*.linux-amd64.tar.gz
tar xf prometheus-*.tar.gz
cp prometheus promtool /usr/local/bin/
mkdir -p /etc/prometheus /data/prometheus
cp -r consoles console_libraries /etc/prometheus/
systemd:
[Unit]
Description=Prometheus Server
After=network-online.target
[Service]
User=prometheus
ExecStart=/usr/local/bin/prometheus \
--config.file=/etc/prometheus/prometheus.yml \
--storage.tsdb.path=/data/prometheus \
--storage.tsdb.retention.time=30d \
--web.enable-lifecycle \
--web.enable-admin-api \
--storage.tsdb.wal-compression
Restart=on-failure
[Install]
WantedBy=multi-user.target
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_timeout: 10s
scrape_configs:
- job_name: 'node'
static_configs:
- targets:
- 10.0.0.1:9100
- 10.0.0.2:9100
如果你是 K8s:
scrape_configs:
- job_name: 'kubernetes-nodes'
kubernetes_sd_configs:
- role: node
alerting:
alertmanagers:
- static_configs:
- targets: ["10.0.0.5:9093"]
| 监控规模 | TS 数量 | 服务器建议配置 |
|---|---|---|
| 小型(<50台) | 500k | 2C / 4G / 50GB |
| 中型(<200台) | 1–5M | 4C–8C / 16–32G / 200GB |
| 大型(>500台) | 5M–20M | 不建议单机 -> Thanos/Cortex |
⚠️ Prometheus 单机不是无限扩容的
⚠️ 内存主要取决于“时间序列数量”(labels 爆炸)
Prometheus TSDB 目录结构:
/data/prometheus
├── wal/ 写前日志(实时)
├── chunks_head/ 内存数据持久化
└── 01AABBCCDDEEFG/ 压缩数据块(2小时 1 个 block)
WAL 写入非常频繁,HDD 会让抓取卡死。
--storage.tsdb.retention.time=15d
--storage.tsdb.wal-compression
- 高基数 = OOM/OOMKill 卡死
- ❌ user_id
- ❌ session_id
- ❌ pod UID
- ❌ request_id
- ❌ random 值
Prometheus 本身是单点,但可以用 双机热备 实现高可用:
Prometheus A <- same targets -> Prometheus B
Alertmanager 负责告警去重。
缺点:数据不共享,但对于告警系统完全可接受。
使用 Thanos 或 VictoriaMetrics。
Thanos 组件
- Sidecar(连接 Prometheus)
- Store(对象存储)
- Query(聚合查询)
- Compactor(数据压缩)
- Ruler(分布式告警)
优势:
- 无限存储
- 多 Prometheus 聚合查询
- 自动去重
- 性能高
Prometheus 默认无鉴权、无 HTTPS,因此必须加固。
auth_basic "Auth";
auth_basic_user_file /etc/nginx/.passwd;
禁用:
--web.enable-admin-api=False
或保护 /api/v1/admin/* 路径。
Prometheus 的 TSDB 支持在线 Snapshot。
curl -XPOST http://localhost:9090/api/v1/admin/tsdb/snapshot
Snapshot 存放在:
/data/prometheus/snapshots/xxxxxx/
systemctl stop prometheus
tar czf prometheus-backup.tar.gz /data/prometheus
systemctl start prometheus
直接覆盖数据目录即可。
Prometheus 自监控非常重要。
推荐抓取自身:
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
常用监控指标:
| 指标 | 含义 |
|---|---|
| prometheus_tsdb_head_series | 当前时序量(容量核心) |
| prometheus_tsdb_wal_corruptions_total | WAL 损坏 |
| prometheus_engine_query_duration_seconds | 查询耗时 |
| prometheus_tsdb_compaction_duration_seconds | 压缩耗时 |
强烈建议至少配置以下告警:
- TSDB Series > 阈值
- 目标抓取失败
- Scrape timeout
- WAL 错误
- CPU 使用率高
- 内存 > 90%
- 磁盘 > 80%
- load 过高
- network error
- 高基数 label
- 查询语句过重(Grafana)
- 抓取间隔太快
解决方法:
- 查 label 数量
- 用 recording rules 固化复杂计算
- 限制 Grafana 查询时间范围
- 防火墙阻挡
- /metrics 不存在
- exporter 挂了
- TLS 证书问题
- ICMP / DNS 问题
- block 太多
- TSDB 目录损坏
- 未正常关闭导致 WAL 恢复过久
可以:
promtool tsdb analyze /data/prometheus
- 查询时间跨度太大(>24h)
- 查询未加标签过滤
- 大量 label join
< 5M 完全能跑
20M 必须上 Thanos / VM