Prometheus 监控采集与可视化
一、Prometheus 监控体系介绍
Prometheus 是云原生领域主流的开源监控告警方案,组件包括 Prometheus、Grafana、Alertmanager
- Prometheus(存储):核心采集器,负责监控指标的采集、存储与查询
- Grafana(展示):可视化面板,将指标数据转化为直观图表展示
- Alertmanager(告警):告警中枢,实现告警分级、抑制、聚合与多渠道分发
三者联动,构建数据采集 - 可视化 - 告警响应的闭环监控体系
二、Prometheus
1.1. 安装
https://prometheus.io/download/
获取安装包


下载到电脑再传到服务器,或者直接在服务器下载,然后解压缩
wget https://github.com/prometheus/prometheus/releases/download/v3.11.3/prometheus-3.11.3.linux-amd64.tar.gz
[root@server1 ~]# ls
anaconda-ks.cfg
init_centos9.sh
prometheus-3.11.3.linux-amd64.tar.gz
[root@server1 ~]# tar -xvf prometheus-3.11.3.linux-amd64.tar.gz
prometheus-3.11.3.linux-amd64/
prometheus-3.11.3.linux-amd64/LICENSE
prometheus-3.11.3.linux-amd64/NOTICE
prometheus-3.11.3.linux-amd64/promtool
prometheus-3.11.3.linux-amd64/prometheus
prometheus-3.11.3.linux-amd64/prometheus.yml
[root@server1 ~]# ls
alertmanager-0.32.1.linux-amd64.tar.gz
anaconda-ks.cfg
init_centos9.sh
prometheus-3.11.3.linux-amd64
prometheus-3.11.3.linux-amd64.tar.gz
[root@server1 ~]# mv prometheus-3.11.3.linux-amd64 /usr/local/prometheus
[root@server1 ~]# cd /usr/local/prometheus
[root@server1 prometheus]# ll
总用量 410948
drwxr-xr-x 2 1001 1001 91 4月 27 23:21 ./
drwxr-xr-x. 13 root root 149 5月 5 21:21 ../
-rw-r--r-- 1 1001 1001 11357 4月 27 23:16 LICENSE
-rw-r--r-- 1 1001 1001 3942 4月 27 23:16 NOTICE
-rwxr-xr-x 1 1001 1001 222840596 4月 27 22:49 prometheus*
-rw-r--r-- 1 1001 1001 1093 4月 27 23:16 prometheus.yml
-rwxr-xr-x 1 1001 1001 197943304 4月 27 22:49 promtool*
[root@server1 prometheus]# wc -l prometheus.yml
32 prometheus.yml
[root@server1 prometheus]# grep 9090 prometheus.yml
- targets: ["localhost:9090"]
[root@server1 prometheus]# cat prometheus.yml
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["localhost:9090"]
# The label name is added as a label `label_name=<label_value>` to any timeseries scraped from this config.
labels:
app: "prometheus"
[root@server1 prometheus]#
cat > prometheus.yml <<EOF
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["localhost:9090"]
# The label name is added as a label `label_name=<label_value>` to any timeseries scraped from this config.
labels:
app: "prometheus"
EOF
# 用 cat 命令创建/覆盖 prometheus.yml 配置文件,内容从 EOF 开始到下一个 EOF 结束
cat > prometheus.yml <<EOF
# ====================== 全局配置段 ======================
# 全局配置:对整个 Prometheus 生效的基础设置
global:
# 数据采集间隔:每 15 秒去目标机器拉取一次监控指标
scrape_interval: 15s
# 规则评估间隔:每 15 秒检查一次告警规则/记录规则
evaluation_interval: 15s
# 采集超时时间:未手动配置,使用默认值 10 秒
# ====================== 告警管理器配置 ======================
# 告警配置:关联 Prometheus 与 AlertManager(告警通知组件)
alerting:
alertmanagers:
- static_configs:
- targets:
# 告警管理器地址:当前已注释,未启用告警通知
# - alertmanager:9093
# ====================== 规则文件配置 ======================
# 规则文件:加载告警规则、聚合指标规则的配置文件列表
rule_files:
# 自定义规则文件1(已注释,未启用)
# - "first_rules.yml"
# 自定义规则文件2(已注释,未启用)
# - "second_rules.yml"
# ====================== 监控采集任务配置 ======================
# 采集任务:定义 Prometheus 要监控哪些服务、怎么采集
scrape_configs:
# 任务名称:任务名会自动变成标签 job=prometheus
- job_name: "prometheus"
# 指标采集路径:默认 /metrics,已省略不写
# metrics_path defaults to '/metrics'
# 访问协议:默认 http,已省略不写
# scheme defaults to 'http'.
# 静态配置:直接写死监控目标地址(不使用服务发现)
static_configs:
# 监控目标:本机 9090 端口(即 Prometheus 自己)
- targets: ["localhost:9090"]
# 自定义标签:给这条监控数据额外打上 app=prometheus 标签
labels:
app: "prometheus"
EOF
1.2. 启动
[root@server1 prometheus]# pwd
/usr/local/prometheus
[root@server1 prometheus]# ./prometheus --config.file=prometheus.yml
time=2026-05-05T21:26:33.613+08:00 level=INFO source=main.go:1678 msg="updated GOGC" old=100 new=75
time=2026-05-05T21:26:33.613+08:00 level=INFO source=main.go:744 msg="Leaving GOMAXPROCS=2: CPU quota undefined" component=automaxprocs
time=2026-05-05T21:26:33.613+08:00 level=INFO source=memlimit.go:198 msg="GOMEMLIMIT is updated" component=automemlimit package=github.com/KimMachineGun/automemlimit/memlimit GOMEMLIMIT=1643260723 previous=9223372036854775807
time=2026-05-05T21:26:33.613+08:00 level=INFO source=main.go:851 msg="Starting Prometheus Server" mode=server version="(version=3.11.3, branch=HEAD, revision=eb173f5256d4022afba1e9bc3d19740a76859fae)"
time=2026-05-05T21:26:33.614+08:00 level=INFO source=main.go:856 msg="operational information" build_context="(go=go1.26.2, platform=linux/amd64, user=root@83aad33dd38e, date=20260427-14:45:32, tags=netgo,builtinassets)" host_details="(Linux 5.14.0-511.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Sep 19 06:52:39 UTC 2024 x86_64 server1 (none))" fd_limits="(soft=524287, hard=524288)" vm_limits="(soft=unlimited, hard=unlimited)"
time=2026-05-05T21:26:33.862+08:00 level=INFO source=web.go:710 msg="Start listening for connections" component=web address=0.0.0.0:9090
time=2026-05-05T21:26:33.863+08:00 level=INFO source=main.go:1410 msg="Starting TSDB ..."
time=2026-05-05T21:26:33.865+08:00 level=INFO source=tls_config.go:354 msg="Listening on" component=web address=[::]:9090
time=2026-05-05T21:26:33.865+08:00 level=INFO source=tls_config.go:357 msg="TLS is disabled." component=web http2=false address=[::]:9090
time=2026-05-05T21:26:33.869+08:00 level=INFO source=head.go:698 msg="Replaying on-disk memory mappable chunks if any" component=tsdb
time=2026-05-05T21:26:33.869+08:00 level=INFO source=head.go:784 msg="On-disk memory mappable chunks replay completed" component=tsdb duration=1.155µs
time=2026-05-05T21:26:33.869+08:00 level=INFO source=head.go:792 msg="Replaying WAL, this may take a while" component=tsdb
time=2026-05-05T21:26:33.870+08:00 level=INFO source=head.go:865 msg="WAL segment loaded" component=tsdb segment=0 maxSegment=0 duration=260.464µs
time=2026-05-05T21:26:33.870+08:00 level=INFO source=head.go:902 msg="WAL replay completed" component=tsdb checkpoint_replay_duration=30.332µs wal_replay_duration=301.02µs wbl_replay_duration=95ns chunk_snapshot_load_duration=0s mmap_chunk_replay_duration=1.155µs total_replay_duration=848.752µs
time=2026-05-05T21:26:33.871+08:00 level=INFO source=main.go:1431 msg="filesystem information" fs_type=XFS_SUPER_MAGIC
time=2026-05-05T21:26:33.871+08:00 level=INFO source=main.go:1434 msg="TSDB started"
time=2026-05-05T21:26:33.871+08:00 level=INFO source=main.go:1632 msg="Loading configuration file" filename=prometheus.yml
time=2026-05-05T21:26:33.872+08:00 level=INFO source=main.go:1048 msg="TSDB retention updated" duration=15d size=0B percentage=0
time=2026-05-05T21:26:33.874+08:00 level=INFO source=main.go:1671 msg="Completed loading of configuration file" db_storage=57.15µs remote_storage=1.503µs web_handler=501ns query_engine=983ns scrape=1.909135ms scrape_sd=77.374µs notify=98.048µs notify_sd=11.431µs rules=1.24µs tracing=3.03µs filename=prometheus.yml totalDuration=2.405738ms
time=2026-05-05T21:26:33.874+08:00 level=INFO source=main.go:1395 msg="Server is ready to receive web requests."
time=2026-05-05T21:26:33.874+08:00 level=INFO source=manager.go:209 msg="Starting rule manager..." component="rule manager"
http://192.168.80.11:9090/query

