背景:
随着项目微服务的拆分越来越详细,中间件使用越来越多,线上环境的监控便来越来越重要,有了监控,就可以及时发现服务器的异常信息并提供报警服务,也会在后面对一些核心日志场景进行异常报告告警,所以让我负责监控这块,自己学习了几天的Prometheus,将Prometheus+grafna监控搭建过程记录如下
一.Prometheus搭建
cd /data wget github.com/prometheus/... tar -zxvf prometheus-2.34.0.linux-arm64.tar.gz mv prometheus-2.34.0.linux-arm64 prometheus cd prometheus
修改配置
vim /data/prometheus/prometheus.yml
设置prometheus开机⾃启
vim /etc/systemd/system/prometheus.service
java
[Unit]
Description=Prometheus Monitoring System
Documentation=https://prometheus.io/docs/introduction/overview/
[Service]
ExecStart=/data/prometheus/prometheus \
--config.file=/data/prometheus/prometheus.yml \
--web.listen-address=:9090
Restart=on-failure
[Install]
WantedBy=multi-user.target
// /data/prometheus 是我prometheus的安装路径,可以按需修改
systemctl daemon-reload // 加载新的systemd配置
systemctl enable prometheus // 设置开机⾃启
systemctl restart prometheus // 启动prometheus
systemctl status prometheus// 查看启动状态
安装 node-exporte
下载
node-exporter可以实现系统监控,在平台每个被监控的机器上安装node-exporter
java
wget
https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_expor
ter-1.3.1.linux-arm64.tar.gz
scp -P 52222 -r node_exporter-1.3.1.linux-arm64.tar.gz
[email protected]:/home/anhengcloud
cd / && mkdir /app && cd /app
#cp /home/anhengcloud/node_exporter-1.3.1.linux-arm64.tar.gz /app
wget
https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_expor
ter-1.3.1.linux-arm64.tar.gz
chmod -R 755 node_exporter-1.3.1.linux-arm64.tar.gz
chown -R root.root node_exporter-1.3.1.linux-arm64.tar.gz
tar -zxvf node_exporter-1.3.1.linux-arm64.tar.gz
mv node_exporter-1.3.1.linux-arm64 node_exporter
cd node_exporter
nohup ./node_exporter --web.listen-address 10.192.241.17:58082 &
设置node-exporter开机启动
vim /etc/systemd/system/node_exporter.service
js
[Unit]
Description=node_exporter
Documentation=https://github.com/prometheus/node_exporter
[Service]
ExecStart=/app/node_exporter/node_exporter \
--web.listen-address 10.192.241.24:58082
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl daemon-reload // 加载新的systemd配置
systemctl enable node_exporter// 设置开机⾃启
systemctl start node_exporter // 启动node_exporter
systemctl status node_exporter // 查看启动状态
安装grafana
js
apt-get install -y adduser libfontconfig1
wget https://mirrors.huaweicloud.com/grafana/8.4.6/grafana
enterprise_8.4.6_arm64.deb
sudo dpkg -i grafana-enterprise_8.4.6_arm64.deb
// 设置grafana为开机⾃启
systemctl daemon-reload // 加载新的systemd配置
systemctl enable grafana-server // 设置开机⾃启
systemctl start grafana-server // 启动grafana
systemctl status grafana-server // 查看进程启动状态
安装alertmanager
下载
在监控服务器上安装alertmanager
js
cd /data
wget
https://github.com/prometheus/alertmanager/releases/download/v0.24.0/alertmanag
er-0.24.0.linux-arm64.tar.gz
tar xvfz alertmanager-0.24.0.linux-arm64.tar.gz
mv alertmanager-0.24.0.linux-arm64 alertmanager
配置服务及开机⾃启 vim /etc/systemd/system/alertmanager.service
js
// ⽂件内容开始
[Unit]
Description=alertmanager
Documentation=https://prometheus.io/docs/alerting/latest/alertmanager/
https://prometheus.io/docs/introduction/overview/
[Service]
ExecStart=/data/alertmanager/alertmanager \
--config.file=/data/alertmanager/alertmanager.yml
Restart=on-failure
[Install]
WantedBy=multi-user.target
// ⽂件内容结束
systemctl daemon-reload
systemctl start alertmanager
systemctl enable alertmanager
systemctl status alertmanager
配置告警规则⽂件
vim /data/prometheus/prometheus.yml
vim /data/prometheus/rules/node_rules.yml
vim /data/prometheus/rules/java_rules.yml
二.平台使用
Prometheus平台使用
访问 9090,就能访问Prometheus

查看node_export数据
访问9100端口,可以查看node_export的数据

Prometheus配置任务,定时拉取node的数据
在配置文件 prometheus.yml中添加job如下

js
- job_name: "node_export" # job的名称,自己写即可
# metrics_path defaults to '/metrics' #拉取数据是用http请求,需要配置请求的路径,默认是/metrics
# scheme defaults to 'http'.
static_configs:
- targets: ["localhost:9100"] # 要拉取数据的服务器的ip:端口
配置好后,需要重启一次Prometheus,配置即可起作用 可在Prometheus控制台上查询数据

三.总结
Prometheus使用go语言编写,在整体的启动是非常方便的,把压缩包解压后,直接执行文件中的可执行文件即可,非常的方便,在后续的文章中,会陆续介绍promethenus的核心功能