node exporter需按需启用collector、用systemd管理并正确配置prometheus target:关闭无用collector(如textfile、hwmon、rapl),用systemd以非root用户运行,target写真实ip而非localhost,验证metrics返回正常文本及关键指标存在。

Node Exporter 不是装上就完事的采集器,它默认开了一堆指标,但很多在生产环境根本用不上,反而拖慢采集、浪费存储。关键得按需开关 collector,再配好 systemd 服务和 Prometheus 抓取目标。
怎么启动 node_exporter 并限制只采集必要指标
直接运行 node_exporter 默认会启用全部支持的 collector(比如 textfile、hwmon、rapl),其中部分模块在普通服务器上没数据、响应慢或触发权限错误。应该显式关闭无用项:
- 常见可关项:
--no-collector.textfile(没配 textfile 目录时会报错)、--no-collector.hwmon(多数云主机无硬件传感器)、--no-collector.rapl(仅 Intel CPU 支持,且需 root 权限) - 保留核心项:CPU、内存、磁盘、网络、系统负载这些默认就开,不用额外加参数
- 如果只要最简集,用
--collector.disable-defaults+ 显式开启几个,例如:--collector.disable-defaults --collector.cpu --collector.meminfo --collector.diskstats - 端口别硬写死,用
--web.listen-address=":9100",避免被其他进程占掉
如何用 systemd 管理 node_exporter 进程
手动跑 ./node_exporter 只适合测试;生产必须用 systemd 管控生命周期、自动拉起、日志归集。注意三个易错点:
- 二进制路径要写绝对路径,比如
/usr/local/bin/node_exporter,不能写./node_exporter - 用户权限别用 root——新建
prometheus用户,用User=prometheus,再chown prometheus:prometheus /usr/local/bin/node_exporter - 启动命令里必须带上 collector 控制参数,例如:
ExecStart=/usr/local/bin/node_exporter --no-collector.textfile --no-collector.hwmon - 配置完记得
systemctl daemon-reload && systemctl enable --now node-exporter,否则不会开机自启
Prometheus.yml 里怎么正确添加 node_exporter target
很多人加完 target 后在 /targets 页面看到状态是 DOWN,多半是这三处错了:
- target 地址写成
localhost:9100——这是指 Prometheus 自己本机;如果是监控远端机器,必须写对方真实 IP,比如192.168.1.50:9100 - 没开防火墙端口:
firewall-cmd --permanent --add-port=9100/tcp && firewall-cmd --reload - job_name 命名带下划线(如
node_exporter)没问题,但别用点号或大写字母,某些旧版 Prometheus 会解析失败 - 建议加个简单 relabel 规则,把主机名打到 label 上:
relabel_configs:下加- source_labels: [__address__] target_label: instance replacement: $1,方便后续按机器筛选
验证 metrics 是否真正可用
别只看 /targets 显示 UP 就以为万事大吉。真实数据可能为空或格式异常:
- curl -s http://
IP:9100/metrics | head -20,确认返回的是纯文本指标,开头有# HELP和# TYPE行 - 查一个基础指标是否存在:
curl -s 'http://IP:9100/metrics' | grep '^node_cpu_seconds_total',没输出说明 collector 没生效或被禁用了 - 如果返回
403 Forbidden,检查web-config.yml是否误配了 TLS 或 Basic Auth,而 Prometheus 没配对应 credentials - 遇到
context deadline exceeded错误,大概率是 collector 卡住(比如systemdcollector 在容器环境查不到 unit),立刻加上--no-collector.systemd
collector 开关不是一次配完就永远安全的——内核升级、换云厂商、加新硬件都可能让某个原本安静的 collector 突然开始报错或拖慢整个抓取周期。每次变更后,盯紧 Prometheus 的 scrape_duration_seconds 和 up 指标,比看 dashboard 数值重要得多。










