1. Node Exporter 服务配置
1.1 Node Exporter systemd 服务文件
cat > /usr/lib/systemd/system/nodeexporter.service <<EOF
[Unit]
Description=prometheus node_exporter
After=network.target[Service]
Type=simple
ExecStart=/bin/node_exporter
KillMode=process[Install]
WantedBy=multi-user.target
EOF
1.2 启动Node Exporter服务
# 重新加载systemd配置
systemctl daemon-reload# 设置开机自启并立即启动
systemctl enable --now node-exporter
2. Prometheus 主配置文件
2.1 基础配置文件
cat > /app/tools/prometheus/prometheus.yml <<EOF
global:scrape_interval: 15sevaluation_interval: 15salerting:alertmanagers:- static_configs:- targets:rule_files:scrape_configs:- job_name: "oldboy_prometheus_server"static_configs:- targets: ["localhost:9090"]- job_name: "oldboy_basic_info_node_exporter"static_configs:- targets:- "prom.oldboylinux.cn:9100"- "gra.oldboylinux.cn:9100"
EOF
2.2 动态配置文件版本
cat > /app/tools/prometheus/prometheus.yml <<EOF
global:scrape_interval: 15sevaluation_interval: 15salerting:alertmanagers:- static_configs:- targets:rule_files:scrape_configs:- job_name: "oldboy_prometheus_server"static_configs:- targets: ["localhost:9090"]# - job_name: "oldboy_basic_info_node_exporter"# static_configs:# - targets:# - "prom.oldboylinux.cn:9100"# - "gra.oldboylinux.cn:9100"- job_name: "oldboy_basic_info_node_exporter_discovery"file_sd_configs:- files:- /app/tools/prometheus/discovery_node_exporter.jsonrefresh_interval: 5s
EOF
配置说明:
file_sd_configs: 动态读取与加载配置文件files: 指定要加载的配置文件路径refresh_interval: 配置文件读取间隔,设置为5秒
3. 动态配置文件
3.1 创建动态配置文件
cat > /app/tools/prometheus/discovery_node_exporter.json <<EOF
[{"targets": ["prom.oldboylinux.cn:9100","gra.oldboylinux.cn:9100"]}
]
EOF
4. 操作步骤总结
-
配置Node Exporter服务:
-
- 创建systemd服务文件
- 启动并启用服务
-
配置Prometheus:
-
- 可选择静态配置或动态配置方式
- 动态配置通过
file_sd_configs实现,便于批量管理主机
-
配置文件格式说明:
-
- 动态配置文件使用JSON格式
- 支持多个targets数组
- Prometheus会自动重新加载配置变更
5. 验证配置
# 检查配置文件语法
/app/tools/prometheus/promtool check config prometheus.yml# 重启Prometheus服务应用配置
systemctl restart prometheus# 查看服务状态
systemctl status prometheus
systemctl status node-exporter
这种配置方式特别适合大规模监控环境,可以轻松扩展和管理监控目标。