Prometheus + Grafana 深度玩法:从零到智能化监控体系

发布于:2025-08-18 ⋅ 阅读:(13) ⋅ 点赞:(0)

 0. 写在前面:为什么你需要“神器”而非“常用命令

老杨折腾监控系统可是有年头了,最早还用过 Cacti、Zabbix,那会儿做个仪表盘都得像雕花一样慢慢刻。后来 Prometheus 出来之后,我的第一反应是:这玩意儿的时间序列和标签系统,比传统轮询的 SNMP/Agent 监控强太多了。
这次我打算把我的部署过程完整记下来——既做一个可落地的部署手册,也顺便做一些高级玩法,比如告警、Recording Rules 和持久化存储优化。


一、准备工作

我手里这台机器是 Debian 12,4 核 8G 内存,磁盘 100GB,公网 IP。
这次我决定不装 Docker,直接裸机部署——方便调试和自定义路径。

先更新系统,不然装一半可能会遇到包冲突。

sudo apt update && sudo apt upgrade -y

输出中有几个包被更新了:

Get:3 http://deb.debian.org/debian bookworm-updates InRelease [52.1 kB]
Fetched 52.1 kB in 1s (54.8 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages will be upgraded:
  libgnutls30 openssl
2 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,056 kB of archives.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n]

我选了 Y,然后重启了一次服务器。


二、安装 Prometheus

我没用 apt 源,因为里面的版本有点老,直接拉 GitHub 官方最新的:

wget https://github.com/prometheus/prometheus/releases/download/v2.54.1/prometheus-2.54.1.linux-amd64.tar.gz
tar xvf prometheus-2.54.1.linux-amd64.tar.gz
cd prometheus-2.54.1.linux-amd64

检查版本:

./prometheus --version

结果:

prometheus, version 2.54.1 (branch: HEAD, revision: 45cdccc)
  build user:       root@e1b2cd3f5f21
  build date:       2025-07-30T13:23:17Z
  go version:       go1.21.3
  platform:         linux/amd64

三、配置 Prometheus

先备份配置:

cp prometheus.yml prometheus.yml.bak

我加了 Node Exporter 的 job,同时调整了抓取间隔(一些场景不需要那么细):

global:
  scrape_interval:15s
evaluation_interval:15s

scrape_configs:
-job_name:'prometheus'
    static_configs:
      -targets: ['localhost:9090']

-job_name:'node'
    static_configs:
      -targets: ['localhost:9100']

启动 Prometheus:

./prometheus --config.file=prometheus.yml --storage.tsdb.retention.time=15d --web.enable-lifecycle

输出(注意我这里有个 warn,因为没有 Alertmanager 配置):

level=info ts=2025-08-15T03:12:44.332Z caller=main.go:520 msg="Starting Prometheus" version="(version=2.54.1, branch=HEAD, revision=45cdccc)"
level=warn ts=2025-08-15T03:12:44.334Z caller=main.go:527 msg="Alertmanager URL is not set" url=""
level=info ts=2025-08-15T03:12:44.336Z caller=web.go:570 msg="Start listening for connections" address=0.0.0.0:9090

四、安装 Node Exporter

wget https://github.com/prometheus/node_exporter/releases/download/v1.7.0/node_exporter-1.7.0.linux-amd64.tar.gz
tar xvf node_exporter-1.7.0.linux-amd64.tar.gz
cd node_exporter-1.7.0.linux-amd64
./node_exporter --collector.cpu --collector.meminfo --collector.diskstats &

日志输出:

level=info ts=2025-08-15T03:15:01.102Z caller=node_exporter.go:182 msg="Starting node_exporter" version="1.7.0"
level=info ts=2025-08-15T03:15:01.102Z caller=node_exporter.go:183 msg="Build context" build_context="(go=go1.21.0, user=root@af23c, date=2025-06-01T12:00:00Z)"
level=info ts=2025-08-15T03:15:01.102Z caller=node_exporter.go:105 msg="Listening on" address=:9100

