Prometheus 基础教程
内容:
- 核心概念
- 架构
- 安装部署
- Exporter
- PromQL
- 告警
- Grafana
- 最佳实践
Prometheus 是一个开源监控系统,“拉(Pull)模式”的时间序列数据库,用于采集、存储、查询、可视化指标。
优势:
- 安装简单、部署易
- 无需客户端 SDK,只要暴露 /metrics 即可
- 强大的查询语言 PromQL
- 与 Grafana 深度集成
- 云原生监控事实标准(Kubernetes 默认监控方案)
Prometheus 通过 HTTP 定期抓取目标服务的 /metrics 数据,存入本地 TSDB(时间序列数据库)。
流程:
Exporter --> Prometheus Server --> TSDB --> PromQL --> Grafana
核心组件:
| 组件 | 描述 |
|---|---|
| Prometheus Server | 抓取数据、存储、查询、告警 |
| Exporter | 暴露被监控的指标 |
| Alertmanager | 管理告警通知 |
| Pushgateway | 临时作业推送指标 |
| Grafana | 可视化工具 |
Prometheus 指标由三部分组成:
指标名 + 标签(可选)+ 样本值 + 时间戳
示例:
http_requests_total{method="GET", handler="/"} 1024 1710000000
有四种指标类型:
| 类型 | 用途 |
|---|---|
| Counter | 单调递增计数器(如接口请求数) |
| Gauge | 可增可减指标(如内存、CPU) |
| Histogram | 直方图(统计分布,常用于延迟) |
| Summary | 百分位计算(通常不建议多用) |
下载与运行
wget https://github.com/prometheus/prometheus/releases/latest/download/prometheus-*.linux-amd64.tar.gz
tar xvf prometheus-*.tar.gz
cd prometheus-*/
./prometheus --config.file=prometheus.yml
默认端口:
- Prometheus UI: http://localhost:9090
- API:/api/v1/query 等
最简配置:
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
说明:
- scrape_interval: 每 15 秒抓取一次
- job_name: 监控任务名称
- targets: 监控目标(Exporter)列表
Prometheus 不直接监控系统,而是通过 Exporter 实现。
常用 Exporter:
| Exporter | 监控内容 | 端口 |
|---|---|---|
| node_exporter | 服务器 CPU/内存/磁盘 | 9100 |
| mysqld_exporter | MySQL 指标 | 9104 |
| blackbox_exporter | 网络/HTTP 探活 | 9115 |
| redis_exporter | Redis 指标 | 9121 |
安装 node_exporter(示例)
wget https://github.com/prometheus/node_exporter/releases/latest/download/node_exporter-*.linux-amd64.tar.gz
tar xvf node_exporter*.tar.gz
./node_exporter
访问查看:
http://localhost:9100/metrics
Prometheus 配置添加监控节点
scrape_configs:
- job_name: 'nodes'
static_configs:
- targets:
- 10.0.0.1:9100
- 10.0.0.2:9100
PromQL 是 Prometheus 最强大的部分,用于:
- 查询单个指标
- 聚合指标
- 监控状态
- 告警表达式
node_cpu_seconds_total
node_cpu_seconds_total{mode!="idle"}
rate(http_requests_total[5m])
统计所有实例的总请求数:
sum(rate(http_requests_total[5m]))
按服务分类:
sum by (instance) (rate(http_requests_total[5m]))
| 函数 | 用途 |
|---|---|
| rate() | counter 速率 |
| irate() | 更灵敏 |
| sum() | 求和 |
| avg() | 平均 |
| max() | 最大 |
| min() | 最小 |
| increase() | 增量 |
告警通过 Prometheus Rule -> Alertmanager -> 通知渠道 实现。
groups:
- name: node_alerts
rules:
- alert: HighCPU
expr: 100 - (avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 90
for: 5m
labels:
severity: warning
annotations:
summary: "节点 CPU 使用率过高"
保存路径如:
/etc/prometheus/rules/*.yml
route:
receiver: 'default'
receivers:
- name: 'default'
email_configs:
- to: "me@example.com"
from: "alert@example.com"
可扩展到:
- 飞书
- 钉钉
- 企业微信
- Webhook
安装 Grafana:
docker run -p 3000:3000 grafana/grafana
添加 Prometheus 作为数据源:
URL: http://prometheus:9090
官方仪表盘(可以直接导入):
| 场景 | Dashboard ID |
|---|---|
| Linux 节点(Node Exporter) | 1860 |
| Kubernetes 监控 | 6417, 8588 |
| Prometheus 自身监控 | 3662 |
标签过多会导致时间序列爆炸 -> OOM/慢查询
避免 user_id、session_id 等动态标签
保持 15s 或 30s。
默认保留 15 天,可调整:
--storage.tsdb.retention.time=30d
否则会扫描大量数据。
使用:
- *_total
- *_seconds
- *_bytes
- 学会安装 Prometheus + node_exporter
- 配置抓取 -> 查看指标
- 学 PromQL(rate / sum / by)
- 配置告警(CPU/内存/磁盘)
- 配合 Grafana 展示图表
- 最后学习运维、性能优化、远程存储(如 Thanos)