1.3. 配置
记得执行 Ctrl + C 停止之前的 prometheus 进程
推荐用 systemd 来管理 prometheus
[root@server1 prometheus]# groupadd prometheus
[root@server1 prometheus]# useradd -r -g prometheus -s /bin/false prometheus
[root@server1 prometheus]# chown -R prometheus:prometheus /usr/local/prometheus
[root@server1 prometheus]# cd
[root@server1 ~]# cat > /etc/systemd/system/prometheus.service <<EOF
[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io/docs/introduction/overview/
After=network-online.target
[Service]
User=prometheus
Group=prometheus
Restart=on-failure
ExecStart=/usr/local/prometheus/prometheus \
--config.file=/usr/local/prometheus/prometheus.yml \
--storage.tsdb.path=/usr/local/prometheus/data \
--storage.tsdb.retention.time=7d \
--web.enable-lifecycle \
--web.enable-admin-api
[Install]
WantedBy=multi-user.target
EOF
[root@server1 ~]# systemctl daemon-reload
[root@server1 ~]# systemctl restart prometheus
systemctl enable prometheus
systemctl status prometheus
Created symlink /etc/systemd/system/multi-user.target.wants/prometheus.service → /etc/systemd/system/prometheus.service.
● prometheus.service - Prometheus Server
Loaded: loaded (/etc/systemd/system/prometheus.service; enabled; preset: d>
Active: active (running) since Tue 2026-05-05 21:29:29 CST; 283ms ago
Docs: https://prometheus.io/docs/introduction/overview/
Main PID: 1504 (prometheus)
Tasks: 5 (limit: 10867)
Memory: 37.1M
CPU: 228ms
CGroup: /system.slice/prometheus.service
└─1504 /usr/local/prometheus/prometheus --config.file=/usr/local/p>
5月 05 21:29:29 server1 systemd[1]: Started Prometheus Server.
5月 05 21:29:29 server1 prometheus[1504]: time=2026-05-05T21:29:29.771+08:00 le>
5月 05 21:29:29 server1 prometheus[1504]: time=2026-05-05T21:29:29.771+08:00 le>
5月 05 21:29:29 server1 prometheus[1504]: time=2026-05-05T21:29:29.772+08:00 le>
5月 05 21:29:29 server1 prometheus[1504]: time=2026-05-05T21:29:29.772+08:00 le>
5月 05 21:29:29 server1 prometheus[1504]: time=2026-05-05T21:29:29.772+08:00 le>
[root@server1 ~]#
# ====================== 1. 创建专用用户和用户组 ======================
# 创建一个名叫 prometheus 的用户组(用于权限分组管理)
groupadd prometheus
# 创建一个 系统专用用户 prometheus:
# -r:系统用户(不用于登录)
# -g prometheus:归到 prometheus 组
# -s /bin/false:禁止登录服务器(安全加固)
useradd -r -g prometheus -s /bin/false prometheus
# 把 /usr/local/prometheus 目录的所有者、所属组 改成 prometheus
# 让 Prometheus 进程有权限读写自己的目录、数据文件
chown -R prometheus:prometheus /usr/local/prometheus
# ====================== 2. 创建 systemd 服务文件 ======================
# 用 cat 命令创建 prometheus.service 服务文件(让系统能管理 Prometheus)
cat > /etc/systemd/system/prometheus.service <<EOF
# 服务描述段
[Unit]
# 服务名称描述
Description=Prometheus Server
# 官方文档地址
Documentation=https://prometheus.io/docs/introduction/overview/
# 启动顺序:等网络准备好再启动 Prometheus
After=network-online.target
# 服务运行参数
[Service]
# 以 prometheus 用户运行
User=prometheus
# 以 prometheus 用户组运行
Group=prometheus
# 服务异常崩溃时,自动重启
Restart=on-failure
# 核心:启动 Prometheus 的命令
ExecStart=/usr/local/prometheus/prometheus \
# 指定配置文件路径
--config.file=/usr/local/prometheus/prometheus.yml \
# 指定时序数据存储目录
--storage.tsdb.path=/usr/local/prometheus/data \
# 数据保留时间:只保留 7 天(超过自动删除)
--storage.tsdb.retention.time=7d \
# 开启热重载功能(改配置不用停服务)
--web.enable-lifecycle \
# 开启管理 API(用于清理数据、调试)
--web.enable-admin-api
# 系统启动级别
[Install]
# 让服务在系统多用户模式下自动启动(开机自启)
WantedBy=multi-user.target
EOF
# ====================== 3. 加载服务并启动 Prometheus ======================
# 重新加载 systemd 配置(让系统识别新添加的服务文件)
systemctl daemon-reload
# 重启 Prometheus 服务(第一次执行=启动)
systemctl restart prometheus
# 设置开机自启(服务器重启后自动跑 Prometheus)
systemctl enable prometheus
# 查看 Prometheus 运行状态(看是否成功启动、有无报错)
systemctl status prometheus
注意:防火墙和SELinux
sed -i -r 's/SELINUX=[ep].*/SELINUX=disabled/g' /etc/selinux/config
# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config &> /dev/null
setenforce 0
systemctl stop firewalld &> /dev/null
systemctl disable firewalld &> /dev/null
iptables -F
1.4. Prometheus Web
打开 Prometheus Web 界面,地址 http://IP:9090/
1.5. PromQL 查询
完成一次 PromQL 查询

查询一分钟的数据

1.6. 其他页面
- 告警规则
未来配置的告警规则显示在这里

- 采集指标健康度

- 告警规则健康度

- 服务发现

- 运行时和版本信息

- Prometheus 健康度

- 命令行参数

- 当前配置文件

- alertmanager 服务发现

服务器监控数据采集
服务器指硬件本身,与服务无关系,包括 CPU 内存 磁盘 等
1.1. node_exporter
Prometheus 官方出品的采集服务器相关指标的组件
1.1.1. 安装
获取安装包

同样,在线下载安装包或者本地拖动安装包
# 下载 Node Exporter 二进制包(用于采集 Linux 服务器的硬件/系统监控指标)
curl -LO https://github.com/prometheus/node_exporter/releases/download/v1.10.2/node_exporter-1.10.2.linux-amd64.tar.gz
或者
# 拖动本地压缩包到Linux服务器
node_exporter-1.10.2.linux-amd64.tar.gz
1.1.2. 启动
启动 node_exporter,后台启动方式
# 1. 解压 Node Exporter 压缩包
# x=解压 v=显示过程 f=指定文件
tar xvf node_exporter-1.10.2.linux-amd64.tar.gz
# 2. 进入解压后的目录
cd node_exporter-1.10.2.linux-amd64/
# 3. 把 node_exporter 可执行文件 移动到系统命令目录
# 这样在任何目录都能直接运行 node_exporter
mv node_exporter /usr/local/bin
# 4. 查看 node_exporter 命令所在路径
# 验证是否安装成功:输出 /usr/local/bin/node_exporter 就是成功
which node_exporter
# 5. 后台启动 Node Exporter
# nohup = 让程序脱离终端,关闭窗口也不会停
# & = 后台运行
nohup node_exporter &
# 6. 查看 Node Exporter 是否在运行
# ps aux 列出所有进程
# grep node_exporter 过滤出这个程序
ps aux|grep node_exporter
# 7. 查看 node_exporter 占用的端口
# 能看到 9100 端口正在被监听
netstat -pantul|grep node_exporter
# 8. 直接用端口号 9100 检查监听状态
# Node Exporter 默认端口 = 9100
netstat -pantul|grep 9100
[root@server1 ~]# ls
anaconda-ks.cfg
init_centos9.sh
node_exporter-1.11.1.linux-amd64.tar.gz
prometheus-3.11.3.linux-amd64.tar.gz
[root@server1 ~]# tar xvf node_exporter-1.11.1.linux-amd64.tar.gz
node_exporter-1.11.1.linux-amd64/
node_exporter-1.11.1.linux-amd64/LICENSE
node_exporter-1.11.1.linux-amd64/node_exporter
node_exporter-1.11.1.linux-amd64/NOTICE
[root@server1 ~]# ls
anaconda-ks.cfg
init_centos9.sh
node_exporter-1.11.1.linux-amd64
node_exporter-1.11.1.linux-amd64.tar.gz
prometheus-3.11.3.linux-amd64.tar.gz
[root@server1 ~]# cd node_exporter-1.11.1.linux-amd64/
[root@server1 node_exporter-1.11.1.linux-amd64]# ls
LICENSE node_exporter NOTICE
[root@server1 node_exporter-1.11.1.linux-amd64]# mv node_exporter /usr/local/bin
[root@server1 node_exporter-1.11.1.linux-amd64]# ls
LICENSE NOTICE
[root@server1 node_exporter-1.11.1.linux-amd64]# ls /usr/local/bin/
node_exporter
[root@server1 node_exporter-1.11.1.linux-amd64]# cd
[root@server1 ~]# which node_exporter
/usr/local/bin/node_exporter
[root@server1 ~]# nohup node_exporter &
[1] 1567
[root@server1 ~]# nohup: 忽略输入并把输出追加到'nohup.out'
[root@server1 ~]# ps aux|grep node_exporter
root 1567 0.1 0.9 1275020 17716 pts/0 Sl 21:49 0:00 node_exporter
root 1574 0.0 0.1 6640 2176 pts/0 S+ 21:49 0:00 grep --color=auto node_exporter
[root@server1 ~]# netstat -pantul|grep node_exporter
tcp6 0 0 :::9100 :::* LISTEN 1567/node_exporter
[root@server1 ~]# netstat -pantul|grep 9100
tcp6 0 0 :::9100 :::* LISTEN 1567/node_exporter
[root@server1 ~]#
1.1.3. 查看/验证
注意:防火墙和SELinux
sed -i -r 's/SELINUX=[ep].*/SELINUX=disabled/g' /etc/selinux/config
# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config &> /dev/null
setenforce 0
systemctl stop firewalld &> /dev/null
systemctl disable firewalld &> /dev/null
iptables -F
查看 node_exporter 采集的内容,打开http://IP:9100/ ,点击 metrics


搜索 "node_cpu"

这就是 node_exporter 所采集到的 metrics(指标)
1.2. 配置 Prometheus
1.2.1. 采集 node_exporter
[root@server1 ~]# find / -name prometheus.yml
/usr/local/prometheus/prometheus.yml
[root@server1 ~]# vim /usr/local/prometheus/prometheus.yml
[root@server1 ~]# cat /usr/local/prometheus/prometheus.yml
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["localhost:9090"]
# The label name is added as a label `label_name=<label_value>` to any timeseries scraped from this config.
labels:
app: "prometheus"
# node_exporter
- job_name: "node"
static_configs:
- targets: ['192.168.80.12:9100','192.168.80.13:9100']
labels:
env: 'test'
[root@server1 ~]#
重载配置
[root@server1 ~]# ps -ef | grep prometheus
prometh+ 1504 1 0 21:29 ? 00:00:01 /usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/usr/local/prometheus/data --storage.tsdb.retention.time=7d --web.enable-lifecycle --web.enable-admin-api
root 1588 1374 0 21:55 pts/0 00:00:00 grep --color=auto prometheus
[root@server1 ~]# kill -1 1504 # 重载 -1 SIGHUP
[root@server1 ~]# curl -ksvvXPOST http://192.168.80.11:9090/-/reload # 重载 URL reload
* Trying 192.168.80.11:9090...
* Connected to 192.168.80.11 (192.168.80.11) port 9090 (#0)
> POST /-/reload HTTP/1.1
> Host: 192.168.80.11:9090
> User-Agent: curl/7.76.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Tue, 05 May 2026 13:56:06 GMT
< Content-Length: 0
<
* Connection #0 to host 192.168.80.11 left intact
[root@server1 ~]#
命令说明:
使用 -s 避免输出干扰,同时用 -vv 打印详细的请求/响应信息,便于观察重载是否成功。
若 Prometheus 开启了 HTTPS 且使用自签名证书,-k 可忽略证书验证,但此处为 HTTP 连接,-k 无实际影响。
或者重启 prometheus
systemctl restart prometheus
systemctl status prometheus
1.2.2. 验证
打开http://192.168.80.11:9090/targets查看

1.2.3. 添加节点
添加两个被监控节点
# 把本机 /usr/local/bin 下的 node_exporter 二进制文件
# 远程拷贝到 192.168.80.12 机器的 /usr/local/bin 目录
scp /usr/local/bin/node_exporter 192.168.80.12:/usr/local/bin/
# 同理:拷贝到第二台被监控节点 192.168.80.13
scp /usr/local/bin/node_exporter 192.168.80.13:/usr/local/bin/
启动 node_exporter(在被监控的机器上执行)
nohup node_exporter &
注意:防火墙和SELinux
sed -i -r 's/SELINUX=[ep].*/SELINUX=disabled/g' /etc/selinux/config
# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config &> /dev/null
setenforce 0
systemctl stop firewalld &> /dev/null
systemctl disable firewalld &> /dev/null
iptables -F
最终效果

三、Grafana
1. 下载 Grafana 安装包
获取安装包下载链接 https://grafana.com/grafana/download?pg=get&edition=oss


wget https://dl.grafana.com/grafana/release/12.4.0/grafana_12.4.0_22325204712_linux_amd64.tar.gz
tar -zxvf grafana_12.4.0_22325204712_linux_amd64.tar.gz
2. 安装 Grafana
注意:在 Prometheus 所在的服务器安装 Grafana
groupadd grafana
useradd -r -g grafana -s /bin/false grafana
tar xvf grafana_12.4.0_22325204712_linux_amd64.tar.gz
mv grafana-12.4.0 /usr/local/grafana
cp /usr/local/grafana/conf/defaults.ini /usr/local/grafana/conf/grafana.ini
chown -R grafana:grafana /usr/local/grafana
cat > /etc/systemd/system/grafana.service <<EOF
[Unit]
Description=Grafana Server
After=network.target
[Service]
Type=simple
User=grafana
Group=grafana
ExecStart=/usr/local/grafana/bin/grafana server --config=/usr/local/grafana/conf/grafana.ini --homepath=/usr/local/grafana
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
# 启动
systemctl daemon-reload
systemctl start grafana
systemctl enable grafana
systemctl status grafana
netstat -pantul|grep grafana
注意:防火墙和SELinux
sed -i -r 's/SELINUX=[ep].*/SELINUX=disabled/g' /etc/selinux/config
# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config &> /dev/null
setenforce 0
systemctl stop firewalld &> /dev/null
systemctl disable firewalld &> /dev/null
iptables -F
# ====================== 1. 创建专用用户(安全加固) ======================
# 创建 grafana 用户组
groupadd grafana
# 创建系统用户 grafana
# -r:系统用户
# -g grafana:归属 grafana 组
# -s /bin/false:禁止登录服务器(更安全)
useradd -r -g grafana -s /bin/false grafana
# ====================== 2. 解压并安装 Grafana ======================
# 解压 Grafana 压缩包
tar xvf grafana_12.4.0_22325204712_linux_amd64.tar.gz
# 把解压后的文件夹移动到 /usr/local/grafana(标准安装目录)
mv grafana-12.4.0 /usr/local/grafana
# 复制默认配置文件(用默认配置启动,后续可自定义修改)
cp /usr/local/grafana/conf/defaults.ini /usr/local/grafana/conf/grafana.ini
# 把 Grafana 整个目录的权限交给 grafana 用户(让程序能正常读写)
chown -R grafana:grafana /usr/local/grafana
# ====================== 3. 创建 systemd 服务文件 ======================
# 创建 grafana 系统服务,实现开机自启、异常自动重启
cat > /etc/systemd/system/grafana.service <<EOF
[Unit]
# 服务描述
Description=Grafana Server
# 等待网络启动后再运行
After=network.target
[Service]
# 简单服务类型
Type=simple
# 使用 grafana 用户运行
User=grafana
# 使用 grafana 组运行
Group=grafana
# 启动 Grafana 的命令
# --config:指定配置文件
# --homepath:指定安装根目录
ExecStart=/usr/local/grafana/bin/grafana server --config=/usr/local/grafana/conf/grafana.ini --homepath=/usr/local/grafana
# 服务崩溃自动重启
Restart=on-failure
[Install]
# 开机自动启动
WantedBy=multi-user.target
EOF
# ====================== 4. 刷新配置与配置开机自启 ======================
# 重新加载 systemd 配置(让系统识别新服务)
systemctl daemon-reload
# 启动 Grafana
systemctl start grafana
# 设置开机自启
systemctl enable grafana
# 查看运行状态(检查是否成功启动)
systemctl status grafana
# 检查 Grafana 监听端口(默认 3000)
netstat -pantul|grep grafana
[root@server1 ~]# tar -xf grafana_12.4.3_24388279614_linux_amd64.tar.gz
[root@server1 ~]# groupadd grafana
[root@server1 ~]# useradd -r -g grafana -s /bin/false grafana
[root@server1 ~]# mv grafana-12.4.3 /usr/local/grafana
[root@server1 ~]# cp /usr/local/grafana/conf/defaults.ini /usr/local/grafana/conf/grafana.ini
[root@server1 ~]# chown -R grafana:grafana /usr/local/grafana
[root@server1 ~]# cat > /etc/systemd/system/grafana.service <<EOF
[Unit]
Description=Grafana Server
After=network.target
[Service]
Type=simple
User=grafana
Group=grafana
ExecStart=/usr/local/grafana/bin/grafana server --config=/usr/local/grafana/conf/grafana.ini --homepath=/usr/local/grafana
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
[root@server1 ~]# systemctl daemon-reload
[root@server1 ~]# systemctl start grafana
systemctl enable grafana
systemctl status grafana
Created symlink /etc/systemd/system/multi-user.target.wants/grafana.service → /etc/systemd/system/grafana.service.
● grafana.service - Grafana Server
Loaded: loaded (/etc/systemd/system/grafana.service; enabled; preset: disa>
Active: active (running) since Tue 2026-05-05 22:05:12 CST; 295ms ago
Main PID: 1683 (grafana)
Tasks: 5 (limit: 10867)
Memory: 32.6M
CPU: 183ms
CGroup: /system.slice/grafana.service
└─1683 /usr/local/grafana/bin/grafana server --config=/usr/local/g>
5月 05 22:05:12 server1 grafana[1683]: logger=migrator t=2026-05-05T22:05:12.91>
5月 05 22:05:12 server1 grafana[1683]: logger=migrator t=2026-05-05T22:05:12.91>
5月 05 22:05:12 server1 grafana[1683]: logger=migrator t=2026-05-05T22:05:12.91>
5月 05 22:05:12 server1 grafana[1683]: logger=migrator t=2026-05-05T22:05:12.91>
5月 05 22:05:12 server1 grafana[1683]: logger=migrator t=2026-05-05T22:05:12.92>
5月 05 22:05:12 server1 grafana[1683]: logger=migrator t=2026-05-05T22:05:12.92>
5月 05 22:05:12 server1 grafana[1683]: logger=migrator t=2026-05-05T22:05:12.92>
5月 05 22:05:12 server1 grafana[1683]: logger=migrator t=2026-05-05T22:05:12.92>
5月 05 22:05:12 server1 grafana[1683]: logger=migrator t=2026-05-05T22:05:12.93>
5月 05 22:05:12 server1 grafana[1683]: logger=migrator t=2026-05-05T22:05:12.93>
[root@server1 ~]#
3. 查看验证
浏览器打开http://192.168.80.11:3000/,登陆,默认账号/密码admin/admin

dashboard 列表

4. 数据源配置
左侧导航栏点击 Connections -> Data sources,然后点击中间的 Add data source

选择数据库类型,这里选择 Prometheus

填入 Prometheus 的 URL


滑到最下面,点击 “ Save & test”,出现 绿色的 Successfully 即为成功



5. Dashboard 开发
5.1. PromQL
Prometheus 提供、功能强大的查询语言,名为 PromQL,允许用户选择和聚合时间序列数据。
向 Prometheus 发送查询请求时,可以是即时查询(在特定时间点进行评估),也可以是范围查询(在起始时间和结束时间之间以等间隔步长进行评估)。
5.2. 基础开发
点击 Create dashboard 新建一个 dashboard

调整 metric
默认 Builder

调整图例

点击保存


实操:修改为百分百内存余量
记得点击 Code
(node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes) / node_memory_MemTotal_bytes

如果想以%号结尾,要找到
标准操作 Standard options
Unit -> Misc -> Percent (0-100)



实操:添加磁盘读写情况
(rate 函数)
统计最近 1 分钟内,真实物理磁盘 每秒磁盘读写操作次数 IOPS(Input/Output Operations Per Second)
rate(node_disk_reads_completed_total{device!="sr0", device!="dm-*"}[1m])

实操:不关注磁盘,只看整体。
(聚合函数)
以最近 1 分钟为计算时间窗口,基于磁盘累计写完成总次数,算出真实物理磁盘每秒写操作次数(写 IOPS);过滤排除光驱 sr0、LVM 映射盘、loop 循环设备、ram 内存盘,只统计真实物理硬盘。
sum(rate(node_disk_writes_completed_total{device!="sr0",device!~"dm.*|loop.*|ram.*"}[1m]))

拓展:监控指标 PromQL 查询
磁盘读取速率
rate(node_disk_reads_completed_total{device!="sr0", device!="dm-*"}[1m])
磁盘 IO 总操作数
rate(node_disk_io_time_seconds_total{device!="sr0", device!="dm-*"}[1m])
磁盘等待时间
rate(node_disk_io_time_weighted_seconds_total{device!="sr0", device!="dm-*"}[1m])
磁盘吞吐量(写入)
rate(node_disk_written_bytes_total{device!="sr0", device!="dm-*"}[1m])
磁盘吞吐量(读取)
rate(node_disk_read_bytes_total{device!="sr0", device!="dm-*"}[1m])
6. Dashboard 导入
官方 Dashboard 下载地址:https://grafana.com/grafana/dashboards/
搜索 node、server 等关键词

右侧复制 dashboard ID,或下载 json


打开 Dashboards 导入监控模板


填写 dashboard ID 或 json,然后点击对应 Load

或者

点击 Import

导入完成

四、Alertmanager
注意:在 Prometheus 所在的服务器上安装 Alertmanager
下载安装包

1. 安装
# 从 GitHub 官方下载 Alertmanager 0.31.1 版本安装包(告警管理器)
wget https://github.com/prometheus/alertmanager/releases/download/v0.31.1/alertmanager-0.31.1.linux-amd64.tar.gz
# 解压下载好的 Alertmanager 压缩包
tar xvf alertmanager-0.31.1.linux-amd64.tar.gz
# 将解压后的文件夹移动到 /usr/local/alertmanager 目录(统一安装路径)
mv alertmanager-0.31.1.linux-amd64 /usr/local/alertmanager
# 将整个目录的所有者和组设置为 prometheus 用户(保证程序有读写权限)
chown -R prometheus:prometheus /usr/local/alertmanager
[root@server1 ~]# ls
alertmanager-0.32.1.linux-amd64.tar.gz
anaconda-ks.cfg
grafana_12.4.3_24388279614_linux_amd64.tar.gz
init_centos9.sh
node_exporter-1.11.1.linux-amd64
node_exporter-1.11.1.linux-amd64.tar.gz
nohup.out
prometheus-3.11.3.linux-amd64.tar.gz
[root@server1 ~]# tar -xvf alertmanager-0.32.1.linux-amd64.tar.gz
alertmanager-0.32.1.linux-amd64/
alertmanager-0.32.1.linux-amd64/alertmanager
alertmanager-0.32.1.linux-amd64/alertmanager.yml
alertmanager-0.32.1.linux-amd64/LICENSE
alertmanager-0.32.1.linux-amd64/NOTICE
alertmanager-0.32.1.linux-amd64/amtool
[root@server1 ~]# ls
alertmanager-0.32.1.linux-amd64
alertmanager-0.32.1.linux-amd64.tar.gz
anaconda-ks.cfg
grafana_12.4.3_24388279614_linux_amd64.tar.gz
init_centos9.sh
node_exporter-1.11.1.linux-amd64
node_exporter-1.11.1.linux-amd64.tar.gz
nohup.out
prometheus-3.11.3.linux-amd64.tar.gz
[root@server1 ~]# mv alertmanager-0.32.1.linux-amd64 /usr/local/alertmanager
[root@server1 ~]# chown -R prometheus:prometheus /usr/local/alertmanager
[root@server1 ~]# ll /usr/local/alertmanager/
总用量 68324
drwxr-xr-x 2 prometheus prometheus 93 4月 30 01:42 ./
drwxr-xr-x. 15 root root 184 5月 5 22:18 ../
-rwxr-xr-x 1 prometheus prometheus 41262793 4月 30 01:35 alertmanager*
-rw-r--r-- 1 prometheus prometheus 559 4月 30 01:41 alertmanager.yml
-rwxr-xr-x 1 prometheus prometheus 28678189 4月 30 01:35 amtool*
-rw-r--r-- 1 prometheus prometheus 11357 4月 30 01:41 LICENSE
-rw-r--r-- 1 prometheus prometheus 311 4月 30 01:41 NOTICE
[root@server1 ~]# cat /usr/local/alertmanager/alertmanager.yml
route:
group_by: ['alertname']
group_wait: 30s
group_interval: 5m
repeat_interval: 1h
receiver: 'web.hook'
receivers:
- name: 'web.hook'
webhook_configs:
- url: 'http://127.0.0.1:5001/'
inhibit_rules:
- source_matchers: [severity="critical"]
target_matchers: [severity="warning"]
# Apply inhibition if the alertname is the same.
# CAUTION:
# If all label names listed in `equal` are missing
# from both the source and target alerts,
# the inhibition rule will apply!
equal: [alertname, dev, instance]
[root@server1 ~]#
2. 启动
# 添加 service 文件
cat > /etc/systemd/system/alertmanager.service <<EOF
[Unit]
Description=AlertManager
Documentation=https://prometheus.io/docs/alerting/latest/overview/
After=network-online.target
[Service]
User=prometheus
Group=prometheus
Restart=on-failure
ExecStart=/usr/local/alertmanager/alertmanager \
--config.file=/usr/local/alertmanager/alertmanager.yml \
--storage.path=/usr/local/alertmanager/data
[Install]
WantedBy=multi-user.target
EOF
# 启动
systemctl daemon-reload
systemctl start alertmanager
systemctl enable alertmanager
systemctl status alertmanager
netstat -pantul|grep alertmanager
# 创建 Alertmanager 系统服务文件,让系统可以开机自启、管理告警服务
cat > /etc/systemd/system/alertmanager.service <<EOF
[Unit]
# 服务描述:AlertManager 告警管理器
Description=AlertManager
# 官方文档地址
Documentation=https://prometheus.io/docs/alerting/latest/overview/
# 启动顺序:等网络就绪后再启动
After=network-online.target
[Service]
# 使用 prometheus 用户运行(安全,不用 root)
User=prometheus
# 使用 prometheus 用户组运行
Group=prometheus
# 服务异常崩溃时自动重启
Restart=on-failure
# 启动 Alertmanager 的命令
ExecStart=/usr/local/alertmanager/alertmanager \
# 指定告警配置文件(邮件、钉钉、企业微信等都在这里配置)
--config.file=/usr/local/alertmanager/alertmanager.yml \
# 指定告警数据存储目录(用于去重、缓存告警)
--storage.path=/usr/local/alertmanager/data
[Install]
# 系统启动级别:多用户模式下开机自启
WantedBy=multi-user.target
EOF
# 重新加载 systemd 配置,让系统识别新增的 alertmanager 服务
systemctl daemon-reload
# 启动 Alertmanager 服务
systemctl start alertmanager
# 设置 Alertmanager 开机自启
systemctl enable alertmanager
# 查看 Alertmanager 运行状态(检查是否启动成功)
systemctl status alertmanager
# 查看 Alertmanager 监听端口(默认 9093)
netstat -pantul|grep alertmanager
[root@server1 ~]# cat > /etc/systemd/system/alertmanager.service <<EOF
[Unit]
Description=AlertManager
Documentation=https://prometheus.io/docs/alerting/latest/overview/
After=network-online.target
[Service]
User=prometheus
Group=prometheus
Restart=on-failure
ExecStart=/usr/local/alertmanager/alertmanager \
--config.file=/usr/local/alertmanager/alertmanager.yml \
--storage.path=/usr/local/alertmanager/data
[Install]
WantedBy=multi-user.target
EOF
[root@server1 ~]# systemctl daemon-reload
[root@server1 ~]# systemctl start alertmanager
systemctl enable alertmanager
systemctl status alertmanager
Created symlink /etc/systemd/system/multi-user.target.wants/alertmanager.service → /etc/systemd/system/alertmanager.service.
● alertmanager.service - AlertManager
Loaded: loaded (/etc/systemd/system/alertmanager.service; enabled; preset:>
Active: active (running) since Tue 2026-05-05 22:20:13 CST; 232ms ago
Docs: https://prometheus.io/docs/alerting/latest/overview/
Main PID: 1790 (alertmanager)
Tasks: 7 (limit: 10867)
Memory: 12.5M
CPU: 75ms
CGroup: /system.slice/alertmanager.service
└─1790 /usr/local/alertmanager/alertmanager --config.file=/usr/loc>
5月 05 22:20:13 server1 systemd[1]: Started AlertManager.
5月 05 22:20:13 server1 alertmanager[1790]: time=2026-05-05T22:20:13.534+08:00 >
5月 05 22:20:13 server1 alertmanager[1790]: time=2026-05-05T22:20:13.534+08:00 >
5月 05 22:20:13 server1 alertmanager[1790]: time=2026-05-05T22:20:13.535+08:00 >
5月 05 22:20:13 server1 alertmanager[1790]: time=2026-05-05T22:20:13.536+08:00 >
5月 05 22:20:13 server1 alertmanager[1790]: time=2026-05-05T22:20:13.604+08:00 >
5月 05 22:20:13 server1 alertmanager[1790]: time=2026-05-05T22:20:13.605+08:00 >
5月 05 22:20:13 server1 alertmanager[1790]: time=2026-05-05T22:20:13.614+08:00 >
5月 05 22:20:13 server1 alertmanager[1790]: time=2026-05-05T22:20:13.615+08:00 >
[root@server1 ~]# netstat -pantul|grep alertmanager
tcp6 0 0 :::9093 :::* LISTEN 1790/alertmanager
tcp6 0 0 :::9094 :::* LISTEN 1790/alertmanager
udp6 0 0 :::9094 :::* 1790/alertmanager
[root@server1 ~]#
- 9093 (公开端口): 面向用户和外部服务,用于访问Web UI和接收Prometheus推送的告警。
- 9094 (私有端口): 仅用于Alertmanager节点之间的数据同步,对外部工具不可见。因此,用
curl或浏览器直接访问9094被拒绝是完全正常的。
3. 查看

总结
- 成功部署了 Prometheus 监控体系核心组件,实现了从指标采集(Node Exporter)、存储查询(Prometheus)到可视化展示(Grafana)的完整闭环。
- 掌握了各组件的生产级部署方法(systemd 管理、用户隔离、配置优化),为实际运维环境落地奠定了基础。
- 学习了 PromQL 基础语法,能够针对服务器资源(CPU、内存、磁盘、网络)设计监控图表,并借助社区仪表盘快速提升可视化效率。
- 学习了 Alertmanager 的安装与基础配置,为后续扩展告警功能提供了清晰路径。