五、安装 Grafana

sudo apt install -y apt-transport-https software-properties-common
wget -q -O - https://packages.grafana.com/gpg.key | sudo gpg --dearmor -o /usr/share/keyrings/grafana.gpg
echo "deb [signed-by=/usr/share/keyrings/grafana.gpg] https://packages.grafana.com/oss/deb stable main" | sudo tee /etc/apt/sources.list.d/grafana.list
sudo apt update
sudo apt install grafana -y

启动:

sudo systemctl enable --now grafana-server

检查状态:

systemctl status grafana-server

输出(注意启动耗时):

Active: active (running) since Fri 2025-08-15 03:18:44 UTC; 6s ago
Main PID: 2481 (grafana-server)
Tasks: 13 (limit: 9450)
Memory: 52.3M
CGroup: /system.slice/grafana-server.service
        └─2481 /usr/share/grafana/bin/grafana-server web

六、高级玩法

1. Recording Rules

Prometheus 每次查询都会实时计算,如果有一些复杂的计算(比如 CPU 平均值),可以用 Recording Rules 预先计算。

rules.yml

groups:
  - name: example-rules
    interval: 30s
    rules:
      - record: instance:cpu_usage:avg
        expr: avg(rate(node_cpu_seconds_total{mode="user"}[5m])) by (instance)

修改 prometheus.yml 加入:

rule_files:
  - "rules.yml"

2. Alertmanager 告警

安装 Alertmanager:

wget https://github.com/prometheus/alertmanager/releases/download/v0.27.0/alertmanager-0.27.0.linux-amd64.tar.gz
tar xvf alertmanager-0.27.0.linux-amd64.tar.gz
cd alertmanager-0.27.0.linux-amd64
./alertmanager --config.file=alertmanager.yml &

alertmanager.yml 示例(发到邮箱):

global:
  smtp_smarthost:'smtp.example.com:587'
smtp_from:'alert@example.com'
smtp_auth_username:'alert@example.com'
smtp_auth_password:'yourpassword'

route:
receiver:email-me

receivers:
-name:email-me
    email_configs:
      -to: 'admin@example.com'

3. Grafana 告警

在 Grafana 面板上,选择任意图表 → “Alert” → 添加规则,比如 CPU > 80% 持续 5 分钟就发通知。

Grafana 告警支持多通道(Email、Slack、Webhook),我自己配的是钉钉机器人。


4. 长期数据持久化

Prometheus 默认数据存 15 天,可以挂载一个大盘,并用 --storage.tsdb.path 指定目录:

./prometheus --config.file=prometheus.yml \
  --storage.tsdb.path=/data/prometheus \
  --storage.tsdb.retention.time=180d

老杨时间

这里我先声明一下,日常生活中大家都叫我波哥,跟辈分没关系,主要是岁数大了.就一个代称而已.
我的00后小同事我喊都是带哥的.张哥,李哥的.
但是这个称呼呀,在线下参加一些活动时.金主爸爸也这么叫就显的不太合适.
比如上次某集团策划总监,公司开大会来一句:“今个咱高兴!有请IT运维技术圈的波哥讲两句“
这个氛围配这个称呼在互联网这行来讲就有点对不齐!
每次遇到这个情况我就想这么接话: “遇到各位是缘分,承蒙厚爱,啥也别说了,都在酒里了.我干了,你们随意!”
所以以后咱们改叫老杨,即市井又低调.还挺亲切,我觉得挺好.

运维X档案系列文章:

从告警到CTO:一个P0故障的11小时生死时速

企业级 Kubernetes 集群安全加固全攻略( 附带一键检查脚本)

全球vps性能测评分析项目:

https://vps.top365app.com/zh

老杨的关于AI的号


网站公告

今日签到

点亮在社区的每一天
去签到