深色模式
prometheus.yml 逐行讲解
配置文件是 Prometheus 的“采集清单”,读懂每一段才能灵活加被监控对象。
适用环境
- 已按二进制方式安装 Prometheus(见前文)
- 配置文件位于
/etc/prometheus/prometheus.yml
bash
# 先看一眼默认配置
cat /etc/prometheus/prometheus.yml1
2
2
操作步骤
1. 全局段 global
yaml
global:
scrape_interval: 15s # 默认每次抓取间隔
evaluation_interval: 15s # 告警/记录规则评估间隔
scrape_timeout: 10s # 单次抓取超时1
2
3
4
2
3
4
2. 抓取任务段 scrape_configs
yaml
scrape_configs:
- job_name: prometheus # 任务名,会作为 job 标签
static_configs:
- targets: ['localhost:9090'] # 抓取目标列表1
2
3
4
2
3
4
3. 增加一个真实主机任务
假设已有一台 node_exporter 跑在 192.168.1.10:9100:
yaml
- job_name: node
static_configs:
- targets: ['192.168.1.10:9100']
labels:
env: prod1
2
3
4
5
2
3
4
5
4. 校验并热加载
bash
# 校验语法(不真正启动)
promtool check config /etc/prometheus/prometheus.yml
# 热加载,不中断服务
curl -X POST http://localhost:9090/-/reload1
2
3
4
2
3
4
验证
promql
# 在 Graph 页面查询,能看到新 target
up{job="node"}1
2
2
常见坑
缩进用空格
YAML 严禁 Tab,缩进错误会直接导致配置无法解析。
热加载需开启 --web.enable-lifecycle
若启动未加 --web.enable-lifecycle,/-/reload 会返回 404,需重启进程。