在现代IT基础设施中,Ubuntu集群监控是保障系统稳定运行的关键环节。对于运维人员或刚入门的小白来说,如何高效地监控多台Ubuntu服务器并在异常时及时收到告警,是一项必备技能。本文将带你从零开始,使用开源工具 Prometheus 和 Grafana 搭建一套完整的Linux服务器集群运维监控与告警系统。
- Prometheus:功能强大的开源监控和告警工具,原生支持时间序列数据采集,适合动态云环境。
- Grafana:优秀的可视化平台,可将 Prometheus 的数据以图表形式展示,支持自定义仪表盘。
这套组合被广泛用于生产环境,社区活跃、文档丰富,非常适合初学者上手。
假设你有以下环境:
Node Exporter 用于采集 Linux 系统指标(CPU、内存、磁盘等),需在每台被监控的 Ubuntu 服务器上安装。
wget https://github.com/prometheus/node_exporter/releases/download/v1.7.0/node_exporter-1.7.0.linux-amd64.tar.gztar xvfz node_exporter-1.7.0.linux-amd64.tar.gzcd node_exporter-1.7.0.linux-amd64sudo cp node_exporter /usr/local/bin/# 创建 systemd 服务sudo tee /etc/systemd/system/node_exporter.service <<EOF[Unit]Description=Node ExporterAfter=network.target[Service]User=rootExecStart=/usr/local/bin/node_exporter[Install]WantedBy=multi-user.targetEOFsudo systemctl daemon-reloadsudo systemctl start node_exportersudo systemctl enable node_exporter 完成后,在浏览器访问 http://你的节点IP:9100/metrics,能看到指标数据即表示安装成功。
# 下载并解压 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.gzcd prometheus-2.45.0.linux-amd64# 编辑配置文件 prometheus.ymlsudo tee prometheus.yml <<EOFglobal: scrape_interval: 15sscrape_configs: - job_name: 'ubuntu-nodes' static_configs: - targets: ['192.168.1.10:9100', '192.168.1.11:9100'] # 替换为你的节点IPEOF# 启动 Prometheus./prometheus --config.file=prometheus.yml 启动后访问 http://监控服务器IP:9090,进入 Prometheus Web UI,点击 “Status” → “Targets”,应看到所有节点状态为 UP。
Prometheus 本身不发送告警,需配合 Alertmanager。先创建告警规则文件 alert.rules.yml:
groups:- name: instance-health rules: - alert: HighCpuUsage expr: 100 - (avg by(instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 80 for: 2m labels: severity: warning annotations: summary: "High CPU usage on {{ $labels.instance }}" description: "CPU usage is above 80% for more than 2 minutes." - alert: LowMemory expr: (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100 < 10 for: 2m labels: severity: critical annotations: summary: "Low memory on {{ $labels.instance }}" description: "Available memory is less than 10%." 然后修改 prometheus.yml,加入 rule_files 和 alerting 配置:
rule_files: - "alert.rules.yml"alerting: alertmanagers: - static_configs: - targets: ['localhost:9093'] 接着安装并启动 Alertmanager(略,可参考官方文档),即可实现邮件、Webhook 等方式告警。
sudo apt-get updatesudo apt-get install -y apt-transport-https software-properties-common wgetwget -q -O - https://packages.grafana.com/gpg.key | sudo gpg --dearmor -o /usr/share/keyrings/grafana.gpgecho "deb [signed-by=/usr/share/keyrings/grafana.gpg] https://packages.grafana.com/oss/deb stable main" | sudo tee /etc/apt/sources.list.d/grafana.listsudo apt-get updatesudo apt-get install grafanasudo systemctl start grafana-serversudo systemctl enable grafana-server 访问 http://监控服务器IP:3000,默认账号密码为 admin/admin。添加 Prometheus 作为数据源(URL 填 http://localhost:9090),然后导入 Dashboard ID(如 11074)即可看到漂亮的 Ubuntu 集群监控面板。
通过本教程,你已成功搭建了一套基于 Prometheus告警设置 和 Grafana可视化监控 的 Ubuntu集群监控系统。这套方案不仅免费开源,而且高度可扩展,适用于从小型实验室到大型生产环境的 Linux服务器集群运维场景。建议后续深入学习 PromQL 查询语言和 Grafana 自定义面板,进一步提升监控能力。
祝你运维顺利,系统稳如泰山!
本文由主机测评网于2025-12-04发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://www.vpshk.cn/2025122750.html