https://www.cnblogs.com/ceshi2016/p/17265925.html
1、实验环境准备 服务 IP Prometheus服务器 192.168.10.4/24 grafana服务器 192.168.10.4/24 被监控服务器 192.168.10.5/24 软件 下载地址 prometheus-2.38.0.linux-amd64.tar.gz https://prometheus.io/download/ grafana-enterprise-9.1.1-1.x86_64.rpm https://grafana.com/grafana/download node_exporter-1.2.2.linux-amd64.tar.gz https://prometheus.io/download/ 2、基础环境配置
关闭防火墙
systemctl stop firewalld systemctl disable firewalld
如果开启防火则,则向防火墙开放端口(如果防火墙已关闭,请忽视)
firewall-cmd —zone=public —add-port=9100/tcp —permanent
重启防火墙(如果防火墙已关闭,请忽视)
firewall-cmd —reload
关闭selinux
sed -i ‘s/enforcing/disabled/’ /etc/selinux/config
临时生效
setenforce 0 3、部署prometheus 从 https://prometheus.io/download/ 下载相应版本,放到到服务器上。 官网提供的是二进制版,解压就可以直接使用用,不需要对其进行编译。
解压安装包
tar -zxvf prometheus-2.38.0.linux-amd64.tar.gz -C /usr/local/
进入解压目录进行重命名
cd /usr/local/ mv prometheus-2.38.0.linux-amd64/ prometheus
进入prometheus目录下
cd /usr/local/prometheus/
编写启动脚本
echo ’ ./prometheus —config.file=./prometheus.yml &’ > start.sh && chmod +x start.sh
编写停止脚本
echo ’ pkill prometheus’ > stop.sh && chmod +x stop.sh
确认是否正常启动(默认端口9090)
[root@localhost prometheus]# netstat -ntlp | grep 9090 tcp6 0 0 :::9090 :::* LISTEN 31022/./prometheus [root@localhost prometheus]# 使用浏览器访问:192.168.10.4:9090 访问到prometheus的主界面 在这里插入图片描述 点击Status—>Targets—>就可以看到监控的本机,点击监控主机的任意一个IP+端口/metrics可以查看到监控的数据在这里插入图片描述 4、部署Grafana可视化图形工具 下载地址:https://grafana.com/grafana/download
这里使用的是CentOS7系统,直接下载rpm包就好,建议选择8.0以上的版本
这里使用yum来进行安装,使用rpm安装也可以,根据自己的喜好进行选择
yum -y install grafana-enterprise-9.1.1-1.x86_64.rpm
启动服务并加入开机启动
systemctl start grafana-server.service systemctl enable grafana-server.service
检查服务状态
systemctl status grafana-server.service
查看端口号(默认使用3000端口)
[root@localhost ~]# netstat -ntlp | grep grafana tcp6 0 0 :::3000 :::* LISTEN 20897/grafana-serve [root@localhost ~]#
离线下载grafana
新建 /etc/yum.repos.d/grafana.repo,内容为
[grafana] name=grafana baseurl=https://mirrors.aliyun.com/grafana/yum/rpm repo_gpgcheck=0 enabled=1 gpgcheck=0
按:wq保存退出即可。
清除YUM缓存
yum makecache
加载YUM镜像源
yum repolist
下载不安装采用:https://blog.csdn.net/weixin_43909790/article/details/126563568 使用浏览器访问:192.168.10.4:3000 访问到grafana的主界面 首次访问 账号:admin 密码: admin 登录成功之后系统会让其重新设置密码,暂时不想修可以先将其跳过 在这里插入图片描述 在这里插入图片描述 至此,我们就可以看到grafana主界面了 5、监控远程Linux主机 监控远程Linux主机:192.168.10.5 下载地址:https://prometheus.io/download/
解压安装包
tar -zxvf node_exporter-1.2.2.linux-amd64.tar.gz -C /usr/local/
进入解压目录进行重命名
cd /usr/local/ mv node_exporter-1.2.2.linux-amd64/ node_exporter
进入node_exporter目录下
cd /usr/local/node_exporter/
编写启动脚本
echo ’ ./node_exporter &’ > start.sh && chmod +x start.sh
编写停止脚本
echo ’ pkill node_exporter ’ > stop.sh && chmod +x stop.sh
确认是否正常启动(默认端口9100)
[root@localhost node_exporter]# netstat -ntlp | grep 9100 tcp6 0 0 :::9100 :::* LISTEN 31115/./node_export [root@localhost node_exporter]# 同样操作,将所有需要被监控主机安装node_exporter组件 使用浏览器访问http://被监控端IP:9100/metrics可以查看到node_exporter在被监控端收集的监控信息。 6、配置prometheus.yml 回到prometheus(192.168.10.4)服务器的配置文件里添加被监控机器
在scrape_configs下进行配置,注意文件yml格式。(此处是在默认的基础上添加修改的)
- job_name: “prometheus” # 给被监控主机取个名字,这里是默认的,名字由自己定义
honor_timestamps: true
scrape_interval: 15s
metrics_path: /metrics
scheme: http
static_configs:
- targets: #[“192.168.10.4:9090”,‘192.168.10.4:9100’,‘192.168.10.5:9100’]
- “192.168.10.4:9090”
- ‘192.168.10.4:9100’
- ‘192.168.10.5:9100’ #有多少台被监控主机就照格式添加在后面就OK。 :wq! # 保存退出
- targets: #[“192.168.10.4:9090”,‘192.168.10.4:9100’,‘192.168.10.5:9100’]
使用编写好的脚本重新启动 prometheus服务。
sh stop.sh #或./stop.sh sh start.sh #或./start.sh 回到web管理界面,可以看到多了两台监控主机 登录到 http://192.168.10.4:9090/targets 进行查看是否将上面配置文件中添加的主机进行监控。 在这里插入图片描述
7、登陆Grafana Web界面对其进行配置
8、导入Grafana Web模板 Grafana官方提供模板地址: https://grafana.com/grafana/dashboards 选择自动同步版
至此就完成了对服务器CPU、磁盘、内存等监控信息。