Prometheus入门|搭建Prometheus+Node‑Exporter+Grafana完整主机监控平台
一、Prometheus基础概念
1.1 什么是Prometheus
Prometheus是开源系统监控与告警工具,2012年诞生于SoundCloud,2016年成为CNCF第二个托管开源项目(仅次于Kubernetes)。
Prometheus将其指标 收集并存储为时间序列数据 ,即指标信息与记录时的时间戳 以及称为标签的可选键值对一起存储。
核心:时序数据库,存储「指标数值 + 时间戳 + 标签label」时间序列数据。
1.2 核心特征
- 多维数据模型:指标名称+键值标签区分不同时序;
- 强大PromQL查询语言,支持复杂聚合过滤;
- 无分布式依赖,单机自治,不依赖外部存储;
- Pull拉取模型,HTTP主动抓取监控指标;
- 支持PushGateway处理短时任务推送指标;
- 支持静态配置 / 服务发现(K8s/file_sd)发现监控目标;
- 内置WebUI,可对接Grafana做可视化。
1.3 什么是指标
-
概念 :指标是数值型测量结果;时间序列=指标数值+时间戳,持续记录系统随时间的变化。
-
举例:web服务器请求响应时间、数据库活跃连接数、CPU/磁盘使用率。
-
价值:通过指标定位系统故障根因,例如请求量暴涨导致应用卡顿,依靠请求计数指标可以分析问题,指导扩容。
-
告警等级规范(按业务影响划分,不单纯看技术数值)
| 等级 | 标签severity | 业务现象 | 通知策略 | 响应要求 |
|---|---|---|---|---|
| P0严重 | critical |
核心服务不可用、核心链路中断、存在数据丢失风险(API成功率为0、etcd故障、连接池耗尽) | 钉钉+短信+电话,repeat_interval:15m重复告警,做告警分组抑制风暴 |
立即处理 |
| P1警告 | warning |
性能严重下降,但业务未完全中断;资源临近阈值(CPU持续90%、磁盘空间不足10%、Pod频繁重启) | 钉钉+邮件,聚合告警,减少消息骚扰 | 30分钟内响应 |
| P2信息 | info |
非核心组件异常、潜在风险,业务无明显影响(非核心服务延迟高、负载轻微偏高) | 仅落日志/展示在监控看板,不即时推送告警 | 工作日处理即可 |
面试准备P1级别的两个故障案例就行
编码配置最佳实践
- prometheus告警规则中,固定标签名:
labels.severity,取值固定critical/warning/info,不要自定义level/priority,不要中英文混用。 - 额外增加业务标签:
service业务服务名、team负责团队,方便Alertmanager按「告警等级+团队」路由分发告警。 - Alertmanager配套参数
group_by:对相同告警聚合,抑制告警风暴;group_wait/group_interval:聚合等待时间,合并多条同类告警再发送;repeat_interval:严重告警重复提醒间隔。
核心原则
告警等级依据业务实际影响程度判定,而不是单纯看技术指标数字。告警等级规范
等级必须锚定在真实业务后果上,而不是技术指标本身。
1.4 Prometheus组件
普罗米修斯生态系统由多个组成部分组成,其中许多是可选的:
- Prometheus Server:核心,抓取、存储时序数据,执行规则;
- Client libraries:用于仪表化应用程序代码的客户端库;
- PushGateway:短时批量作业指标推送网关;
- Exporter:各类中间件、主机指标采集导出器-----被监控端下载;
- Alertmanager:独立告警管理器,处理告警分组、去重、通知;
- 周边工具:Grafana、promtool等。
大部分组件使用Go编写,编译为静态二进制文件,部署简单。
1.5 Prometheus架构
每个组件相互独立,自带目标发现机制

