Prometheus + AlertManager 全方位监控告警系统部署指南|附Node/MySQL/Redis/Nginx采集器配置
大家好,今天带来一篇硬核技术干货:如何从零开始搭建一套企业级Prometheus监控告警系统!无论你是运维工程师、SRE还是DevOps爱好者,这套组合拳都能让你的监控能力大幅提升!
一、什么是Prometheus + AlertManager?
Prometheus 是一款开源的系统监控和告警工具包,以其强大的多维数据模型和灵活的查询语言著称,已成为云原生监控的事实标准。
AlertManager 负责处理由Prometheus发送的告警,进行去重、分组、静默,并通过邮件、钉钉、微信等多种方式发送告警通知。
常用采集探针(Exporters):
l node_exporter:采集服务器硬件和OS指标
l mysqld_exporter:采集MySQL数据库指标
l redis_exporter:采集Redis指标
l nginx_exporter:采集Nginx指标
二、准备工作:下载安装包
建议从官方GitHub Release页面下载最新版本的二进制包:
Prometheus :https://github.com/prometheus/prometheus/releases/download/v2.45.0/prometheus-2.45.0.linux-amd64.tar.gz
AlertManager:https://github.com/prometheus/alertmanager/releases/download/v0.26.0/alertmanager-0.26.0.linux-amd64.tar.gz
Node_exporter: https://github.com/prometheus/node_exporter/releases/download/v1.6.1/node_exporter-1.6.1.linux-amd64.tar.gz
Mysqld_exporter: https://github.com/prometheus/mysqld_exporter/releases/download/v0.15.0/mysqld_exporter-0.15.0.linux-amd64.tar.gz
Redis_exporter
https://github.com/oliver006/redis_exporter
Nginx_exporter
https://github.com/nginxinc/nginx-prometheus-exporter/releases
https://blog.csdn.net/qq_41372916/article/details/129766849
其他采集探针可查看官方网站 https://prometheus.io/download/
或社区:https://prometheus.io/docs/instrumenting/exporters/
在linux中可直接通过 wget XXX(url)下载。
下面的探针部署示例以node和mysqld探针为主。
1.解压并安装
# tar -zxvf prometheus-2.45.0.linux-amd64.tar.gz
# mv prometheus-2.45.0.linux-amd64 prometheus
# cp prometheus promtool /usr/local/bin/
# cp prometheus.yml /etc/prometheus/
2.配置系统服务
在 /etc/systemd/system/ 下创建 prometheus.service 文件,写入以下信息,注意按实际路径修改:
内容:
[Unit]
Description=Prometheus Monitoring System
Documentation=Prometheus Monitoring System
After=network.target
[Service]
ExecStart=/usr/local/bin/prometheus --web.enable-lifecycle --config.file=/etc/prometheus/prometheus.yml --web.listen-address=:9090
Restart=on-failure
[Install]
WantedBy=multi-user.target
3.启动并验证
# systemctl daemon-reload
# systemctl enable prometheus
# systemctl start prometheus
# systemctl status prometheus
访问web地址:http://IP:9090
排查: 可使用启动服务中的整条命令执行,执行后系统会显示日志:
# /usr/local/bin/prometheus --web.enable-lifecycle --config.file=/etc/prometheus/prometheus.yml --web.listen-address=:9090
1.解压并安装
# tar -zxvf alertmanager-0.26.0.linux-amd64.tar.gz
# mv alertmanager-0.26.0.linux-amd64 alertmanager
# cp alertmanager amtool /usr/local/bin/
# cp alertmanager.yml /etc/alertmanager/
2.配置系统服务
在 /etc/systemd/system/ 下创建 alertmanager.service 文件,写入以下信息,注意按实际路径修改:
内容:
[Unit]
Description=Alertmanager
After=network.target
[Service]
ExecStart=/opt/monitortools/alertmanager/alertmanager --config.file=/etc/alertmanager/alertmanager.yml
Restart=on-failure
[Install]
WantedBy=multi-user.target
3.启动并验证
# systemctl daemon-reload
# systemctl enable alertmanager
# systemctl start alertmanager
# systemctl status alertmanager
访问web地址:http://IP:9093
1. Node Exporter(服务器监控)
1.1解压并安装
# tar -zxvf node_exporter-1.6.1.linux-amd64.tar.gz
# mv node_exporter-1.6.1.linux-amd64 node_exporter
# cp node_exporter/node_exporter /usr/local/bin/
1.2 配置系统服务
在 /etc/systemd/system/ 下创建 node_exporter.service 文件,写入以下信息,注意按实际路径修改:
内容:
[Unit]
Description=node_export
Documentation=https://github.com/prometheus/node_exporter
After=network.target
[Service]
Type=simple
ExecStart= /opt/monitortools/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
1.3. 启动并验证
# systemctl daemon-reload
# systemctl enable node_exporter
# systemctl start node_exporter
systemctl status node_exporter
访问web地址:http://IP:9100
2. MySQLD Exporter(数据库监控)
2.1创建监控用户
CREATE USER 'mysqld_exporter'@'localhost' IDENTIFIED BY '123456' WITH MAX_USER_CONNECTIONS 3;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'mysqld_exporter'@'localhost';
flush privileges;
#如果mysqld_exporter和mysql不一台机器,建议将localhost改为%
注意:建议为用户设置最大连接限制,以避免服务器在重负载下因监控抓取而超载。并非所有 MySQL/MariaDB 版本都支持此功能;例如,
MariaDB 10.1(随 Ubuntu 18.04 提供)不支持此功能。
2.2创建配置文件
在安装目录下创建其所需的配置文件:mysqld_exporter.cnf
# vim mysqld_exporter.cnf
写入相应的要监控数据库密码和用户登信息,支持多个对象,可参考:https://github.com/prometheus/mysqld_exporter
内容(按实际情况填写):
[client]
host=localhost
port=3306
socket=/var/run/mysqld/mysqld.sock
user=XXXX
password=XXX
在 /etc/systemd/system/ 下创建 mysqld_exporter.service 文件,写入以下信息,注意按实际路径修改:
内容:
[Unit]
Description=mysqld_export
Documentation=https://github.com/prometheus/mysqld_exporter
After=network.target
[Service]
Type=simple
ExecStart= /opt/monitortools/mysqld_exporter/mysqld_exporter --config.my-cnf=/opt/monitortools/mysqld_exporter/mysqld_exporter.cnf --web.listen-address=0.0.0.0:9104
Restart=on-failure
[Install]
WantedBy=multi-user.target
2.3 启动并验证
配置开机启动及启动服务、验证:
# systemctl daemon-reload
# systemctl enable mysqld_exporter
# systemctl start mysqld_exporter
# systemctl status mysqld_exporter
访问web地址:http://IP:9104
3. Redis Exporter
3.1创建服务文件
在 /etc/systemd/system/ 下创建 redis_exporter.service 文件,写入以下信息,注意按实际路径修改:
内容:
[Unit]
Description=redis_export
After=network.target
[Service]
Type=simple
ExecStart= /usr/local/redis_exporter/redis_exporter-v1.56.0.linux-amd64/redis_exporter -redis.addr localhost:6379 -redis.password Passw0rd
Restart=on-failure
[Install]
WantedBy=multi-user.target
3.2 启动并验证
配置开机启动及启动服务、验证:
# systemctl daemon-reload
# systemctl enable redis_exporter
# systemctl start redis_exporter
# systemctl status redis_exporter
访问web地址:http://IP:9121
4. Nginx Exporter
nginx探针和其他探针有所区别,在此简要介绍:
首先判断nginx的stub_status_module是否安装:
# ./nginx -V 2>&1 | grep -o with-http_stub_status_module
确认安装后,需要在nginx配置文件中增加一个专用于监控的端口,比如在nginx.conf增加:
server {
listen 8082;
#端口可以自己重新起一个
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1; #白名单
deny all; #指定白名单除外无法访问
}
}
完成配置后,启动Nginx_exporter,如果需要用服务的方式请参考:
https://github.com/nginxinc/nginx-prometheus-exporter/blob/main/examples/systemd/README.md
下面不使用服务,直接启动:
# nohup ./nginx-prometheus-exporter -nginx.scrape-uri http://127.0.0.1:8082/nginx_status > /dev/null &
在网页中查看其状态:
http://192.168.166.164:9113/
六、配置整合:让Prometheus发现监控目标
1. 配置AlertManager关联
# vim /etc/prometheus/prometheus.yml
增加AlertManager配置内容,如下:
2. 配置Node和Mysqld、redis、Nginx监控对象
# vim /etc/prometheus/prometheus.yml
增加监控对象配置内容,如下:
3. 验证
打开Prometheus的web (http://ip:9090),查看菜单:status -> targets
相关配置方法和参数意义请参考官方文档:
https://prometheus.io/docs/prometheus/2.45/configuration/configuration/
七、 常见问题排查
1.端口冲突:确保9090、9093、9100等端口未被占用
2.权限问题:确保Exporter有权限访问目标服务
3.配置错误:使用promtool check config prometheus.yml检查配置
4.防火墙:确保相关端口的防火墙已开放
八、总结
通过本文的步骤,你应该已经成功部署了一套完整的Prometheus监控系统,包括:
✅ Prometheus Server
✅ AlertManager告警管理
✅ Node Exporter服务器监控
✅ MySQLD Exporter数据库监控
✅ Redis Exporter缓存监控
✅ Nginx ExporterWeb服务器监控
这套组合拳能够为你的系统提供全方位的监控能力,为稳定性保障打下坚实基础!
你在使用Prometheus过程中遇到过哪些坑?或者有什么独家配置技巧?欢迎在评论区分享交流!
- 2022-03-22
- 2022-03-22
- 2022-03-22
- 2022-03-22
- 2022-03-18
- 2022-03-18