当前位置: 首页 > backend >正文

使用 Prometheus 监控服务器节点:Node Exporter 详解与配置

前言

在上一篇文章中,我们介绍了如何在 CentOS 上部署 Prometheus 并使用 systemd 进行管理。本文将继续深入,讲解如何使用 Prometheus 监控服务器节点,重点介绍 Node Exporter 的作用、安装和配置方法。

Node Exporter 是 Prometheus 生态系统中用于收集服务器硬件和操作系统指标的官方组件,它暴露了丰富的系统指标,使我们可以全面监控服务器的运行状态。


Node Exporter 简介

什么是 Node Exporter?

Node Exporter 是一个 Prometheus 的 exporter,专门用于收集 *NIX 内核公开的硬件和操作系统指标。它不会收集应用程序级别的指标,而是专注于系统层面的监控数据,包括:

  • CPU 使用情况
  • 内存利用率
  • 磁盘 I/O 统计
  • 网络接口统计
  • 文件系统使用情况
  • 系统负载
  • 硬件温度(如果可用)
  • 服务状态(systemd)

为什么需要 Node Exporter?

当 Prometheus Server 部署在一台机器上时,它只能收集自身指标。要监控其他服务器节点,需要在每个节点上运行 Node Exporter,使其暴露系统指标,然后由 Prometheus Server 定期抓取这些指标。


部署 Node Exporter

1. 在目标节点上创建用户和目录

注意: 这些操作需要在被监控的服务器节点上执行,而不是在 Prometheus Server 上。

# 创建 Prometheus 组
sudo groupadd prometheus# 创建 Prometheus 用户
sudo useradd -g prometheus -m -d /var/lib/prometheus -s /sbin/nologin prometheus# 创建 Prometheus 目录
sudo mkdir -p /usr/local/prometheus/
sudo chown -R prometheus:prometheus /usr/local/prometheus/

命令解释:

  • groupadduseradd: 创建专用用户和组,提高安全性
  • mkdir -p: 递归创建目录
  • chown -R: 递归更改目录所有权

2. 下载并安装 Node Exporter

# 下载 Node Exporter
wget https://github.com/prometheus/node_exporter/releases/download/v1.9.1/node_exporter-1.9.1.linux-amd64.tar.gz# 解压
tar -zxvf node_exporter-1.9.1.linux-amd64.tar.gz# 移动到安装目录
mv node_exporter-1.9.1.linux-amd64 /usr/local/prometheus/node_exporter# 更改所有权
chown -R prometheus:prometheus /usr/local/prometheus/node_exporter

3. 创建 Systemd 服务

sudo vim /etc/systemd/system/node_exporter.service

添加以下内容:

