-今日
-本周
-本月
-累计
独立访客
项目工作台‹运维 个人项目与实践记录
我的手记第 14 篇 / 共 14 篇
→ 下一篇

手记 014:从零搭 Prometheus + Grafana:指标、告警和面板

node_exporter`127.0.0.1:9100`
Prometheus`127.0.0.1:19090`
Grafana`127.0.0.1:3000`

日期: 2026-11-10 难度: ⭐⭐⭐⭐☆

这次把自研的 daily-check 巡检升级成标准监控栈:node_exporter 采指标,Prometheus 存指标并做告警,Grafana 画图。 三个组件全部只绑 127.0.0.1,通过 SSH 隧道访问,不开公网端口。

1. 先看资源与端口

服务器 2C2G,可用内存约 1.2GB,swap 4GB。三个组件加起来约占 200–300MB。

预检发现两件事:

端口规划:

组件地址
node_exporter127.0.0.1:9100
Prometheus127.0.0.1:19090
Grafana127.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。

FinalShell 隧道配置
FinalShell 隧道配置

6. 数据源与面板

Grafana → Connections → Add new connection → Data sources → Prometheus:

Connections 菜单
Connections 菜单

做了一页「运维总览」,四张图:

运维总览
运维总览

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

节点详情
节点详情

7. 踩坑汇总

  1. 9090 被 nginx 占用:Prometheus 改用 19090。
  2. GitHub 下载超时:改用 daocloud 镜像,必要时从镜像里提取二进制。
  3. node_exporter 报 217/USER:用户没建成,useradd 被折行吃掉。
  4. rules.yml YAML 缩进错误:Prometheus 退出码 2,用脚本按标准缩进重写。
  5. 容器名冲突:docker rm -f prometheus 后再启动。
  6. SSH 已连接不等于端口已转发:必须单独加隧道。

8. 验收与遗留

验收:

遗留:

我的手记第 14 篇 / 共 14 篇
→ 下一篇

可用键盘 ← → 切换上下篇