简介:本文详细介绍Prometheus监控系统的搭建与使用方法,涵盖环境准备、安装部署、配置管理、数据采集、告警规则设置及可视化展示等全流程,帮助读者快速掌握Prometheus的核心功能与实践技巧。
Prometheus是由SoundCloud开发的开源监控系统,现已成为CNCF(云原生计算基金会)毕业项目。其核心设计理念围绕”指标优先”展开,通过多维度数据模型和强大的查询语言PromQL,为现代分布式系统提供高效的监控能力。相比传统监控工具,Prometheus具有三大核心优势:
典型应用场景包括:
| 组件 | 最低配置 | 推荐配置 |
|---|---|---|
| CPU | 2核 | 4核 |
| 内存 | 2GB | 8GB |
| 存储 | 50GB(SSD优先) | 200GB(SSD) |
| 操作系统 | Linux 64位 | Linux 64位 |
| 安装方式 | 适用场景 | 优点 | 缺点 |
|---|---|---|---|
| 二进制包 | 物理机/虚拟机环境 | 配置灵活 | 需手动维护 |
| Docker容器 | 快速测试/容器化环境 | 部署简单 | 持久化存储需额外配置 |
| Kubernetes | 生产环境集群部署 | 自动扩缩容 | 需熟悉Operator机制 |
# 下载最新稳定版wget https://github.com/prometheus/prometheus/releases/download/v2.47.2/prometheus-2.47.2.linux-amd64.tar.gz# 解压并配置环境变量tar xvfz prometheus-*.tar.gzcd prometheus-*echo "export PATH=\$PATH:$(pwd)" >> ~/.bashrcsource ~/.bashrc# 验证安装prometheus --version
version: '3'services:prometheus:image: prom/prometheus:v2.47.2container_name: prometheusvolumes:- ./prometheus.yml:/etc/prometheus/prometheus.yml- prometheus-data:/prometheuscommand:- '--config.file=/etc/prometheus/prometheus.yml'- '--storage.tsdb.path=/prometheus'- '--web.enable-lifecycle'ports:- "9090:9090"restart: unless-stoppedvolumes:prometheus-data:
# prometheus.yml 示例global:scrape_interval: 15sevaluation_interval: 15sscrape_timeout: 10sscrape_configs:- job_name: 'prometheus'static_configs:- targets: ['localhost:9090']- job_name: 'node-exporter'static_configs:- targets: ['node1:9100', 'node2:9100']relabel_configs:- source_labels: [__address__]target_label: instancerule_files:- 'alert.rules'
通过发送SIGHUP信号或调用API实现配置更新:
# 方法1:发送信号kill -HUP <prometheus-pid># 方法2:API调用curl -X POST http://localhost:9090/-/reload
storage:tsdb:path: /prometheus/dataretention.time: 30dwal-compression: truemax-block-duration: 2hmin-block-duration: 2h
| 类型 | 典型Exporter | 监控对象 |
|---|---|---|
| 主机监控 | node_exporter | CPU/内存/磁盘/网络 |
| 数据库 | mysqld_exporter | MySQL性能指标 |
| 中间件 | redis_exporter | Redis缓存命中率 |
| 自定义 | jmx_exporter | Java应用JVM指标 |
# 安装Node Exporterwget https://github.com/prometheus/node_exporter/releases/download/v1.6.1/node_exporter-1.6.1.linux-amd64.tar.gztar xvfz node_exporter-*.tar.gzcd node_exporter-*./node_exporter# 配置systemd服务[Unit]Description=Node ExporterAfter=network.target[Service]User=prometheusExecStart=/usr/local/bin/node_exporterRestart=on-failure[Install]WantedBy=multi-user.target
通过Pushgateway实现短生命周期任务的监控:
# 安装Pushgatewaywget https://github.com/prometheus/pushgateway/releases/download/v1.6.1/pushgateway-1.6.1.linux-amd64.tar.gztar xvfz pushgateway-*.tar.gzcd pushgateway-*./pushgateway# 推送指标示例echo "test_metric 42" | curl --data-binary @- http://pushgateway:9091/metrics/job/test_job/instance/instance1
# alertmanager.yml 示例global:resolve_timeout: 5msmtp_smarthost: 'smtp.example.com:587'smtp_from: 'alert@example.com'smtp_auth_username: 'user'smtp_auth_password: 'password'route:group_by: ['alertname']group_wait: 30sgroup_interval: 5mrepeat_interval: 1hreceiver: 'email'receivers:- name: 'email'email_configs:- to: 'devops@example.com'send_resolved: true
# alert.rules 示例groups:- name: node.rulesrules:- alert: NodeMemoryUsageexpr: (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100 < 20for: 5mlabels:severity: warningannotations:summary: "Memory usage high on {{ $labels.instance }}"description: "Memory usage is {{ $value }}% on instance {{ $labels.instance }}"
# 抑制规则示例inhibit_rules:- source_match:severity: 'critical'target_match:severity: 'warning'equal: ['alertname', 'instance']
通过http://<prometheus-server>:9090/graph访问内置仪表盘,支持:
version: '3'services:grafana:image: grafana/grafana:10.2.2container_name: grafanaports:- "3000:3000"environment:- GF_SECURITY_ADMIN_USER=admin- GF_SECURITY_ADMIN_PASSWORD=passwordvolumes:- grafana-data:/var/lib/grafanarestart: unless-stoppedvolumes:grafana-data:
推荐配置的仪表盘模板:
--web.route-prefix和--query.lookback-delta参数实现分级存储
# 性能优化配置示例global:scrape_interval: 30sscrape_timeout: 20sevaluation_interval: 30sstorage:tsdb:retention.time: 90dwal-compression: trueno-lockfile: true
tls_server_config:cert_file: /etc/prometheus/server.crtkey_file: /etc/prometheus/server.key
basic_auth_users:admin: $2a$10$... # bcrypt加密密码
| 现象 | 可能原因 | 解决方案 |
|---|---|---|
| 目标不可达 | 网络防火墙限制 | 检查安全组规则 |
| 指标缺失 | Exporter未正确配置 | 验证Exporter日志 |
| 告警未触发 | 规则表达式错误 | 使用PromQL测试工具验证 |
| 内存溢出 | 存储保留期设置过长 | 调整retention.time参数 |
# 查看Prometheus启动日志journalctl -u prometheus -f# 启用调试日志--log.level=debug
# 查询耗时分析histogram_quantile(0.99, sum(rate(prometheus_engine_query_duration_seconds_bucket[5m])) by (le))# 内存使用监控process_resident_memory_bytes{job="prometheus"}
通过本文的系统性介绍,读者可以掌握从Prometheus基础部署到生产环境优化的完整知识体系。建议在实际应用中遵循”小步快跑”原则,先实现核心监控功能,再逐步完善告警和可视化体系。对于大型分布式系统,推荐结合Thanos实现全局视图和长期存储,构建企业级监控解决方案。