- 数据采集模式
- 默认拉取(Pull) :直接访问被监控端兼容Prometheus的HTTP
/metrics接口,采集CPU、内存、请求延迟、业务自定义指标。 - 短时任务/无法直连场景:借助 PushGateway推送网关,作业把指标推送给网关,Prometheus再从网关拉取数据。
- 默认拉取(Pull) :直接访问被监控端兼容Prometheus的HTTP
- 数据存储 采集的指标存入本地时序数据库TSDB;以指标名称 + labels标签作为维度组织时序数据,查询检索效率高。
- 查询与告警 内置 PromQL查询语言,支持复杂查询、聚合统计;支持配置规则,可生成衍生指标,还能根据条件生成告警。
- 可视化展示 自身UI能力有限,一般对接 Grafana;Grafana调用Prometheus API,实现图表、仪表盘可视化,方便监控排查故障。
总结:Prometheus采用拉取模型采集各类指标,本地时序库存储,依靠PromQL做查询告警,搭配Grafana完成可视化,用于业务与基础设施实时监控排障。
1.6 适用场景
- 记录纯数字时序指标;
- 物理机、虚拟机主机监控;
- 微服务、K8s高度动态环境监控;
- 故障场景下,依然可以查看现有监控数据。
1.6 不适合场景
不能需要100%精确计费类场景,Prometheus抓取会丢采样数据,不适合计费统计。
官方文档地址:https://prometheus.io/docs/introduction/overview/
二、Promethei部署
环境准备:
| 主机名 | IP | 角色 |
|---|---|---|
| prometheus‑server | 192.168.18.132 | Prometheus服务端、Grafana |
| prometheus‑agent | 192.168.18.133 | 被监控agent节点,部署node_exporter |
端口说明
- Prometheus:9090
- node_exporter:9100
- Grafana:3000
三、部署Prometheus Server(二进制)
3.1 源码包下载
可从github上下载,这里直接拖入安装包:
解压并移动安装目录
bash
[root@prometheus-server ~]# tar zxvf prometheus-3.5.1.linux-amd64.tar.gz
[root@prometheus-server ~]# mv prometheus-3.5.1.linux-amd64 /usr/local/src/prometheus
[root@prometheus-server ~]# cd /usr/local/src/prometheus/
[root@prometheus-server prometheus]# ls
LICENSE NOTICE prometheus prometheus.yml promtool
3.2 修改prometheus.yml配置文件
bash
[root@prometheus-server prometheus]# vim prometheus.yml
修改抓取配置,监控prometheus自身服务
yaml
...
- job_name: "prometheus-server" #23行 监控本地节点名称
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["192.168.18.132:9090"] #29行 设置本地节点IP
3.3 临时nohup启动
bash
[root@prometheus-server prometheus]# nohup ./prometheus --config.file=prometheus.yml &
[1] 37685
[root@prometheus-server prometheus]# nohup: 忽略输入并把输出追加到"nohup.out"
查看端口
bash
[root@prometheus-server prometheus]# ps -ef | grep "prometheus"
root 2138 1903 0 09:43 pts/0 00:00:00 ./prometheus --
config.file=prometheus.yml
生产环境不要使用nohup,使用systemd托管。先kill测试进程。
bash
[root@prometheus-server prometheus]# kill -9 $(pidof prometheus)
3.4 使用systemd管理prometheus服务
编写service单元文件
bash
[root@prometheus-server prometheus]# vim /usr/lib/systemd/system/prometheus.service
[Unit]
Description=prometheus
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/src/prometheus/prometheus --config.file=/usr/local/src/prometheus/prometheus.yml
[Install]
WantedBy=multi-user.target
重载systemd,开机自启并启动服务
bash
[root@prometheus-server prometheus]# systemctl daemon-reload
[root@prometheus-server prometheus]# systemctl enable prometheus.service --now
[root@prometheus-server prometheus]# systemctl status prometheus.service
关闭防火墙与selinux(实验环境)
bash
[root@prometheus-server prometheus]# systemctl disable firewalld --now
[root@prometheus-server prometheus]# setenforce 0
3.5 访问Prometheus 网站
浏览器访问:http://192.168.18.132:9090

