第一章:Prometheus 简介
全面介绍 Prometheus 监控系统的核心概念、架构设计、优势特点,以及与其他监控系统的对比分析
最后更新: 2024-01-01
页面目录
第一章:Prometheus 简介
1.1 什么是 Prometheus
Prometheus 是一个开源的监控系统和时序数据库,由 SoundCloud 开发,现已成为 Cloud Native Computing Foundation (CNCF) 的毕业项目。它专为云原生环境设计,能够高效地采集、存储、查询监控指标数据。
核心特性
| 特性 | 说明 |
|---|---|
| 多维度数据模型 | 通过指标名称和标签键值对实现灵活的数据组织 |
| 强大的 PromQL | 支持高效的时间序列数据查询和聚合 |
| 不依赖分布式存储 | 单个服务器节点即可工作,适合云原生架构 |
| pull 模式采集 | 基于 HTTP 的拉取模式,便于服务发现和监控 |
| 多种图形和仪表盘 | 支持集成 Grafana 等可视化工具 |
1.2 Prometheus 架构
┌─────────────────────────────────────────────────────┐
│ Prometheus Server │
│ ┌─────────────┐ ┌─────────────┐ ┌──────────┐ │
│ │ Service │ │ TSDB │ │ HTTP │ │
│ │ Discovery │───▶│ Storage │◀───│ Server │ │
│ └─────────────┘ └─────────────┘ └──────────┘ │
│ ▲ │ │
│ │ │ │
│ ┌──────┴───────┐ │ │
│ │ Retrieval │ │ │
│ │ (抓取模块) │───────────┘ │
│ └──────────────┘ │
└─────────────────────────────────────────────────────┘
│
┌───────────────────────┼───────────────────────┐
▼ ▼ ▼
┌───────────────┐ ┌───────────────┐ ┌───────────────┐
│ Exporters │ │ Pushgateway │ │ Alertmanager │
│ (指标暴露) │ │ (短任务推送) │ │ (告警处理) │
└───────────────┘ └───────────────┘ └───────────────┘
▲ ▲ ▲
│ │ │
┌──────┴──────┐ ┌──────┴──────┐ ┌──────┴──────┐
│ Applications│ │ Batch Jobs │ │ Email │
│ (应用) │ │ (定时任务) │ │ Slack │
└─────────────┘ └──────────────┘ └─────────────┘
架构组件详解
| 组件 | 功能描述 |
|---|---|
| Prometheus Server | 核心服务,负责采集、存储、查询指标数据 |
| Retrieval | 抓取模块,主动从 targets 拉取指标 |
| TSDB | 时序数据库,存储时间序列数据 |
| HTTP Server | 提供 API 和 Web UI |
| Service Discovery | 自动发现监控目标 |
| Alertmanager | 处理和路由告警通知 |
1.3 核心概念
1.3.1 时间序列(Time Series)
Prometheus 以时间序列的形式存储数据,每条记录包含:
- 指标名称 (Metric Name): 标识测量的指标,如
http_requests_total - 标签 (Labels): 键值对,用于区分同一指标的不同维度,如
method="GET", status="200" - 样本 (Sample): 具体的时间点数据,包含 timestamp 和 value
# 格式示例
http_requests_total{method="GET", status="200", handler="/api/users"} 10293 1704067200000
1.3.2 指标类型 (Metric Types)
| 类型 | 说明 | 使用场景 |
|---|---|---|
| Counter | 计数器,只增不减 | 请求总数、错误总数 |
| Gauge | 仪表盘,可增可减 | CPU 使用率、内存占用 |
| Histogram | 直方图,统计分布 | 请求延迟分布 |
| Summary | 摘要,统计分位数 | 请求延迟百分比 |
1.3.3 Job 和 Instance
# Job 配置示例
job_name: api-server
static_configs:
- targets:
- 'server1:9090'
- 'server2:9090'
- Job: 一组具有相同角色的 targets,如
api-server - Instance: 具体的监控目标,如
server1:9090
自动添加的标签:
| 标签 | 说明 |
|---|---|
job |
Job 名称 |
instance |
Instance 地址 |
1.4 Prometheus 优势
1.4.1 与传统监控对比
| 特性 | Prometheus | Zabbix | Nagios |
|---|---|---|---|
| 数据模型 | 多维度 | 平面化 | 平面化 |
| 查询语言 | PromQL | LLD | 命令行 |
| 部署难度 | 简单 | 中等 | 复杂 |
| 云原生支持 | ⭐⭐⭐⭐⭐ | ⭐⭐ | ⭐ |
| 社区生态 | 丰富 | 一般 | 丰富 |
| 规模 | 百万级指标 | 万级主机 | 千级主机 |
1.4.2 适用场景
✅ 强烈推荐使用 Prometheus 的场景:
- Kubernetes 容器编排环境
- 微服务架构应用
- 需要快速问题定位的开发运维场景
- 需要自定义指标和告警的团队
❌ 不推荐使用 Prometheus 的场景:
- 需要监控网络设备和硬件(使用 SNMP Exporter 补充)
- 传统单体应用监控
- 需要分布式追踪(配合 Jaeger/Prometheus 使用)
1.5 生态系统
┌─────────────────────────────────────────────────────────────────┐
│ Prometheus 生态系统 │
├─────────────────────────────────────────────────────────────────┤
│ │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ Grafana │◀──▶│ Prometheus │───▶│ Alertmanager │ │
│ │ (可视化) │ │ (核心) │ │ (告警) │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
│ │ │ │ │
│ │ │ │ │
│ ┌──────┴──────┐ ┌───────┴───────┐ ┌──────┴──────┐ │
│ │ Dashboard │ │ Exporters │ │ Notifier │ │
│ │ 模板库 │ │ (100+) │ │ Email/Slack│ │
│ └─────────────┘ └───────────────┘ └─────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────┘
常用 Exporters
| Exporter | 用途 | 端口 |
|---|---|---|
| node_exporter | 主机监控 | 9100 |
| cadvisor | 容器监控 | 8080 |
| mysqld_exporter | MySQL 监控 | 9104 |
| postgres_exporter | PostgreSQL 监控 | 9187 |
| redis_exporter | Redis 监控 | 9121 |
| nginx-prometheus-exporter | Nginx 监控 | 9113 |
1.6 快速开始
安装 Prometheus
# 使用 Docker 运行
docker run -d \
--name prometheus \
-p 9090:9090 \
-v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml \
prom/prometheus:latest
# 或者下载二进制文件
wget https://github.com/prometheus/prometheus/releases/download/v2.47.0/prometheus-2.47.0.linux-amd64.tar.gz
tar xzf prometheus-2.47.0.linux-amd64.tar.gz
cd prometheus-2.47.0.linux-amd64
./prometheus --config.file=prometheus.yml
访问 Web UI
打开浏览器访问 http://localhost:9090,即可使用:
- Graph: 执行 PromQL 查询
- Status > Targets: 查看监控目标
- Alerts: 查看告警规则状态
1.7 本章小结
本章介绍了 Prometheus 监控系统的核心概念和架构设计:
- Prometheus 是 CNCF 毕业项目,专为云原生环境设计
- 多维度数据模型 通过指标名称和标签实现灵活的数据组织
- 四种指标类型:Counter、Gauge、Histogram、Summary
- Pull 模式 使监控更加简单和可靠
- 强大的生态系统 包括 Grafana、Alertmanager 和丰富的 Exporters
📖 下一步