以下是围绕 Prometheus 监控物理机 展开的完整技术指南,包含架构设计、部署流程、核心配置、实战案例及运维技巧等内容:
为何选择 Prometheus 监控物理机?
Prometheus 是一款开源监控系统,其设计理念天然适配物理机监控场景:
✅ 无侵入式采集:基于 HTTP 协议拉取指标,无需在被监控端安装复杂代理;
✅ 多维数据模型:支持以 {label}_name{key="value"}
形式定义动态标签,可区分不同机房/业务的物理机;
✅ 强大查询能力:PromQL 提供聚合、过滤、数学运算等功能,能快速定位性能瓶颈;
✅ 生态完善:配合 Alertmanager 实现告警收敛,Grafana 完成可视化,Blackbox Exporter 可扩展至 IPMI/BMC 等带外管理接口。
特性 | 传统监控方案(SNMP/Zabbix) | Prometheus |
---|---|---|
数据采集方式 | 主动推送/轮询 | 主动拉取 |
指标类型 | 静态计数器为主 | 直方图+摘要统计 |
标签体系 | 有限维度 | 任意维度动态扩展 |
存储引擎 | RRDTool/InfluxDB | 本地时序数据库 |
横向扩展能力 | 依赖中间件 | 联邦集群原生支持 |
物理机监控核心指标清单
通过 node_exporter
可获取以下关键指标分类:
CPU 类指标
指标名 | 说明 | 典型阈值建议 |
---|---|---|
node_cpu_seconds_total |
CPU 核心累计使用秒数 | 结合 mode="idle" 计算利用率 |
node_cpu_utilisation |
实时 CPU 使用率 (%) | >80% 持续 5min 触发告警 |
node_load1/load5/load15 |
1/5/15 分钟平均负载 | load1 > CPU 核数×2 |
内存类指标
指标名 | 说明 | 典型阈值建议 |
---|---|---|
node_memory_MemAvailable_bytes |
可用内存 | <总内存×10% 告警 |
node_memory_MemUsedPercent |
已用内存百分比 | >90% 告警 |
node_memory_Buffers_bytes |
PageCache + Slab 缓存 | 反映系统缓存效率 |
磁盘 I/O 指标
指标名 | 说明 | 典型阈值建议 |
---|---|---|
node_disk_reads_completed_total |
成功读操作次数 | 突增可能预示业务高峰 |
node_disk_written_bytes_total |
累计写入字节数 | 结合速率判断异常流量 |
node_disk_io_time_seconds_total |
IO 等待总时长 | >1s/次需优化存储介质 |
网络指标
指标名 | 说明 | 典型阈值建议 |
---|---|---|
node_network_receive_bytes_total |
接收总字节数 | 带宽利用率超 80% 告警 |
node_network_packets_dropped_total |
丢包总数 | 非零值即需关注 |
node_network_tcp_connections |
当前 TCP 连接数 | 超过最大文件描述符限制风险 |
部署实施步骤详解
环境准备
# CentOS/RHEL 示例 sudo useradd --no-create-home --shell /bin/false prometheus sudo mkdir -p /opt/prometheus /etc/prometheus sudo chown -R prometheus:prometheus /opt/prometheus /etc/prometheus
安装 Node Exporter
wget https://github.com/prometheus/node_exporter/releases/download/v1.6.1/node_exporter-1.6.1.linux-amd64.tar.gz tar xvf node_exporter-.tar.gz sudo mv node_exporter /usr/local/bin/ cat <<EOF | sudo tee /etc/systemd/system/node_exporter.service [Unit] Description=Node Exporter Service After=network.target [Service] ExecStart=/usr/local/bin/node_exporter --collector.filesystem.ignored-mount-points="^(/(dev|proc|sys|var/lib/docker)/)" --collector.netdev.ignored-devices="^(lo|tun|tap:.)" User=prometheus Group=prometheus Restart=on-failure [Install] WantedBy=multi-user.target EOF sudo systemctl daemon-reload && systemctl enable --now node_exporter
⚠️ 关键参数说明:
--collector.filesystem...
: 排除无关挂载点(如容器层)--collector.netdev...
: 忽略环回接口和虚拟网卡- 生产环境建议启用 TLS 加密(
--web.enable-tls
)
Prometheus 主程序配置
/etc/prometheus/prometheus.yml
核心片段:
global: scrape_interval: 30s # 默认采集间隔 evaluation_interval: 30s scrape_configs: job_name: 'physical-hosts' static_configs: targets: ['host1:9100', 'host2:9100'] # 替换为实际物理机 IP+端口 labels: environment: production datacenter: beijing
👉 进阶配置:
- 使用
relabel_config
实现动态服务发现(如 Consul/ETCD) - 添加
honor_labels: true
保留原始标签 - 配置
metric_relabel_configs
过滤敏感指标(如密码相关)
Alertmanager 告警规则示例
/etc/prometheus/alert.rules
:
groups: name: host-critical rules: alert: HighCPUUsage expr: avg(rate(node_cpu_seconds_total{mode="user"}[5m])) / avg(rate(node_cpu_seconds_total{mode="idle"}[5m])) 100 > 80 for: 5m labels: severity: critical annotations: summary: "{{ $labels.instance }} CPU 使用率过高" description: "过去5分钟平均使用率 {{ humanize $value }}%"
🔧 调优技巧:
- 使用
for
字段避免瞬时波动误报 - 通过
inhibit_rules
抑制重复告警(如同一主机多个容器异常) - 配置路由树实现分级通知(P1→值班人员,P2→团队邮件)
典型故障排查场景
场景1:Prometheus UI 看不到物理机数据
🔍 排查路径:
- 检查
node_exporter
日志:journalctl -u node_exporter -f
- 验证端口连通性:
telnet host1 9100
- 查看 Prometheus Targets 页面状态码是否为
UP
- 确认防火墙规则:
firewall-cmd --list-all
(CentOS)/ufw status
(Ubuntu) - 检查 SELinux 策略:
getenforce
→ 如果为 Enforcing,临时设为 Permissive 测试
场景2:磁盘空间快速增长
💡 解决方案:
- 调整 retention.resolution 根据需求降级精度(默认 15s→1h)
- 启用压缩算法:
storage: { ..., compression: { snappy: {} } }
- 定期执行
promtool compact
手动压缩块数据 - 分离冷热数据:将历史数据归档至对象存储(S3/GCS)
高级应用场景扩展
结合 IPMI/BMC 监控基板管理控制器
# 安装 Blackbox Exporter docker run -d --name=blackbox-exporter -p 9115:9115 prom/blackbox-exporter --config.file=/etc/blackbox_exporter.yaml
配置文件示例:/etc/blackbox_exporter.yaml
:
modules: bmc: walk: [table, odata => mib] lookup: source: hardware-platform:bmc-info -> target: bmc_info source: hardware-health:status -> target: health_status
🔗 价值体现:可获取电源状态、风扇转速、温度传感器等带外管理数据。
构建混合云统一监控视图
资源类型 | 采集方式 | 注意事项 |
---|---|---|
本地物理机 | node_exporter + textfile receiver | 确保 NTP 同步精确到毫秒级 |
VMware ESXi | esxi_exporter | 需启用 SSH 并分配只读角色 |
OpenStack | openstack_exporter | 依赖 Keystone 认证服务 |
AWS CloudWatch | remote read API | 注意跨账号权限配置 |
常见问题解答 (FAQs)
Q1: Prometheus 无法抓取某台物理机的指标怎么办?
A: 按以下顺序排查:
- 网络层:
nc -zv host1 9100
测试端口可达性; - 服务状态:
systemctl status node_exporter
确认进程运行; - 日志分析:查看
/var/log/messages
是否有拒绝连接记录; - 配置校验:执行
promtool check config /etc/prometheus/prometheus.yml
; - 特殊字符:若主机名含下划线,尝试用引号包裹
"host_name"
。
Q2: 如何监控没有公网 IP 的物理机?
A: 推荐两种方案:
① 反向代理方案:在跳板机部署 Nginx,配置反向代理转发请求;
② DNS SRV 记录:在内部 DNS 中为每台物理机创建 SRV 记录,指向其私有 IP;
③ Proxy Mode:修改 Prometheus 启动参数 --proxy-from=internal.domain
,允许跨域访问。
通过以上方案,可实现从基础资源监控到高级运维分析的全栈能力,实际部署时需根据业务特点调整采集频率(CPU/MEM 设为 30s,磁盘 I/O 设为 15s),并严格控制告警噪声比(建议 <1:10),对于超大规模集群(>500节点),建议采用 Thanos/Cortex 进行联邦聚合
原创文章,发布者:酷盾叔,转转请注明出处:https://www.kd.cn/ask/94597.html