- Status → Target health 查看监控目标状态----查看到本节点被监控;

- 点击metrics查看指标数据

四、Prometheus监控主机
node_exporter专门采集Linux主机指标:CPU、内存、磁盘、inode、网络、负载,对外暴露
/metrics接口,Prometheus通过9100端口拉取。
4.1 使用exporter监控本地节点
bash
[root@prometheus-server~]# tarzxvfnode_exporter-1.10.2.linux-amd64.tar.gz
[root@prometheus-server~]# mv node_exporter-1.10.2.linux-amd64 /usr/local/src/node_exporter
[root@prometheus-server~]# cd usr/local/src/node_exporter/
[root@prometheus-server node_exporter]# ls
LICENSE node_exporter NOTICE
创建systemd服务,添加service文件
bash
[root@prometheus-server ~]# vim /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=node_exporter
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/src/node_exporter/node_exporter
[Install]
WantedBy=multi-user.target
启动服务
bash
[root@prometheus-server ~]# systemctl daemon-reload
[root@prometheus-server ~]# systemctl enable node_exporter.service --now
[root@prometheus-server ~]# systemctl status node_exporter.service
修改prometheus.yml增加监控job
bash
[root@prometheus-server ~]# vim /usr/local/src/prometheus/prometheus.yml
末尾添加
yaml
- job_name: "node_exporter_prometheus-server" #名称
static_configs:
- targets: ["192.168.18.132:9100"] #端口9100
重启prometheus加载配置
bash
[root@prometheus-server ~]# systemctl restart prometheus.service
打开web页面查看target监控到目标:Status‑>Target health,确认状态UP。

4.2 使用exporter监控其他节点
在agent机器执行
关闭防火墙selinux
bash
[root@agent ~]# systemctl disable firewalld --now
[root@agent ~]# setenforce 0
解压软件包
bash
[root@agent ~]# tar zxvf node_exporter-1.10.2.linux-amd64.tar.gz
[root@agent ~]# mv node_exporter-1.10.2.linux-amd64 /usr/local/src/node_exporter
添加service文件
bash
[root@agent ~]# vim /usr/lib/systemd/system/node_exporter.service
[Service]
ExecStart=/usr/local/src/node_exporter/node_exporter
[Install]
WantedBy=multi-user.target
[Unit]
Description=node_exporter
After=network.target
启动服务
bash
[root@agent ~]# systemctl daemon-reload
[root@agent ~]# systemctl enable node_exporter.service --now
[root@agent ~]# systemctl status node_exporter.service
回到prometheus‑server服务器,修改prometheus.yml,新增agent节点job
bash
[root@prometheus-server ~]# vim /usr/local/src/prometheus/prometheus.yml
...末尾添加
- job_name: "node_exporter_agent"
static_configs:
- targets: ["192.168.18.133:9100"]
重启prometheus服务
bash
[root@prometheus-server ~]# systemctl restart prometheus.service
刷新Target页面,可以看到两台主机全部UP。

