一、Prometheus 是什么

Prometheus 是由 SoundCloud 开发、后被 CNCF(Cloud Native Computing Foundation)接管的 开源监控与告警系统
它是 Kubernetes 生态的核心监控组件,专为云原生环境设计,具备 高性能、可扩展、易集成 的特性。

一句话概括:

Prometheus = 时间序列数据库 + 实时监控引擎 + 告警系统。


二、Prometheus 的核心特性

特性描述
多维度数据模型使用标签(Label)管理监控数据
高效时序数据库(TSDB)自动压缩与索引存储
灵活查询语言(PromQL)强大的聚合与分析能力
无代理采集通过 HTTP Pull 模式主动拉取数据
告警集成 Alertmanager支持分组、静默、告警路由
与 Grafana 完美配合可视化展示监控数据
服务自动发现支持 Kubernetes、Consul、EC2 等动态环境

三、安装与启动

1. 下载 Prometheus

wget https://github.com/prometheus/prometheus/releases/download/v2.52.0/prometheus-2.52.0.linux-amd64.tar.gz tar -zxvf prometheus-2.52.0.linux-amd64.tar.gz cd prometheus-2.52.0.linux-amd64

2. 启动

./prometheus --config.file=prometheus.yml

访问界面:


http://localhost:9090


四、Prometheus 配置文件结构

prometheus.yml


global: scrape_interval: 15s scrape_configs: - job_name: "prometheus" static_configs: - targets: ["localhost:9090"]

解释:

  • scrape_interval: 数据采集间隔;

  • job_name: 任务名称;

  • targets: 被监控的目标地址。


五、监控系统组件架构


+------------------------+ | Prometheus | | - 数据抓取 (Scrape) | | - 时序存储 (TSDB) | | - PromQL 查询引擎 | +-----------+------------+ | ↓ +------------------------+ | Alertmanager | | - 告警路由与通知 | +-----------+------------+ | ↓ +------------------------+ | Grafana | | - 可视化与仪表盘展示 | +------------------------+


六、监控 Node Exporter

Node Exporter 用于采集主机系统信息。

1. 安装

wget https://github.com/prometheus/node_exporter/releases/download/v1.8.0/node_exporter-1.8.0.linux-amd64.tar.gz tar -zxvf node_exporter-1.8.0.linux-amd64.tar.gz cd node_exporter-1.8.0.linux-amd64 ./node_exporter &

默认监听端口:9100

2. 添加到 Prometheus

scrape_configs: - job_name: "node" static_configs: - targets: ["localhost:9100"]

访问:


http://localhost:9090/targets


七、PromQL 查询语言

PromQL(Prometheus Query Language)是用于查询监控数据的强大语言。

基本语法

http_requests_total

查询总请求次数。

按标签过滤

http_requests_total{method="GET", status="200"}

聚合函数

sum(rate(http_requests_total[5m])) by (method)

表示:过去 5 分钟内按请求方法统计平均请求速率。

常用函数
函数含义
rate()计算时间序列增长速率
sum()求和
avg()平均值
max()最大值
min()最小值
histogram_quantile()计算直方图百分位
increase()区间增量

八、监控 Web 服务(Exporter 示例)

1. 使用 HTTP 接口暴露指标

# app.py from flask import Flask, Response from prometheus_client import Counter, generate_latest app = Flask(__name__) REQUEST_COUNT = Counter("app_requests_total", "Total app requests") @app.route("/") def index(): REQUEST_COUNT.inc() return "Hello, Prometheus!" @app.route("/metrics") def metrics(): return Response(generate_latest(), mimetype="text/plain") app.run(port=5000)

运行后访问:


http://localhost:5000/metrics

2. 添加到 Prometheus

scrape_configs: - job_name: "myapp" static_configs: - targets: ["localhost:5000"]


九、Grafana 可视化

1. 安装

sudo apt install grafana -y sudo systemctl start grafana-server

访问:


http://localhost:3000

2. 添加 Prometheus 数据源

在 Grafana 界面 → “Connections → Data Sources” → 选择 Prometheus → 填入:


http://localhost:9090

3. 导入 Dashboard

在 “Dashboards → Import” 处输入 ID(如 1860,Node Exporter 官方模板)即可。

