Prometheus
Prometheus 上手教程
Prometheus 的核心思路很简单:用 Pull 模型定期抓取目标暴露的 /metrics 端点,把指标存成本地时序数据,再通过 PromQL 查询、告警和 Grafana 可视化。
一、安装 Prometheus
推荐先下载官方二进制包,解压后直接运行。
# 下载并解压
tar xvfz prometheus-*.tar.gz
cd prometheus-*
# 启动
./prometheus --config.file=prometheus.yml启动后访问:
http://localhost:9090如果本地已有 Docker,也可以用 Docker Compose 快速跑起来:
version: '3.8'
services:
prometheus:
image: prom/prometheus:latest
ports:
- "9090:9090"
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
command:
- '--config.file=/etc/prometheus/prometheus.yml'docker compose up -d二、最小配置文件
prometheus.yml 是 Prometheus 唯一的主配置:
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]关键字段含义:
| 字段 | 作用 |
|---|---|
scrape_interval | 默认抓取间隔,一般 15s |
evaluation_interval | 告警规则评估间隔 |
scrape_configs | 定义抓取哪些目标 |
job_name | 任务名,会作为 job 标签写入指标 |
targets | 被监控目标的地址列表 |
三、配置第一个监控目标:Node Exporter
Node Exporter 用来采集 Linux 主机的 CPU、内存、磁盘、网络等指标。
# 下载 node_exporter
wget https://github.com/prometheus/node_exporter/releases/download/v1.9.0/node_exporter-1.9.0.linux-amd64.tar.gz
tar xvfz node_exporter-*.tar.gz
cd node_exporter-*
# 启动
./node_exporter验证是否生效:
curl http://localhost:9100/metrics然后把它加入 Prometheus 配置:
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
- job_name: "node"
static_configs:
- targets: ["localhost:9100"]修改配置后重启 Prometheus,或者在启动时加上 --web.enable-lifecycle,然后用:
curl -X POST http://localhost:9090/-/reload热加载配置。
打开 Status → Targets,如果看到 node 任务状态为 UP,说明采集成功。
四、用 PromQL 查询指标
打开 Prometheus UI,进入 Graph / Query 页面,常用查询如下:
# 查看所有指标
up
# 查看某个指标
node_cpu_seconds_total
# 按标签过滤
node_cpu_seconds_total{mode="idle"}
# 计算 CPU 使用率
(1 - avg(rate(node_cpu_seconds_total{mode="idle"}[5m])) by (instance)) * 100
# 按 job 聚合
sum by (job) (rate(http_requests_total[5m]))
# 计算 P99 延迟
histogram_quantile(0.99, rate(http_request_duration_seconds_bucket[5m]))几个高频函数:
| 函数 | 用途 |
|---|---|
rate() | 计算计数器每秒增长率 |
increase() | 计算一段时间内的总增量 |
avg() / sum() | 聚合计算 |
histogram_quantile() | 计算分位数,如 P95、P99 |
五、配置告警规则
告警分为两步:定义规则 + 配置 Alertmanager。
创建 alert_rules.yml:
groups:
- name: node_alerts
rules:
- alert: HighCpuUsage
expr: (1 - avg(rate(node_cpu_seconds_total{mode="idle"}[5m])) by (instance)) * 100 > 80
for: 5m
labels:
severity: warning
annotations:
summary: "实例 {{ $labels.instance }} CPU 使用率过高"
value: "{{ $value }}%"
- alert: InstanceDown
expr: up == 0
for: 1m
labels:
severity: critical
annotations:
summary: "实例 {{ $labels.instance }} 已下线"在 prometheus.yml 中引用:
rule_files:
- "alert_rules.yml"
alerting:
alertmanagers:
- static_configs:
- targets: ["localhost:9093"]Alertmanager 最小配置 alertmanager.yml:
route:
receiver: "email"
repeat_interval: 1h
receivers:
- name: "email"
email_configs:
- to: "ops@example.com"六、对接 Grafana
Grafana 是 Prometheus 最常用的可视化前端。
1. 启动 Grafana,默认端口 3000。
2. 左侧菜单进入 Configuration → Data Sources。
3. 点击 Add data source,选择 Prometheus。
4. 在 URL 中填写:
http://localhost:90905. 点击 Save & Test。
之后就可以新建 Dashboard,用 PromQL 画 CPU、内存、磁盘、网络等面板。也可以直接导入社区现成模板,比如搜索 Node Exporter Full。
七、进阶路径
- 用 file_sd_configs 管理多主机目标,避免频繁改主配置。
- 用 Kubernetes 服务发现 自动发现 Pod 和 Service。
- 用 Thanos / VictoriaMetrics / Mimir 做长期存储。
- 用 Pushgateway 采集短生命周期任务指标。
- 用 Recording Rules 预计算高频查询,降低查询压力。
- 生产环境建议配置 TSDB 保留策略,例如
--storage.tsdb.retention.time=30d。
新手避坑
- 不要只装 Prometheus,不装 Exporter;Prometheus 本身只能监控自己和配置里声明的目标。
targets地址写错会导致 Target 显示 DOWN。prometheus.yml是 YAML 文件,缩进必须正确。- 告警规则修改后,需要确认 Prometheus 已重新加载配置。
- 生产环境不要把
prometheus.yml和alert_rules.yml放在临时目录,建议纳入版本管理。 - 本地测试完成后,记得给 Prometheus 和 Node Exporter 配置 systemd 服务,避免重启丢失。