五、Grafana可视化
Grafana 是一个开源的可视化和监控工具 ,能把各种数据源的指标和日志,统一展示在酷炫的仪表盘上,特别适合监控系统状态和分析业务数据。
核心功能
-
数据统一:不用把数据都导入一个地方,它能直接连上现有数据源,比如云服务、数据库甚至Google Sheets,统一可视化。
-
灵活仪表盘:支持多种图表类型(折线图、热力图等),还能自定义查询和告警,团队协作和分享也方便。
-
多数据源支持:兼容 Prometheus、MySQL、Elasticsearch 等主流数据源。
5.1 部署Grafana
将软件包拖入新节点,注意关闭防火墙
bash
[root@localhost ~]# tar zxvf grafana_12.3.1_20271043721_linux_amd64.tar.gz
[root@localhost ~]# mv grafana-12.3.1/ /usr/local/src/grafana
临时nohup测试启动
bash
[root@localhost ~]# nohup /usr/local/src/grafana/bin/grafana-server --config=/usr/local/src/grafana/conf/defaults.ini --homepath=/usr/local/src/grafana &
[1] 50437
[root@localhost ~]# nohup: 忽略输入并把输出追加到"nohup.out"
[root@localhost ~]# ps -ef | grep grafana
root 50437 48685 13 17:41 pts/2 00:00:03 grafana server --
config=/usr/local/src/grafana/conf/defaults.ini --
homepath=/usr/local/src/grafana
root 50452 48685 0 17:41 pts/2 00:00:00 grep --color=auto grafana
验证3000端口监听
bash
[root@localhost ~]# ss -anput | grep 3000
tcp LISTEN 0 128 :::3000 :::*
users:(("grafana",pid=50437,fd=15))
kill测试进程,改用systemd托管
bash
[root@localhost ~]# kill -9 50437
5.2 使用systemd管理grafana
bash
[root@localhost ~]# vim /usr/lib/systemd/system/grafana.service
[Unit]
Description=grafana
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/src/grafana/bin/grafana-server --config=/usr/local/src/grafana/conf/defaults.ini --homepath=/usr/local/src/grafana
[Install]
WantedBy=multi-user.target
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl enable grafana.service --now
[root@localhost ~]# systemctl status grafana.service
浏览器访问grafana页面http://192.168.18.132:3000
默认账号密码:admin / admin,可选择跳过修改密码。

5.3 添加Prometheus数据源
- 左侧菜单 Connections → Data sources → Add data source;

- 选择
Prometheus时序数据库; - URL填写prometheus地址:
[http://192.168.18.132:9090](http://192.168.18.132:9090); - 点击
Save & test,提示Successfully queried the Prometheus API.代表连通成功。
5.4 导入社区主机监控模板(ID:8919)
Grafana社区模板ID:8919,专门用于node‑exporter主机监控大盘。
操作步骤:
- Dashboards → New → Import;
- 在输入框填入ID
8919,点击Load加载模板; - 在导入页面,选择刚刚配置好的prometheus数据源;
- 点击Import完成导入。
导入成功后,就可以看到CPU、内存、磁盘、网络、负载全套主机监控图表。 截图自行插入:导入完成后的大盘界面。

六、常见排错清单
- Prometheus Target状态DOWN
- 检查被监控端防火墙放行端口;
- 确认exporter进程正常运行;
- 确认prometheus.yml中target的IP和端口填写正确;
- 访问
http://ip:9100/metrics看是否返回指标文本。
- Grafana Save & test报错无法连接prometheus
- 确认prometheus服务9090端口监听;
- 防火墙放行9090端口;
- 数据源URL不要写localhost,填写服务器实际内网IP。
- systemd启动失败
- 使用
journalctl -u prometheus -f查看详细报错; - promtool校验配置语法:
./promtool check config prometheus.yml。
七、后续拓展方向
- 编写alert告警规则,对接Alertmanager实现邮件/钉钉告警;
- 学习PromQL常用查询语法;
- 学习file_sd/k8s服务发现动态管理target;
- 监控mysql、redis等中间件,使用对应exporter。
完整实验链路总结: node‑exporter采集主机指标 → Prometheus定时pull抓取存储时序数据 → Grafana接入数据源导入大盘做可视化展示。
est报错无法连接prometheus
- 确认prometheus服务9090端口监听;
- 防火墙放行9090端口;
- 数据源URL不要写localhost,填写服务器实际内网IP。
- systemd启动失败
- 使用
journalctl -u prometheus -f查看详细报错; - promtool校验配置语法:
./promtool check config prometheus.yml。
七、后续拓展方向
- 编写alert告警规则,对接Alertmanager实现邮件/钉钉告警;
- 学习PromQL常用查询语法;
- 学习file_sd/k8s服务发现动态管理target;
- 监控mysql、redis等中间件,使用对应exporter。
完整实验链路总结: node‑exporter采集主机指标 → Prometheus定时pull抓取存储时序数据 → Grafana接入数据源导入大盘做可视化展示。