Prometheus+Grafana
一、什么是prometheus监控
-
Prometheus是一款开源系统监控和告警工具包
-
Prometheus 将其指标收集并存储为时间序列数据,即指标信息与其记录的时间戳一起存储,以及称为标签的可选键值对。
二、特点
-
一个多维数据模型,其中包含由指标名称和键值对标识的时间序列数据。
-
PromQL 是一种灵活的查询语言 ,可以利用这种维度。
-
不依赖分布式存储;单个服务器节点是自治的。
-
时间序列数据采集是通过HTTP的拉取模型进行的。
-
推送时间序列数据是通过中间网关实现的。
-
目标通过服务发现或静态配置来发现。
-
支持多种图表和仪表盘模式
三、指标与时间序列数据
指标是指用通俗易懂的方式表达的数值测量。时间序列指的是记录随时间推移发生的变化。用户想要衡量的指标因应用而异。例如,对于 Web 服务器,可能是请求时间;对于数据库,可能是活跃连接数或活跃查询数,等等。
指标在理解应用程序运行原因方面发挥着重要作用。假设您正在运行一个 Web 应用程序,并发现它运行缓慢。要了解应用程序的运行状况,您需要一些信息。例如,当请求数量很高时,应用程序可能会变慢。如果您有请求计数指标,就可以确定原因并增加服务器数量来处理负载。
四、架构
![![[architecture.svg]]](https://i-blog.csdnimg.cn/direct/89593ef74078445dae1057fbb793fe8a.png)
五、实验案例
1.实验环境
-
promethus服务器
-
agent被监控服务器(LB、Web01/Web02、MySQL)
-
Grafana服务器
2.服务器准备
| 编号 | 主机名 | 主机IP地址 | 角色 |
|---|---|---|---|
| 1 | promethus | 192.168.81.133 | master |
| 2 | agent | 192.168.81.134 | node01 |
| 3 | grafana | 192.168.81.135 | node02 |
3.初始化服务器
IP地址、主机名、绑定/etc/hosts文件、时间同步
IP地址:
#vi /etc/sysconfig/network-scripts/ifcfg-ens33
BOOTPROTO=static # 将 dhcp 改为 static (静态)
ONBOOT=yes # 设置开机自启
IPADDR=192.168.81.133 # 你想设定的新
IP NETMASK=255.255.255.0 # 子网掩码
GATEWAY=192.168.1.1 # 网关(通常是路由器的 IP)
DNS1=114.114.114.114 # DNS 服务器
# 重启网络服务
systemctl restart network
主机名设置:
# hostnamectl set-hostname promethus.itcxh.cn
# hostnamectl set-hostname agent.itcxh.cn
# hostnamectl set-hostname grafana.itcxh.cn
/etc/hosts文件:
192.168.81.133 promethus promethus.itcxh.cn
192.168.81.134 agent agent.itcxh.cn
192.168.81.135 grafana grafana.itcxh.cn
时间同步ntp
sudo timedatectl set-timezone Asia/Shanghai
# 修复yum
sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*.repo
sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*.repo
yum clean all && yum makecache
# 安装
sudo yum install chrony -y
# 启动并设置开机自启
sudo systemctl enable --now chronyd
# 配置国内高速时间源
sudo vi /etc/chrony.conf
找到pool或server开头的行,将其替换为:
server ntp.aliyun.com iburst
server ntp.tencent.com iburst
server cn.ntp.org.cn iburst
# 重启服务并强制同步
sudo systemctl restart chronyd
# 开启系统的自动同步开关
sudo timedatectl set-ntp true
# 查看同步状态
chronyc sources -v

4.安装prometheus
wget https://github.com/prometheus/prometheus/releases/download/v2.51.0/prometheus-2.51.0.linux-amd64.tar.gz
mkdir promethus
tar -xvf prometheus-2.51.0.linux-amd64.tar.gz -C ~/promethus/
cd ~/promethus/prometheus-2.51.0.linux-amd64
# 开放防火墙
firewall-cmd --permanent --add-port=9090/tcp
firewall-cmd --reload
# 运行promethus
./prometheus --config.file=prometheus.yml
# 浏览器访问
http://192.168.81.133:9090
![![[Pasted image 20260319172023.png]]](https://i-blog.csdnimg.cn/direct/5d151d3993504ebe9c4baeb71641cf5a.png)
![![[Pasted image 20260319172045.png]]](https://i-blog.csdnimg.cn/direct/8048c0d673f64e9ab7a6d5f651797267.png)
5.监控Linux主机Agent
1.node组件介绍
在远程linux主机(被监控端agent)上安装node_exporter组件,收集系统信息。
监控不同的信息需要安装不同的组件。
2.安装node_exporter
第一步:上传软件包到Linux服务器中
wget https://github.com/prometheus/node_exporter/releases/download/v1.7.0/node_exporter-1.7.0.linux-amd64.tar.gz
第二步:解压安装node_exporter组件
mkdir exporter
tar -xvf node_exporter-*.tar.gz -C ~/exporter
第三步:开放防火墙
sudo firewall-cmd --permanent --add-port=9100/tcp
sudo firewall-cmd --reload
第四步:启动node_exporter收集系统信息
cd exporter/node_exporter-1.7.0.linux-amd64/
./node_exporter &
第五步:确认端口(9100)
lsof -i:9100 或 ss -naltp | grep 9100
第六步:使用http协议+9100端口收集Linux主机信息
http://192.168.81.134:9100/metrics
![![[Pasted image 20260319235116.png]]](https://i-blog.csdnimg.cn/direct/d76574857f9b471b98b496b887362db1.png)
6.回到prometheus服务器的配置文件里添加被监控机器的配置段:
在主配置文件最后加上下面三行
# vim promethus/prometheus-2.51.0.linux-amd64/prometheus.yml
- job_name: 'agent1'
static_configs:
- targets: ['192.168.81.134:9100]
改完配置文件后,重启服务
systemctl restart promrtheus
# 确认端口被占用,说明重启成功
ss -naltp | grep 9090
#补充!!!
由于在启动Prometheus和node_exporter的时候是直接以./执行的(缺点是占用命令窗口无法退出,而systemctl可以将进程放在后台运行,不占用窗口),所以没有正确配置server文件。如果后面想要使用systemctl来进行启停服务的话,需要补充以下:
第一步:把“说明书”塞进系统目录
cat <<EOF > /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/root/promethus/prometheus-2.51.0.linux-amd64
ExecStart=/root/promethus/prometheus-2.51.0.linux-amd64/prometheus --config.file=/root/promethus/prometheus-2.51.0.linux-amd64/prometheus.yml
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
第二步:正式启动对象
systemctl daemon-reload
systemctl start prometheus
systemctl status prometheus
![![[Pasted image 20260319235009.png]]](https://i-blog.csdnimg.cn/direct/cbbbf4f0beb2402885123965995773bc.png)
7.监控mysqld服务
1.如何监控其他组件
在node_exporter的基础上,可以根据自己的需要收集其他信息。
各组件是平级关系,以node_exporter为基础是为了更好地查询信息。
2.安装MySQL数据库
sudo yum -y install https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
# 安装MySQL服务
sudo yum -y install mysql-community-server --nogpgcheck
sudo systemctl start mysqld
sudo systemctl enable mysqld
# 修改密码
sudo grep 'temporary password' /var/log/mysqld.log # 获取临时密码
sudo mysql_secure_installation # 输入密码
3.创建mysql账号
sudo mysql -u root -p
mysql> CREATE USER 'exporter'@'localhost' IDENTIFIED BY '你的密码' WITH MAX_USER_CONNECTIONS 3;
mysql> GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';
mysql> FLUSH PRIVILEGES;
mysql> EXIT;
4.安装mysqld_exporter组件
sudo wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.15.1/mysqld_exporter-0.15.1.linux-amd64.tar.gz
sudo tar -xvf mysqld_exporter-*.tar.gz -C ~/exporter
5.在mysqld_exporter组件中配置mysql信息 .my.cnf
cd ~/exporter/mysqld_exporter-0.15.1.linux-amd64/
sudo vim .my.cnf
# 写入以下内容
[client]
user=exporter
password=你的密码
6.开放防火墙
sudo firewall-cmd --permanent --add-port=9104/tcp
sudo firewall-cmd --reload
将mysqld_exporter变成一个标准的服务:
cat <<EOF > /etc/systemd/system/mysqld_exporter.service
[Unit]
Description=MySQL Exporter
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/home/promethus/exporter/mysqld_exporter-0.15.1.linux-amd64
ExecStart=/home/promethus/exporter/mysqld_exporter-0.15.1.linux-amd64/mysqld_exporter --config.my-cnf=/home/promethus/exporter/mysqld_exporter-0.15.1.linux-amd64/.my.cnf
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
第二步:
# 1. 重新加载系统配置
systemctl daemon-reload
# 2. 启动并设置开机自启
systemctl start mysqld_exporter
systemctl enable mysqld_exporter
# 3. 检查状态
systemctl status mysqld_exporter
7.启动mysqld_exporter组件
8.确认端口
ss -nalpt | grep 9104
http://192.168.81.134:9104/metrics
![![[Pasted image 20260320143657.png]]](https://i-blog.csdnimg.cn/direct/9716e95106954a289612d3854e053266.png)
9.让promethus可以拉取mysqld节点信息
回到promethus配置prometheus.yml
vim /root/promethus/prometheus-2.51.0.linux-amd64/prometheus.yml
![![[Pasted image 20260320145237.png]]](https://i-blog.csdnimg.cn/direct/98bbdd7424284ac8ad211a5989c58c5a.png)
修改完保存退出后,重启Prometheus:
systemctl restart prometheus
访问http://192.168.81.133:9090![![[Pasted image 20260320145459.png]]](https://i-blog.csdnimg.cn/direct/2d6e4157595b49a3ab32d7da754d8057.png)
六、Grafana
第一步:在Prometheus虚拟机上安装Grafana
1.添加Grafana仓库(CentOS 7):
vim /etc/yum.repos.d/grafana.repo
# 写入以下内容
[grafana]
name=grafana
baseurl=https://packages.grafana.com/oss/rpm
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://packages.grafana.com/gpg.key
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
2.安装并启动:
sudo yum install grafana -y
sudo systemctl start grafana-server
sudo systemctl enable grafana-server
3.打开防火墙:
# 开放防火墙
firewall-cmd --permanent --add-port=3000/tcp
firewall-cmd --reload
第二步:登录与配置数据源
1.访问http://192.168.81.133:3000(Grafana默认端口是3000)
2.默认账号密码:用户名admin,密码admin(首次登录会要求修改密码:设置新密码)
3.连接Prometheus:
-
点击左侧菜单的 Connections -> Data Sources。
-
点击 Add data source,选择 Prometheus。
-
在 Connection 栏的 URL 中填入:
http://localhost:9090。 -
拉到最下面点击 Save & test。如果显示绿色,说明“路”修通了!
![![[Pasted image 20260322190831.png]]](https://i-blog.csdnimg.cn/direct/4a0a9a23fd33490ab8b749f27e82e251.png)
![![[Pasted image 20260322192014.png]]](https://i-blog.csdnimg.cn/direct/03cbbc0384a04c4495b4bb42ab37904f.png)
第三步:导入MySQL监控大屏
-
点击左侧菜单的 Dashboards。
-
点击 New -> Import。
-
在 Import via grafana.com 栏输入 ID:
7362(这是最经典的 MySQL 监控面板 ID)。 -
点击 Load。
-
在底部的 Prometheus 选择框里,选择你刚才创建的那个数据源。
-
点击 Import。
![![[Pasted image 20260322200602.png]]](https://i-blog.csdnimg.cn/direct/3b1c6ff57eb1423fbd3787872aff01f0.png)
![![[Pasted image 20260322201917.png]]](https://i-blog.csdnimg.cn/direct/072c1f840d1a4b2eb6a5364253c2c37e.png)
相关信息:
-
MySQL Uptime(运行时间)
-
Current QPS(实时每秒查询量)
-
InnoDB Buffer Pool Usage(内存缓冲池使用率)
-
Top 10 Slow Queries(前十条慢查询)
七、Grafana+钉钉实现告警
实现告警之前把所有机器时间同步再检查一遍。
ntpdate cn.ntp.org.cn
1.创建钉钉机器人,选择自定义
![![[Pasted image 20260325114708.png]]](https://i-blog.csdnimg.cn/direct/1861f7a0b1ad49e883a1e76223d177e9.png)
2.设置推送通道(Contact Points)
Integration选项可以选择推送对象,如钉钉、企业微信、e-mail等等
![![[Pasted image 20260325155029.png]]](https://i-blog.csdnimg.cn/direct/763ed86b83894eb4b6f9ddaa100ec463.png)
3.定义通知策略 (Notification Policies)![![[Pasted image 20260325155432.png]]](https://i-blog.csdnimg.cn/direct/7e6a54de75e64141885dcf8ee2476579.png)
4.创建告警规则 (Alert Rules)![![[Pasted image 20260325155717.png]]](https://i-blog.csdnimg.cn/direct/aa3f110be4af415a84735038addb7e3a.png)
5.发送到钉钉的信息无法正常访问,原因是grafana默认把链接跳转地址设置成了localhost![![[Pasted image 20260325161306.png]]](https://i-blog.csdnimg.cn/direct/632ba9ff91e346cf9cf9d4d33f52f59c.png)
修改配置文件
sudo vi /etc/grafana/grafana.ini
# 找到 `[server]` 这一行
# 修改以下两行内容(去掉行首的分号 `;` 才能生效)
domain = 192.168.81.133
root_url = http://192.168.81.133:3000/
重启grafana服务
sudo systemctl restart grafana-server
为什么这样做就能打开了?
-
之前的情况:Grafana 发给钉钉的链接可能是
http://localhost:3000/alerting/...。当你点击时,你的电脑/手机会在它自己身上找 3000 端口,当然打不开。 -
修改后:Grafana 发出的链接会变成
http://192.168.81.133:3000/alerting/...。只要你的电脑和服务器在同一个局域网(比如连了同一个 Wi-Fi),点击就能直接跳转到告警页面。
特别提示:
如果你是在手机上点开钉钉:
-
手机必须连接到与服务器同一个网段的 Wi-Fi 才能打开
192.168.81.133这个内网地址。 -
如果手机用的是 5G/4G 网络,由于它是内网 IP,依然是打不开的(除非你做了公网映射或 VPN)。
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐

所有评论(0)