线上监控1:Prometheus搭建

背景:

随着项目微服务的拆分越来越详细,中间件使用越来越多,线上环境的监控便来越来越重要,有了监控,就可以及时发现服务器的异常信息并提供报警服务,也会在后面对一些核心日志场景进行异常报告告警,所以让我负责监控这块,自己学习了几天的Prometheus,将Prometheus+grafna监控搭建过程记录如下

一.Prometheus搭建

下载⽹址 prometheus.io/download/

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的核心功能

相关推荐
你怎么知道我是队长3 小时前
GO语言---匿名函数
开发语言·后端·golang
G探险者8 小时前
为什么 Zookeeper 越扩越慢,而 Nacos 却越扩越快?
分布式·后端
不太厉害的程序员8 小时前
NC65配置xml找不到Bean
xml·java·后端·eclipse
不被定义的程序猿8 小时前
Golang 在 Linux 平台上的并发控制
开发语言·后端·golang
AntBlack8 小时前
Python : AI 太牛了 ,撸了两个 Markdown 阅读器 ,谈谈使用感受
前端·人工智能·后端
mikes zhang9 小时前
Flask文件上传与异常处理完全指南
后端·python·flask
Pitayafruit9 小时前
跟着大厂学架构01:如何利用开源方案,复刻B站那套“永不崩溃”的评论系统?
spring boot·分布式·后端
方圆想当图灵9 小时前
深入理解软件设计:领域驱动设计 DDD
后端·架构
excel9 小时前
MySQL 9 在 Windows 上使用 mysqld --initialize-insecure 无响应的排查与解决方案
后端
你怎么知道我是队长9 小时前
GO语言---defer关键字
开发语言·后端·golang