在现代运维工作中,对服务器状态进行实时监控并及时发出告警是保障系统稳定运行的关键。本文将详细讲解如何在 Ubuntu 系统上部署一套完整的 监控告警配置,使用开源工具 Prometheus、Grafana 和 Node Exporter,即使是 Linux 小白也能轻松上手。
确保你有一台运行 Ubuntu 20.04 或更高版本的服务器,并拥有 sudo 权限。建议先更新系统:
sudo apt update && sudo apt upgrade -yNode Exporter 是 Prometheus 官方提供的用于采集主机系统指标(如 CPU、内存、磁盘、网络等)的工具。我们先来部署它。
# 下载最新版 Node Exporter(以 v1.7.0 为例,请根据官网替换最新版本)wget https://github.com/prometheus/node_exporter/releases/download/v1.7.0/node_exporter-1.7.0.linux-amd64.tar.gz# 解压tar xvfz node_exporter-1.7.0.linux-amd64.tar.gz# 移动到系统目录sudo mv node_exporter-1.7.0.linux-amd64/node_exporter /usr/local/bin/# 创建专用用户(安全起见)sudo useradd --no-create-home --shell /bin/false node_exportersudo chown node_exporter:node_exporter /usr/local/bin/node_exporter接下来创建 systemd 服务,以便开机自启:
sudo tee /etc/systemd/system/node_exporter.service <启动服务:
sudo systemctl daemon-reexecsudo systemctl enable node_exportersudo systemctl start node_exporter验证是否成功运行(默认监听 9100 端口):
curl http://localhost:9100/metrics如果看到大量指标数据,说明 Node Exporter部署 成功!

Prometheus 是一个强大的开源监控和告警工具包,我们将用它来拉取并存储 Node Exporter 的数据。
# 创建用户和目录sudo useradd --no-create-home --shell /bin/false prometheussudo mkdir /etc/prometheus /var/lib/prometheussudo chown prometheus:prometheus /etc/prometheus /var/lib/prometheus# 下载 Prometheus(同样请替换为最新版本)wget 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 cp prometheus-2.45.0.linux-amd64/prometheus /usr/local/bin/sudo cp prometheus-2.45.0.linux-amd64/promtool /usr/local/bin/sudo chown prometheus:prometheus /usr/local/bin/prometheus /usr/local/bin/promtool创建基础配置文件 /etc/prometheus/prometheus.yml:
sudo tee /etc/prometheus/prometheus.yml <创建 systemd 服务:
sudo tee /etc/systemd/system/prometheus.service <启动 Prometheus:
sudo systemctl daemon-reexecsudo systemctl enable prometheussudo systemctl start prometheus访问 http://你的服务器IP:9090,即可看到 Prometheus Web UI。点击 “Status” → “Targets”,应能看到一个名为 “node” 的目标,状态为 UP。
Grafana 能将 Prometheus 中的数据以图表形式展示,让监控更直观。
# 添加官方仓库sudo apt-get install -y software-properties-commonwget -q -O - https://packages.grafana.com/gpg.key | sudo gpg --dearmor -o /usr/share/keyrings/grafana-keyring.gpgecho "deb [signed-by=/usr/share/keyrings/grafana-keyring.gpg] https://packages.grafana.com/oss/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list# 安装sudo apt updatesudo apt install grafana -y# 启动服务sudo systemctl enable grafana-serversudo systemctl start grafana-server访问 http://你的服务器IP:3000,默认账号密码均为 admin。首次登录会要求修改密码。
添加 Prometheus 作为数据源:
然后导入一个现成的 Node Exporter 仪表盘(ID: 1860),即可看到漂亮的 Grafana监控面板!
在 /etc/prometheus/prometheus.yml 同目录下创建告警规则文件:
sudo tee /etc/prometheus/alert.rules.yml <然后在 prometheus.yml 中引用该规则文件:
# 在 global 下添加rule_files: - "alert.rules.yml"重启 Prometheus 生效:
sudo systemctl restart prometheus现在,当服务器宕机超过 1 分钟,Prometheus 就会触发告警。你可以进一步集成 Alertmanager 实现邮件、微信等通知(本文暂不展开)。
通过以上步骤,你已成功在 Ubuntu 上完成了 Ubuntu监控告警配置 的基础搭建。这套组合(Prometheus + Node Exporter + Grafana)是业界标准方案,功能强大且完全免费。
记住四个核心关键词:Ubuntu监控告警配置、Prometheus Ubuntu安装、Grafana监控面板、Node Exporter部署。掌握它们,你就迈出了自动化运维的第一步!
快去试试吧,让你的服务器“看得见、管得住、报得准”!
本文由主机测评网于2025-12-17发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://www.vpshk.cn/2025129221.html