Prometheus 和 node_exporter 安装指南
15 分钟阅读 - 2026年5月29日

安装 Prometheus 和 node_exporter、配置 scrape 目标、设置 systemd 服务并确保监控堆栈的安全。分步进行 Linux 操作。
Prometheus 和 node_exporter 服务器监控配置
Prometheus 用于采集并存储时间序列指标。node_exporter 负责向 Prometheus 提供 CPU、内存和磁盘使用率等系统级数据以便其采集。本指南涵盖两者的安装、采集目标的配置、将 node_exporter 作为 systemd 服务运行以及访问权限的限制。
安装 node_exporter
从官方 GitHub 发布页面下载最新稳定版本。截至 2026 年 5 月,最新版本为 1.11.1。请将 amd64 替换为 arm64 。
wget https://github.com/prometheus/node_exporter/releases/download/v1.11.1/node_exporter-1.11.1.linux-amd64.tar.gz
将 SHA256 校验和与发布页面上的值进行核对,然后解压并安装:
tar -xzvf node_exporter-1.11.1.linux-amd64.tar.gz
sudo mv node_exporter-1.11.1.linux-amd64/node_exporter /usr/local/bin/
创建一个专用的系统用户,该用户不设主目录且无登录 shell:
sudo useradd --no-create-home --shell /bin/false node_exporter
sudo chown node_exporter:node_exporter /usr/local/bin/node_exporter
通过运行 /usr/local/bin/node_exporter ,然后检查输出:
curl http://localhost:9100/metrics
您应看到以 node_,其中包含诸如 node_cpu_seconds_total 和 node_memory_MemAvailable_bytes等指标。默认情况下,node_exporter 会暴露约 500 个时间序列。
将 node_exporter 作为 systemd 服务运行
在终端中运行 node_exporter 适合用于测试,但关闭会话后它就会停止。请在 /etc/systemd/system/node_exporter.service:
[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target
[Service]
User=node_exporter
Group=node_exporter
Type=simple
Restart=on-failure
RestartSec=5
ExecStart=/usr/local/bin/node_exporter
[Install]
WantedBy=multi-user.target
启用并启动服务:
sudo systemctl daemon-reload
sudo systemctl enable node_exporter
sudo systemctl start node_exporter
使用以下命令确认服务是否正在运行 sudo systemctl status node_exporter。输出应显示 active (running).
如果 Prometheus 运行在同一主机上,请通过修改 ExecStart 行:
ExecStart=/usr/local/bin/node_exporter --web.listen-address="127.0.0.1:9100"
配置 Prometheus 以抓取 node_exporter
打开 /etc/prometheus/prometheus.yml 并添加一个任务到 scrape_configs:
scrape_configs:
- job_name: 'node_exporter'
scrape_interval: 15s
static_configs:
- targets: ['localhost:9100']
labels:
env: 'production'
该 job_name 用于在查询和仪表盘中标识数据源。 targets 指向 node_exporter 监听的主机和端口。诸如 env 可帮助您在后续筛选指标。
重启前请验证配置:
promtool check config /etc/prometheus/prometheus.yml
如果验证通过,请在不影响服务的情况下重新加载 Prometheus:
sudo systemctl reload prometheus
打开 http://<your-prometheus-ip>:9090,转至“状态 > 目标”,确认 node_exporter 任务显示为绿色的“UP”状态。在表达式浏览器中运行类似 node_cpu_seconds_total ,以确认数据正在流动。
保障监控架构的安全
切勿将 9090 或 9100 端口暴露在公共互联网上。在 Ubuntu/Debian 系统上,请将 node_exporter 的访问权限限制为 Prometheus 服务器的 IP 地址:
sudo ufw allow from <prometheus-ip> to any port 9100
在 CentOS/RHEL 系统(使用 firewalld):
firewall-cmd --permanent --add-port=9100/tcp
对于 Prometheus Web 界面,请将其部署在 Nginx 等反向代理之后,并启用基本认证和 TLS。如果您需要从多个位置访问系统而不直接暴露端口,Tailscale 等网状 VPN 也是另一种选择。
监控最佳实践与后续步骤
请使用 node_memory_MemAvailable_bytes 代替 MemFree 来表示内存警报。 MemAvailable 将缓冲区和缓存纳入计算,从而更准确地反映实际可用空间。
使用 --no-collector.<name> 来减少干扰。
对于磁盘空间警报, predict_linear PromQL 函数可让您根据当前趋势预测卷何时会满。设置 7 天的预测窗口,可在缓慢泄漏导致服务中断之前及时发现。
要监控多台服务器,请在每台机器上安装 node_exporter,并将它们的 IP 地址添加到 targets 列表中 prometheus.yml。对于大型环境,请改用基于文件的服务发现机制,而非硬编码 IP 地址。
添加 Grafana 可获得可视化仪表盘。Node Exporter Full 仪表盘(ID 1860)是一个不错的起点。Alertmanager 将关键警报转发至 Slack、电子邮件或 PagerDuty。
FDC 的专用服务器和 VPS 方案开箱即支持 Prometheus 和 node_exporter。请参阅 FDC 的专用服务器选项。
故障排除
| 问题 | 可能原因 | 检查命令 |
|---|---|---|
| 服务无法启动 | 二进制文件路径或权限错误 | journalctl -u node_exporter -xe |
| 无法访问指标 | 防火墙阻塞了 9100 端口,或绑定地址错误 | ss -lntp | grep 9100 |
| Prometheus 中的目标不可用 | 网络问题或 prometheus.yml 中的目标 IP 错误 | curl -I http://<target-ip>:9100/metrics |
| 缺少特定指标 | 收集器默认处于禁用状态 | node_exporter --help |

Prometheus 和 node_exporter 安装指南
安装 Prometheus 和 node_exporter、配置 scrape 目标、设置 systemd 服务并确保监控堆栈的安全。分步进行 Linux 操作。
15 分钟阅读 - 2026年5月29日
用于 Linux 数据包处理的 XDP 和 eBPF
14 分钟阅读 - 2026年5月27日