利用Prometheus实现监控所有服务器
创始人
2025-01-18 06:04:47
0

一 、简单介绍prometheus

1.1prometheus架构原理图

1.2各种组件的功能

Prometheus Server:数据采集,数据存储,数据查询,告警,自动发现,web ui

Exporters:将非Prometheus格式的应用或服务指标转换为Prometheus可抓取的格式。

Alertmanager:处理Prometheus生成的告警,并将其发送给指定的接收者

Pushgateway:接收短期作业或批处理作业的指标数据,并允许Prometheus Server拉取

Grafana:数据可视化和监控平台,与Prometheus集成,提供图表和仪表盘展示监控数据

Client Libraries:提供在应用程序中实现自定义指标的方式,支持多种编程语言

二、二进制源码安装

1.1.将源码文件上传到ansible服务器

使用xftp直接上传文件即可

[root@nfs-ansible-prom ansible]# mkdir /prom        新建prom文件夹

[root@nfs-ansible-prom ansible]# cd /prom

[root@nfs-ansible-prom prom]# ls

grafana-enterprise-9.1.2-1.x86_64.rpm        prometheus-2.43.0.linux-amd64.tar.gz

node_exporter-1.4.0-rc.0.linux-amd64.tar.gz

1.2.解压源码包

[root@nfs-ansible-prom prom]# tar xf prometheus-2.43.0.linux-amd64.tar.gz        解压源码包

[root@nfs-ansible-prom prom]# ls

grafana-enterprise-9.1.2-1.x86_64.rpm        prometheus-2.43.0.linux-amd64

node_exporter-1.4.0-rc.0.linux-amd64.tar.gz  prometheus-2.43.0.linux-amd64.tar.gz

[root@nfs-ansible-prom prom]# mv prometheus-2.43.0.linux-amd64 prometheus        修改解压后的压缩包名字为prometheus

[root@nfs-ansible-prom prom]# ls        可以看到修改成功

grafana-enterprise-9.1.2-1.x86_64.rpm        prometheus

node_exporter-1.4.0-rc.0.linux-amd64.tar.gz  prometheus-2.43.0.linux-amd64.tar.gz

1.3.临时和永久修改PATH变量,添加prometheus的路径

[root@nfs-ansible-prom prom]# PATH=/prom/prometheus:$PATH

[root@nfs-ansible-prom prom]# echo 'PATH=/prom/prometheus:$PATH'  >>/etc/profile

1.4.把prometheus做成一个服务来进行管理,非常方便日后维护和使用

[root@nfs-ansible-prom prom]# vim /usr/lib/systemd/system/prometheus.service

[Unit]

Description=prometheus

[Service]

ExecStart=/prom/prometheus/prometheus --config.file=/prom/prometheus/prometheus.yml

ExecReload=/bin/kill -HUP $MAINPID

KillMode=process

Restart=on-failure

[Install]

WantedBy=multi-user.target

1.5.重新加载systemd相关的服务,识别Prometheus服务的配置文件并启动服务

[root@nfs-ansible-prom prom]# systemctl  daemon-reload

启动Prometheus服务

[root@nfs-ansible-prom prom]# systemctl start prometheus

[root@nfs-ansible-prom prom]# systemctl restart prometheus

[root@nfs-ansible-prom prom]# ps aux|grep prome        查看prometheus进程是否启动

root       2740  2.7  0.9 798956 37312 ?        Ssl  14:57   0:00 /prom/prometheus/prometheus --config.file=/prom/prometheus/prometheus.yml

root       2748  0.0  0.0 112824   976 pts/0    S+   14:57   0:00 grep --color=auto prome

1.6.设置开机启动

[root@nfs-ansible-prom prom]# systemctl enable prometheus

Created symlink from /etc/systemd/system/multi-user.target.wants/prometheus.service to /usr/lib/systemd/system/prometheus.service.

1.7.查看服务启动后的效果

http://192.168.203.135:9090

http://192.168.203.135:9090/metrics

三、安装exporter

1.1上传exporter到web服务器和mysql服务器

直接利用xftp工具上传node_exporter软件,也可以使用远程拷贝命令scp,或者还可以在ansible服务器中利用copy模块拷贝软件到剩余两台服务器中 ansible all -m copy  -a 'src=node_exporter-1.4.0-rc.0.linux-amd64.tar.gz dest=/root/'
 

1.2编写在其他机器上安装node_exporter的脚本

