手记 014:从零搭 Prometheus + Grafana:指标、告警和面板
日期: 2026-11-10 难度: ⭐⭐⭐⭐☆
这次把自研的 daily-check 巡检升级成标准监控栈:node_exporter 采指标,Prometheus 存指标并做告警,Grafana 画图。 三个组件全部只绑 127.0.0.1,通过 SSH 隧道访问,不开公网端口。
1. 先看资源与端口
服务器 2C2G,可用内存约 1.2GB,swap 4GB。三个组件加起来约占 200–300MB。
预检发现两件事:
- 9090 已被 nginx 占用(lab02 的 vhost),Prometheus 默认端口不能用,改成 19090;
- 3000 / 9100 / 19090 空闲,没装任何 Prometheus/Grafana 组件。
端口规划:
| 组件 | 地址 |
|---|---|
| node_exporter | 127.0.0.1:9100 |
| Prometheus | 127.0.0.1:19090 |
| Grafana | 127.0.0.1:3000 |
2. node_exporter
GitHub 下载超时(HTTP/2 PROTOCOL_ERROR),改成从 Docker 镜像里提取二进制:
docker pull docker.m.daocloud.io/prom/node-exporter:latest
docker create --name ne_tmp docker.m.daocloud.io/prom/node-exporter:latest
docker cp ne_tmp:/bin/node_exporter /tmp/ne_bin
docker rm -f ne_tmp
安装并建用户:
useradd --no-create-home --shell /sbin/nologin node_exporter
install -m 755 /tmp/ne_bin /usr/local/bin/node_exporter
systemd unit(顶格写):
[Unit]
Description=Node Exporter
After=network.target
[Service]
User=node_exporter
Group=node_exporter
ExecStart=/usr/local/bin/node_exporter --web.listen-address=127.0.0.1:9100
Restart=on-failure
RestartSec=2s
[Install]
WantedBy=multi-user.target
验证:
$ systemctl is-active node_exporter
active
$ curl -s http://127.0.0.1:9100/metrics | head -3
# HELP go_gc_duration_seconds ...
踩坑:第一次 unit 报 217/USER,因为 useradd 和 install 被终端折行吃掉了,用户和二进制都没装上。
3. Prometheus
用 Docker,配置目录 /root/ops-lab/docker/prometheus/,数据用命名卷 prometheus-data,监听 19090。
prometheus.yml:
global:
scrape_interval: 15s
evaluation_interval: 15s
rule_files:
- /etc/prometheus/rules.yml
scrape_configs:
- job_name: node
static_configs:
- targets: ["127.0.0.1:9100"]
关键启动参数:
--storage.tsdb.retention.time=15d
--storage.tsdb.retention.size=512MB
--web.listen-address=127.0.0.1:19090
--web.enable-lifecycle
踩坑:容器 Restarting (2),日志报:
parse rules from file "/etc/prometheus/rules.yml":
yaml: line 3: mapping values are not allowed in this context
原因是 rules.yml 的缩进被终端粘贴弄乱了。用脚本按标准两空格缩进重写后,容器正常启动。
验证:
$ curl -s http://127.0.0.1:19090/-/ready
Prometheus Server is Ready.
$ curl -s http://127.0.0.1:19090/api/v1/targets | grep -o '"health":"[a-z]*"'
"health":"up"
4. 告警规则与负向测试
rules.yml:
groups:
- name: demo
rules:
- alert: TargetDown
expr: up == 0
for: 1m
labels:
severity: warning
annotations:
summary: "{{ $labels.instance }} is down"
停掉 node_exporter 90 秒,验证告警真的会触发:
$ systemctl stop node_exporter
$ sleep 90
$ curl -s http://127.0.0.1:19090/api/v1/alerts
{"status":"success","data":{"alerts":[{"labels":{"alertname":"TargetDown",...},"state":"firing",...}]}}
$ systemctl start node_exporter
$ sleep 30
$ curl -s http://127.0.0.1:19090/api/v1/alerts
{"status":"success","data":{"alerts":[]}}
告警从 firing 回到空,说明「能失败」的验证成立。
5. Grafana
Grafana 用 Docker 镜像 docker.m.daocloud.io/grafana/grafana:latest,版本 13.3.0,监听 127.0.0.1:3000,密码随机生成,数据用命名卷 grafana-data。
健康检查:
$ curl -s http://127.0.0.1:3000/api/health
{"database":"ok","version":"13.3.0",...}
访问方式:不开公网 3000,用 FinalShell 加一条本地端口转发 3000 -> 127.0.0.1:3000。

6. 数据源与面板
Grafana → Connections → Add new connection → Data sources → Prometheus:
- URL:
http://127.0.0.1:19090 - Save & test 成功

做了一页「运维总览」,四张图:
- 1 分钟负载:
node_load1 - 内存使用率:
100 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes * 100) - CPU 使用率:
100 - (avg by(instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) - 根分区使用率:
100 - (node_filesystem_avail_bytes{mountpoint="/",fstype!~"tmpfs|overlay"} / node_filesystem_size_bytes{mountpoint="/",fstype!~"tmpfs|overlay"} * 100)

另外导入了 Node Exporter Full(ID 1860),用于排障深挖。

7. 踩坑汇总
- 9090 被 nginx 占用:Prometheus 改用 19090。
- GitHub 下载超时:改用 daocloud 镜像,必要时从镜像里提取二进制。
- node_exporter 报 217/USER:用户没建成,
useradd被折行吃掉。 - rules.yml YAML 缩进错误:Prometheus 退出码 2,用脚本按标准缩进重写。
- 容器名冲突:
docker rm -f prometheus后再启动。 - SSH 已连接不等于端口已转发:必须单独加隧道。
8. 验收与遗留
验收:
- node_exporter
/metrics有数据; - Prometheus targets
up; TargetDown停目标后 firing,恢复后清空;- Grafana
/api/health正常,数据源 Save & test 成功,「运维总览」四图有曲线。
遗留:
- 没有接 Alertmanager,告警只停留在 Prometheus 的 firing 状态,没有通知出口;
- Grafana 的告警通知渠道还没配;
- 容器和 systemd 服务已设开机自启,但监控配置还没有全部纳入
conf/版本管理; - 2C2G 内存有限,Prometheus 目前约 39MB、Grafana 约 150MB,后面面板多了要观察内存。
可用键盘 ← → 切换上下篇