线上监控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

anhengcloud@10.192.241.17:/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的核心功能

相关推荐
Rain的Java大神之路9 小时前
Spring Boot 4 把自动配置拆成 47 个模块后,我的 `dependency:tree` 翻天了:一份从排查到治理的完整实战手册
java·spring boot·后端
狗头大军之江苏分军9 小时前
前端转行干能干什么?我转行试了下(被迫)
前端·后端
孟陬9 小时前
Foobar 技术周刊#2:约会已死?用户急剧下滑,约会App大概率会弱化约会属性转向轻陪伴。我有个建议:
前端·后端·rust
NEIL_XU_11 小时前
自研提效工具——Bug通:打通测试与开发的「最后一公里」
后端·ai编程
Mikko712 小时前
Redis 主从切换期间可用吗?6.2 实测哨兵切换:应用不可写 33 秒,主从连不上、数据不一致,以及设了 ACL 后哨兵不切换
java·运维·redis·后端
猿人谷12 小时前
从百万个 Pareto 解中选出 100 个:Riesz s-Energy 如何实现高效的多样性选择?
后端·算法·aigc
不积跬步无以至千里mmbb2612 小时前
【软件工艺师 rust相关课程】
开发语言·后端·rust
不得了zz12 小时前
11-Agent 可观测与评估:Spring AI Alibaba Admin Studio 到底解决了什么
java·人工智能·后端·spring·ai编程·springaialibaba
软件技术NINI12 小时前
基于 SpringBoot 的酒店预定系统
java·spring boot·后端