Prometheus监控实战:从零搭建到监控Linux/Windows/MySQL全攻略
Prometheus监控实战:从零搭建到监控Linux/Windows/MySQL全攻略
在数字化转型浪潮中,系统监控已成为保障业务连续性的关键环节。Prometheus作为云原生时代的监控标杆,其多维数据模型和灵活的查询语言正在重塑运维监控体系。本文将带您从零开始,构建覆盖Linux主机、Windows服务器和MySQL数据库的全栈监控方案,每个环节都经过生产环境验证。
1. 环境准备与部署策略选择
1.1 硬件与系统要求
部署Prometheus前需确保:
- 最低配置:2核CPU/4GB内存/50GB存储(可保留15天监控数据)
- 推荐配置:4核CPU/8GB内存/200GB SSD(适用于每秒10万指标采集)
- 网络要求:被监控节点需开放相应端口(默认9090/tcp)
不同规模下的资源配置参考:
| 监控目标规模 | CPU核心 | 内存(GB) | 存储(GB) | 预估指标量/秒 |
|---|---|---|---|---|
| 小型(≤50节点) | 2 | 4 | 50 | 10,000 |
| 中型(≤200节点) | 4 | 8 | 200 | 50,000 |
| 大型(≥500节点) | 8 | 16 | 500+ | 100,000+ |
1.2 部署方式对比
二进制部署优势在于:
- 资源占用更少
- 适合长期稳定运行的生产环境
- 直接控制系统服务
容器化部署更适合:
- 快速搭建测试环境
- 需要版本灵活切换的场景
- Kubernetes集群内部署
提示:生产环境建议使用二进制部署配合systemd守护进程,可获得更好的稳定性
2. 二进制部署实战
2.1 安装流程精要
# 下载最新稳定版(示例为2.37.0) wget https://github.com/prometheus/prometheus/releases/download/v2.37.0/prometheus-2.37.0.linux-amd64.tar.gz # 解压并创建符号链接 tar xvf prometheus-*.tar.gz -C /usr/local/ ln -sv /usr/local/prometheus-2.37.0.linux-amd64 /usr/local/prometheus # 创建专用用户和数据目录 useradd -M -s /sbin/nologin prometheus mkdir /var/lib/prometheus chown -R prometheus:prometheus /usr/local/prometheus /var/lib/prometheus2.2 系统服务配置关键参数
[Unit] Description=Prometheus Monitoring System After=network.target [Service] User=prometheus ExecStart=/usr/local/prometheus/prometheus \ --config.file=/usr/local/prometheus/prometheus.yml \ --storage.tsdb.path=/var/lib/prometheus \ --web.enable-lifecycle \ --web.external-url=http://yourdomain.com/prometheus \ --storage.tsdb.retention.time=30d Restart=always RestartSec=3 [Install] WantedBy=multi-user.target关键启动参数解析:
--web.enable-lifecycle:启用配置热重载(POST请求/-/reload端点)--storage.tsdb.retention.time:数据保留周期(默认15d)--web.external-url:反向代理场景下的公共访问地址
3. 容器化部署方案
3.1 Docker Compose最佳实践
version: '3' services: prometheus: image: prom/prometheus:v2.37.0 container_name: prometheus restart: always volumes: - ./prometheus.yml:/etc/prometheus/prometheus.yml - prometheus_data:/prometheus ports: - "9090:9090" command: - '--config.file=/etc/prometheus/prometheus.yml' - '--storage.tsdb.path=/prometheus' - '--web.enable-lifecycle' volumes: prometheus_data:3.2 配置文件热加载技巧
修改配置后无需重启容器:
curl -X POST http://localhost:9090/-/reload注意:需确保容器启动时已添加
--web.enable-lifecycle参数
4. Linux主机监控实战
4.1 Node Exporter深度配置
高级采集项启用示例:
/usr/local/node_exporter/node_exporter \ --collector.systemd \ --collector.processes \ --collector.tcpstat \ --collector.meminfo_numa常用采集器说明:
| 采集器名称 | 作用描述 | 默认启用 |
|---|---|---|
| node_cpu | CPU使用情况统计 | 是 |
| node_disk | 磁盘I/O指标 | 是 |
| node_netstat | 网络连接状态统计 | 否 |
| node_systemd | systemd单元状态监控 | 否 |
| node_textfile | 读取自定义指标文本文件 | 是 |
4.2 安全加固方案
- 启用HTTPS基础认证:
basic_auth_users: [ username: "$(htpasswd -nB admin)" ]- 限制访问IP:
iptables -A INPUT -p tcp --dport 9100 -s 192.168.1.0/24 -j ACCEPT iptables -A INPUT -p tcp --dport 9100 -j DROP5. Windows服务器监控
5.1 WMI Exporter定制安装
# 下载最新版本 Invoke-WebRequest -Uri "https://github.com/martinlindhe/wmi_exporter/releases/download/v0.16.0/wmi_exporter-0.16.0-amd64.msi" -OutFile "wmi_exporter.msi" # 安装时启用特定采集器 msiexec /i wmi_exporter.msi ENABLED_COLLECTORS="cpu,memory,net,logical_disk,os" LISTEN_PORT=50005.2 关键性能指标对照表
| 指标名称 | 对应Windows性能计数器 |
|---|---|
| wmi_cpu_time_total | \Processor(_Total)% Processor Time |
| wmi_memory_available_bytes | \Memory\Available Bytes |
| wmi_net_bytes_total | \Network Interface(*)\Bytes Total/sec |
| wmi_logical_disk_free_bytes | \LogicalDisk(*)\Free Megabytes |
6. MySQL数据库监控
6.1 权限精细控制方案
CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'ComplexPassword123!'; GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost'; GRANT SELECT ON performance_schema.* TO 'exporter'@'localhost';6.2 关键监控指标解析
查询吞吐监控:
rate(mysql_global_status_questions_total[1m])连接池健康度:
mysql_global_variables_max_connections - mysql_global_status_threads_connected慢查询分析:
increase(mysql_global_status_slow_queries[1h])7. 配置管理进阶技巧
7.1 基于文件的服务发现
scrape_configs: - job_name: 'node' file_sd_configs: - files: - '/etc/prometheus/targets/nodes/*.json' refresh_interval: 5m示例target文件:
[ { "targets": ["192.168.1.10:9100"], "labels": { "env": "production", "role": "web-server" } } ]7.2 记录规则优化查询性能
rule_files: - '/etc/prometheus/rules/*.yml' # 示例rules/db.rules.yml groups: - name: db.rules rules: - record: instance:mysql_uptime:avg expr: avg_over_time(mysql_global_status_uptime[1h])8. 故障排查指南
8.1 常见问题速查表
| 现象 | 可能原因 | 解决方案 |
|---|---|---|
| Target状态为DOWN | 防火墙阻断/服务未运行 | 检查端口连通性和服务状态 |
| 指标缺失 | Exporter未启用对应采集器 | 检查启动参数和采集器配置 |
| PromQL查询超时 | 查询时间范围过大 | 添加时间限制或使用记录规则 |
| 内存占用持续增长 | 指标基数过高 | 优化标签使用和指标过滤 |
8.2 诊断命令工具箱
# 检查指标暴露情况 curl http://target:port/metrics | grep -i key_metric # 测试Prometheus配置 promtool check config prometheus.yml # 分析TSDB状态 promtool tsdb analyze /path/to/data