[root@nfs-ansible-prom prom]# vim install_node_exporter.sh        编写自动安装exporter脚本

#!/bin/bash

tar xf /root/node_exporter-1.4.0-rc.0.linux-amd64.tar.gz  -C /

cd  /

mv node_exporter-1.4.0-rc.0.linux-amd64/ node_exporter

cd /node_exporter/

echo 'PATH=/node_exporter/:$PATH' >>/etc/profile

#生成nodeexporter.service文件

cat >/usr/lib/systemd/system/node_exporter.service  <

[Unit]

Description=node_exporter

[Service]

ExecStart=/node_exporter/node_exporter --web.listen-address 0.0.0.0:9090

ExecReload=/bin/kill -HUP $MAINPID

KillMode=process

Restart=on-failure

[Install]

WantedBy=multi-user.target

EOF

#让systemd进程识别node_exporter服务

systemctl daemon-reload

#设置开机启动

systemctl  enable node_exporter

#启动node_exporter

systemctl  start node_exporter

1.3使用ansible的copy模块推送脚本到db和web服务器

[root@ansible prometheus]# ls

console_libraries  consoles  data  install_node_exporter.sh  LICENSE  nohup.out  NOTICE  prometheus  prometheus.yml  promtool

[root@ansible prometheus]# ansible all  -m copy  -a "src=install_node_exporter.sh dest=/root"

1.4在其他的服务器上查看是否安装node_exporter成功

[root@lb2 ~]# ps aux|grep node        查看node——exporter进程是否存在

root       5408  0.0  0.2 716288 11068 ?        Ssl  15:25   0:00 /node_exporter/node_exporter --web.listen-address 0.0.0.0:9090

root       5524  0.0  0.0 112824   976 pts/1    S+   15:27   0:00 grep --color=auto node

四、添加被监控的服务器

1.1修改配置文件

[root@nfs-ansible-prom prometheus]# vim prometheus.yml         修改yml配置文件

将安装了exporter的服务器添加到配置文件中

1.2重启Prometheus服务并查看效果

[root@nfs-ansible-prom prometheus]# service prometheus restart

Redirecting to /bin/systemctl restart prometheus.service

到此,我们实现了对web服务器和mysql服务器的监控

相关内容

热门资讯

一直以来!朋朋政和软件有辅助器... 一直以来!朋朋政和软件有辅助器的吗,约战竞技场辅助脚本-竟然真的有辅助工具(哔哩哔哩)1、朋朋政和软...
第3分钟了解!雀友会广东潮汕麻... 第3分钟了解!雀友会广东潮汕麻雀辅助!确实是真的有辅助攻略(有挂透视)-哔哩哔哩1、这是跨平台的雀友...
第5分钟了解!福建旺旺麻将有挂... 第5分钟了解!福建旺旺麻将有挂吗,开心泉州小程序辅助免费下载,积累教程(存在有挂)-哔哩哔哩该软件可...
透视玄学!掌中乐游戏中心破解版... 透视玄学!掌中乐游戏中心破解版,wepoker私人局辅助器-确实真的有辅助软件(哔哩哔哩)1、游戏颠...
7分钟了解!新漫游大厅辅助!总... 7分钟了解!新漫游大厅辅助!总是是真的有辅助脚本(有人有挂)-哔哩哔哩在进入新漫游大厅辅助软件靠谱后...
近期!爱来辅助器,情怀宜春辅助... 近期!爱来辅助器,情怀宜春辅助-本来存在有辅助app(哔哩哔哩)暗藏猫腻,小编详细说明情怀宜春辅助破...
7分钟了解!兴动互娱辅助工具如... 7分钟了解!兴动互娱辅助工具如何下载,潮汕老友麻将开挂,手段教程(有挂神器)-哔哩哔哩1、很好的工具...
5分钟了解!werplan外开... 5分钟了解!werplan外开挂!确实是真的有辅助攻略(有挂透明挂)-哔哩哔哩1、许多玩家不知道we...
透视模拟器!小闲川南宜宾辅助,... 透视模拟器!小闲川南宜宾辅助,hhpoker辅助是真的吗-真是是有辅助教程(哔哩哔哩)1、超多福利:...
第六分钟了解!微信小程序微乐房... 第六分钟了解!微信小程序微乐房间怎么辅助,上品游戏辅助软件,机巧教程(确实有挂)-哔哩哔哩1、微信小...