参考案例:www.lapwl.cn


十、Alertmanager 告警系统

Alertmanager 用于处理 Prometheus 生成的告警。

1. 安装

wget https://github.com/prometheus/alertmanager/releases/download/v0.27.0/alertmanager-0.27.0.linux-amd64.tar.gz tar -zxvf alertmanager-0.27.0.linux-amd64.tar.gz cd alertmanager-0.27.0.linux-amd64 ./alertmanager --config.file=alertmanager.yml

2. 配置文件

alertmanager.yml


route: receiver: "email-alert" receivers: - name: "email-alert" email_configs: - to: "admin@example.com" from: "prometheus@example.com" smarthost: "smtp.example.com:587" auth_username: "prometheus@example.com" auth_password: "yourpassword"


十一、Prometheus 告警规则


groups: - name: node_alerts rules: - alert: HighCPUUsage expr: avg(rate(node_cpu_seconds_total{mode="system"}[5m])) > 0.8 for: 2m labels: severity: warning annotations: summary: "CPU 使用率过高" description: "当前系统 CPU 使用率超过 80%"

加载规则:


./prometheus --config.file=prometheus.yml --rule.file=alert.rules.yml


十二、Kubernetes 监控

1. 启用自动发现

scrape_configs: - job_name: "kubernetes-pods" kubernetes_sd_configs: - role: pod

Prometheus 会自动发现集群中的 Pod 并采集指标。

2. 常用 Exporters
Exporter作用
kube-state-metrics采集 Kubernetes 资源状态
cAdvisor采集容器性能数据
kubelet metrics采集节点运行指标

十三、黑盒监控(Blackbox Exporter)

Blackbox 用于监控外部 HTTP/TCP/ICMP 可用性。


modules: http_2xx: prober: http timeout: 5s http: valid_status_codes: [200,302]

Prometheus 配置:


scrape_configs: - job_name: "blackbox" metrics_path: /probe params: module: [http_2xx] static_configs: - targets: - https://example.com


十四、性能优化

  1. 调整采集间隔与保留周期:

    
    

    scrape_interval: 30s retention_time: 15d

  2. 使用远程存储(Thanos / VictoriaMetrics);

  3. 启用压缩存储;

  4. 分区部署 Exporter;

  5. 限制高频抓取目标。


十五、常见命令

命令说明
promtool check config prometheus.yml校验配置
promtool test rules rules.yml测试告警规则
promtool query range查询时间区间数据
prometheus --version查看版本

十六、企业级架构示例


+---------------------------------------------+ | Grafana 可视化层 | +---------------------------------------------+ | Alertmanager 告警路由层 | +---------------------------------------------+ | Prometheus 主从采集与聚合层 | +---------------------------------------------+ | Node Exporter | cAdvisor | App Exporter 等 | +---------------------------------------------+

Prometheus 主节点汇总各子节点数据,并统一输出到 Grafana。


十七、告警集成示例

Slack 通知

receivers: - name: "slack" slack_configs: - api_url: "https://hooks.slack.com/services/XXXX" channel: "#alerts" text: "⚠️ 告警:{{ .CommonAnnotations.summary }}"

Webhook 集成

receivers: - name: "webhook" webhook_configs: - url: "http://ops-system.local/alert"


十八、日志与监控结合

结合 ELK(Elasticsearch + Logstash + Kibana):

  • Prometheus → 性能指标监控;

  • ELK → 日志与事件分析;

  • Grafana → 统一可视化大屏。

实现 可观测性三要素:Metrics、Logs、Traces。


十九、Prometheus + Thanos 高可用架构

Thanos 可实现:

  • 跨数据中心聚合;

  • 长期存储;

  • 全局查询。

部署方式:

  • Prometheus + Thanos Sidecar;

  • Thanos Query + Store + Compactor 模块。


二十、企业实战应用场景

场景实践方式
Kubernetes 集群监控节点、Pod、容器全覆盖采集
Web 服务可用性监控HTTP 探针 + 延迟指标
业务性能监控自定义 Exporter
数据库监控PostgreSQL/MySQL Exporter
CI/CD 流程监控监测构建与发布任务状态
自动化告警中心与 Slack / 邮件 / Webhook 联动

更多推荐