你需要提前准备以下资源,所有操作均使用root权限执行,避免权限不足报错:
CentOS系统执行以下命令:
``` yum update -y yum install -y wget tar vim ```Ubuntu系统执行以下命令:
``` apt update -y apt install -y wget tar vim ```Prometheus负责采集、存储监控指标数据,执行以下命令安装:
``` 下载官方稳定版安装包 wget https://github.com/prometheus/prometheus/releases/download/v2.47.0/prometheus-2.47.0.linux-amd64.tar.gz 解压 tar zxvf prometheus-2.47.0.linux-amd64.tar.gz 移动到系统软件目录 mv prometheus-2.47.0.linux-amd64 /usr/local/prometheus 创建数据存储目录 mkdir /usr/local/prometheus/data ```配置系统服务实现开机自启,创建/usr/lib/systemd/system/prometheus.service文件,写入以下完整内容:
启动服务并验证:
``` systemctl daemon-reload systemctl start prometheus systemctl enable prometheus 验证是否启动成功,返回healthy即为正常 curl localhost:9090/-/healthy ```Node Exporter负责采集服务器CPU、内存、磁盘、网络等基础指标,执行以下命令安装:
``` wget https://github.com/prometheus/node_exporter/releases/download/v1.6.1/node_exporter-1.6.1.linux-amd64.tar.gz tar zxvf node_exporter-1.6.1.linux-amd64.tar.gz mv node_exporter-1.6.1.linux-amd64 /usr/local/node_exporter ```配置系统服务,创建/usr/lib/systemd/system/node_exporter.service文件,写入以下内容:
启动服务并验证:
``` systemctl daemon-reload systemctl start node_exporter systemctl enable node_exporter 验证,返回大量指标数据即为正常 curl localhost:9100/metrics ```配置Prometheus拉取Node Exporter数据,编辑/usr/local/prometheus/prometheus.yml,找到scrape_configs字段,在末尾添加以下内容:
重启Prometheus生效:systemctl restart prometheus

Grafana负责将Prometheus的指标数据转化为可视化图表,按操作系统执行对应命令:
CentOS系统:
``` wget https://dl.grafana.com/oss/release/grafana-10.1.0-1.x86_64.rpm yum install -y grafana-10.1.0-1.x86_64.rpm ```Ubuntu系统:
``` wget https://dl.grafana.com/oss/release/grafana_10.1.0_amd64.deb dpkg -i grafana_10.1.0_amd64.deb apt install -f -y ```启动服务:
``` systemctl start grafana-server systemctl enable grafana-server ```配置可视化面板:
首先安装告警组件Alertmanager:
``` wget https://github.com/prometheus/alertmanager/releases/download/v0.25.0/alertmanager-0.25.0.linux-amd64.tar.gz tar zxvf alertmanager-0.25.0.linux-amd64.tar.gz mv alertmanager-0.25.0.linux-amd64 /usr/local/alertmanager ```创建系统服务文件/usr/lib/systemd/system/alertmanager.service:
配置告警规则,创建/usr/local/prometheus/rules.yml文件,写入以下完整规则:
修改Prometheus配置文件/usr/local/prometheus/prometheus.yml,添加以下内容:
修改Alertmanager配置文件/usr/local/alertmanager/alertmanager.yml,替换为以下内容(钉钉为例,企业微信仅需修改webhook地址即可):
重启所有服务生效:
``` systemctl restart prometheus systemctl start alertmanager systemctl enable alertmanager ```测试告警:执行yum install -y stress && stress -c 4压测CPU,5分钟后即可收到告警通知,按Ctrl+C停止压测。
firewall-cmd --add-port=端口号/tcp --permanent && firewall-cmd --reload,Ubuntu执行ufw allow 端口号/tcp,同时确认云服务器安全组已开放对应端口易频IT社区是综合性互联网IT技术门户网站,专注分享网络技术、服务器运维、网络安全、编程开发、系统架构、云计算、大数据等行业干货,实时更新IT行业资讯、零基础教程、实战案例,为IT从业者、技术爱好者提供专业的学习交流平台。
Copyright © 2021-2026 易频IT社区. All Rights Reserved. 备案号:闽ICP备2023013482号 网站地图