在运维工作中,及时发现服务器异常至关重要。本文将手把手教你如何在CentOS系统上部署一套完整的监控告警系统,使用开源工具 Prometheus 采集指标,并通过 Alertmanager 发送告警通知。即使你是 Linux 新手,也能轻松完成部署。
确保你的 CentOS 服务器满足以下条件:
Node Exporter 是 Prometheus 官方提供的用于采集主机系统指标(CPU、内存、磁盘、网络等)的工具。
# 下载并解压 Node Exporterwget https://github.com/prometheus/node_exporter/releases/download/v1.6.1/node_exporter-1.6.1.linux-amd64.tar.gztar xvfz node_exporter-1.6.1.linux-amd64.tar.gz# 移动到系统目录sudo mv node_exporter-1.6.1.linux-amd64/node_exporter /usr/local/bin/# 创建 systemd 服务sudo tee /etc/systemd/system/node_exporter.service < 验证是否成功:访问 http://你的服务器IP:9100/metrics,应能看到大量指标数据。
Prometheus 负责拉取指标、存储时序数据并触发告警规则。
# 下载 Prometheuswget https://github.com/prometheus/prometheus/releases/download/v2.45.0/prometheus-2.45.0.linux-amd64.tar.gztar xvfz prometheus-2.45.0.linux-amd64.tar.gz# 创建配置目录sudo mkdir -p /etc/prometheus /var/lib/prometheus# 复制二进制文件sudo cp prometheus-2.45.0.linux-amd64/prometheus /usr/local/bin/sudo cp prometheus-2.45.0.linux-amd64/promtool /usr/local/bin/# 复制配置文件模板sudo cp -r prometheus-2.45.0.linux-amd64/consoles /etc/prometheussudo cp -r prometheus-2.45.0.linux-amd64/console_libraries /etc/prometheus 创建主配置文件 /etc/prometheus/prometheus.yml:
global: scrape_interval: 15srule_files: - "alert.rules"scrape_configs: - job_name: 'node' static_configs: - targets: ['localhost:9100']alerting: alertmanagers: - static_configs: - targets: ['localhost:9093'] 创建告警规则文件 /etc/prometheus/alert.rules:
groups:- name: instance_down rules: - alert: InstanceDown expr: up == 0 for: 1m labels: severity: critical annotations: summary: "Instance {{ $labels.instance }} down" description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 1 minute."- name: high_cpu_load rules: - alert: HighCpuLoad expr: 100 - (avg by (instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 80 for: 2m labels: severity: warning annotations: summary: "High CPU load on {{ $labels.instance }}" description: "CPU load is above 80% for more than 2 minutes." 创建 Prometheus 的 systemd 服务:
sudo tee /etc/systemd/system/prometheus.service < Alertmanager 负责处理 Prometheus 发来的告警,并通过邮件、Webhook 等方式通知你。
# 下载 Alertmanagerwget https://github.com/prometheus/alertmanager/releases/download/v0.25.0/alertmanager-0.25.0.linux-amd64.tar.gztar xvfz alertmanager-0.25.0.linux-amd64.tar.gz# 安装二进制文件sudo cp alertmanager-0.25.0.linux-amd64/alertmanager /usr/local/bin/sudo cp alertmanager-0.25.0.linux-amd64/amtool /usr/local/bin/# 创建配置目录sudo mkdir -p /etc/alertmanager 编辑配置文件 /etc/alertmanager/alertmanager.yml(以邮件通知为例):
global: smtp_smarthost: 'smtp.qq.com:587' smtp_from: 'your_email@qq.com' smtp_auth_username: 'your_email@qq.com' smtp_auth_password: 'your_email_password_or_auth_code'route: receiver: 'email-notifications'receivers:- name: 'email-notifications' email_configs: - to: 'admin@example.com' 创建 Alertmanager 的 systemd 服务:
sudo tee /etc/systemd/system/alertmanager.service < 现在你可以通过浏览器访问以下地址:
http://你的服务器IP:9090http://你的服务器IP:9093http://你的服务器IP:9100/metrics如果一切正常,Prometheus 会自动抓取 Node Exporter 的数据,并在触发告警规则后,由 Alertmanager 发送邮件通知。
通过本教程,你已经成功在 CentOS 上部署了一套完整的 CentOS监控告警 系统。这套系统基于 服务器监控部署 的最佳实践,使用了 Prometheus安装配置 和 Alertmanager告警通知 两大核心组件,能够有效提升服务器运维效率和故障响应速度。
建议你根据实际业务需求调整告警规则,并考虑增加 Grafana 可视化面板以获得更直观的监控体验。
本文由主机测评网于2025-12-18发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://vpshk.cn/2025129604.html