[Unit]
Description=node_exporter
Documentation=https://github.com/prometheus/node_exporter
After=network.target[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/prometheus/node_exporter/node_exporter
Restart=on-failure[Install]
WantedBy=multi-user.target

配置解释:

  • Description: 服务描述
  • Documentation: 相关文档链接
  • After: 指定启动顺序依赖
  • Type: 服务类型(simple 表示直接启动主进程)
  • User: 指定运行服务的用户
  • ExecStart: 启动命令
  • Restart: 故障时自动重启
  • WantedBy: 指定服务所属 target

4. 启动 Node Exporter 服务

# 重新加载 systemd 配置
sudo systemctl daemon-reload# 启动服务
sudo systemctl start node_exporter# 设置开机自启
sudo systemctl enable node_exporter# 检查服务状态
sudo systemctl status node_exporter

5. 防火墙配置

如果系统启用了防火墙,需要开放 Node Exporter 的默认端口(9100):

# 开放9100端口
sudo firewall-cmd --permanent --add-port=9100/tcp# 重新加载防火墙配置
sudo firewall-cmd --reload

配置 Prometheus Server

现在我们需要在 Prometheus Server 上配置,使其能够抓取 Node Exporter 暴露的指标。

修改 Prometheus 配置文件

编辑 Prometheus Server 上的配置文件:

sudo vim /usr/local/prometheus/prometheus.yml

scrape_configs 部分添加或修改配置:

# 全局配置
global:scrape_interval: 15s # 每15秒抓取一次指标evaluation_interval: 15s # 每15秒评估一次规则# 告警配置
alerting:alertmanagers:- static_configs:- targets:# - alertmanager:9093# 规则文件配置
rule_files:# - "first_rules.yml"# - "second_rules.yml"# 抓取配置
scrape_configs:# 监控 Prometheus 自身- job_name: "prometheus"static_configs:- targets: ["localhost:9090"]labels:app: "prometheus"# 监控节点- job_name: "node"static_configs:- targets: ["localhost:9090", "被监控节点IP:9100"]labels:app: "node-exporter"

关键配置解释:

  • scrape_configs: 定义抓取目标配置
  • job_name: 任务名称,用于分组相关目标
  • static_configs: 静态目标配置
  • targets: 指定要监控的目标地址和端口
    • localhost:9090: Prometheus 自身
    • 被监控节点IP:9100: Node Exporter 节点(将"被监控节点IP"替换为实际IP)

重启 Prometheus 服务

# 检查配置文件语法
sudo -u prometheus /usr/local/prometheus/promtool check config /usr/local/prometheus/prometheus.yml# 重启 Prometheus
sudo systemctl restart prometheus# 检查服务状态
sudo systemctl status prometheus

验证监控

1. 访问 Prometheus Web 界面

在浏览器中打开 http://Prometheus服务器IP:9090,转到 “Status” -> “Targets” 页面。你应该能看到两个目标:

  • prometheus (localhost:9090) 状态为 UP
  • node (被监控节点IP:9100) 状态为 UP

如果状态为 DOWN,请检查网络连接和防火墙设置。

2. 查询节点指标

在 Prometheus 的 “Graph” 页面中,可以尝试查询各种节点指标:

查询 CPU 使用率
100 - (avg by(instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)

查询解释:

  • node_cpu_seconds_total: Node Exporter 提供的 CPU 时间指标
  • mode="idle": 筛选空闲状态的 CPU 时间
  • irate([5m]): 计算最近5分钟内的瞬时增长率
  • avg by(instance): 按实例分组计算平均值
  • 100 - (... * 100): 计算使用率百分比
其他常用查询

内存使用率:

100 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes * 100)

磁盘使用率:

100 - (node_filesystem_avail_bytes{mountpoint="/"} / node_filesystem_size_bytes{mountpoint="/"} * 100)

系统负载:

node_load1

高级配置

多节点监控

当需要监控多个节点时,可以修改 prometheus.yml 中的 targets 列表:

- job_name: "node"static_configs:- targets: - "node1-ip:9100"- "node2-ip:9100"- "node3-ip:9100"labels:group: "production-servers"

使用文件服务发现

对于大规模环境,建议使用文件服务发现而不是静态配置:

- job_name: "node"file_sd_configs:- files:- /usr/local/prometheus/targets/nodes/*.json

然后创建对应的 JSON 文件定义目标。


故障排除

常见问题

  1. 连接被拒绝

    • 检查 Node Exporter 是否正在运行
    • 验证防火墙设置
  2. 无数据

    • 检查 Prometheus 配置中的目标地址是否正确
    • 验证网络连通性
  3. 权限问题

    • 确保 Node Exporter 以正确用户身份运行
    • 检查目录和文件权限

查看 Node Exporter 指标

可以直接访问 Node Exporter 的 metrics 端点验证数据:

curl http://被监控节点IP:9100/metrics

这将显示 Node Exporter 暴露的所有原始指标。


总结

通过本文,我们学习了:

  1. Node Exporter 的作用:收集服务器硬件和操作系统指标
  2. 安装和配置 Node Exporter:在被监控节点上部署并配置为 systemd 服务
  3. 配置 Prometheus Server:添加节点监控任务
  4. 查询节点指标:使用 PromQL 查询 CPU 使用率等关键指标

Node Exporter 是 Prometheus 监控体系中的重要组件,它使我们能够全面了解服务器的运行状态。结合 Prometheus 的强大查询能力和 Alertmanager 的告警功能,可以构建完整的服务器监控和告警系统。

在下一篇文章中,我们将介绍如何使用 Grafana 可视化这些监控数据,创建直观的监控仪表盘。

相关资源:

  • Node Exporter GitHub 仓库
  • Prometheus 查询文档
  • Node Exporter 常用查询

希望本文对您使用 Prometheus 监控服务器节点有所帮助!如有任何问题,欢迎在评论区留言讨论。

http://www.xdnf.cn/news/18675.html

相关文章:

  • 实时监测蒸汽疏水阀的工作状态的物联网实时监控平台技术解析
  • 容器学习day02
  • 基于 OpenCV 与 Mediapipe 的二头肌弯举追踪器构建指南:从环境搭建到实时计数的完整实现
  • 力扣498 对角线遍历
  • 4G模块 EC200通过MQTT协议连接到阿里云
  • (LeetCode 每日一题) 498. 对角线遍历 (矩阵、模拟)
  • 撤回git 提交
  • 【龙泽科技】汽车车身测量与校正仿真教学软件【赛欧+SHARK】
  • 什么是共模抑制比?
  • 三坐标如何实现测量稳定性的提升
  • RustFS在金融行业的具体落地案例中,是如何平衡性能与合规性要求的?
  • WRC2025 | 澳鹏亮相2025世界机器人大会,以数据之力赋能具身智能新纪元
  • 大数据毕业设计选题推荐-基于大数据的餐饮服务许可证数据可视化分析系统-Spark-Hadoop-Bigdata
  • LevelDB SSTable模块
  • Consul 在 Windows 上的启动方法
  • 【ACP】2025-最新-疑难题解析-6
  • pytest+requests+Python3.7+yaml+Allure+Jenkins+docker实现接口自动化测试
  • 消息中间件RabbitMQ03:结合WebAPI实现点对点(P2P)推送和发布-订阅推送的Demo
  • 软考中级网络工程师通关指南:从学习到实战
  • 04-Maven工具介绍
  • 从0开始学习Java+AI知识点总结-25.web实战(AOP)
  • KEPServerEX——工业数据采集与通信的标准化平台
  • 服务器(Linux)新账户搭建Pytorch深度学习环境
  • Devops之Jenkins:Jenkins服务器中的slave节点是什么?我们为什么要使用slave节点?如何添加一个windows slave节点?
  • 云计算之中间件与数据库
  • 机器学习:贝叶斯派
  • 2025年金九银十Java面试场景题大全:高频考点+深度解析+实战方案
  • 【C++详解】哈希表概念与实现 开放定址法和链地址法、处理哈希冲突、哈希函数介绍
  • Linux 进阶之性能调优,文件管理,网络安全
  • Java 22 新特性及具体应用