使用 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 上。

bash 复制代码
# 创建 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

bash 复制代码
# 下载 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 服务

bash 复制代码
sudo vim /etc/systemd/system/node_exporter.service

添加以下内容:

ini 复制代码
[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 服务

bash 复制代码
# 重新加载 systemd 配置
sudo systemctl daemon-reload

# 启动服务
sudo systemctl start node_exporter

# 设置开机自启
sudo systemctl enable node_exporter

# 检查服务状态
sudo systemctl status node_exporter

5. 防火墙配置

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

bash 复制代码
# 开放9100端口
sudo firewall-cmd --permanent --add-port=9100/tcp

# 重新加载防火墙配置
sudo firewall-cmd --reload

配置 Prometheus Server

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

修改 Prometheus 配置文件

编辑 Prometheus Server 上的配置文件:

bash 复制代码
sudo vim /usr/local/prometheus/prometheus.yml

scrape_configs 部分添加或修改配置:

yaml 复制代码
# 全局配置
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 服务

bash 复制代码
# 检查配置文件语法
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 使用率
promql 复制代码
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): 计算使用率百分比
其他常用查询

内存使用率:

promql 复制代码
100 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes * 100)

磁盘使用率:

promql 复制代码
100 - (node_filesystem_avail_bytes{mountpoint="/"} / node_filesystem_size_bytes{mountpoint="/"} * 100)

系统负载:

promql 复制代码
node_load1

高级配置

多节点监控

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

yaml 复制代码
- job_name: "node"
  static_configs:
    - targets: 
        - "node1-ip:9100"
        - "node2-ip:9100"
        - "node3-ip:9100"
      labels:
        group: "production-servers"

使用文件服务发现

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

yaml 复制代码
- 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 端点验证数据:

bash 复制代码
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 可视化这些监控数据,创建直观的监控仪表盘。

相关资源:

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

相关推荐
用户0328472220708 小时前
如何搭建本地yum源(上)
运维
虚无境3 天前
如何编写一个SpringBoot项目告警推送的Starter
java·prometheus·webhook
大树883 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠3 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质3 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
小宇宙Zz3 天前
Maven依赖冲突
java·服务器·maven
Inhand陈工3 天前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
酣大智3 天前
ARP代理--工作原理
运维·网络·arp·arp代理
shushangyun_3 天前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
古城小栈3 天前
Unix 与 Linux 异同小叙
linux·服务器·unix