第一章:Prometheus 简介¶
什么是 Prometheus?¶
Prometheus 是由 SoundCloud 开发的开源监控系统和时序数据库,于 2016 年加入 CNCF,成为继 Kubernetes 之后的第二个毕业项目。
核心特性¶
- 多维数据模型 - 指标名称和键值对标识的时间序列数据
- PromQL 查询语言 - 灵活强大的查询语言
- Pull 模式采集 - 主动拉取指标数据
- 服务发现 - 自动发现监控目标
- 告警支持 - 内置 Alertmanager 告警组件
Prometheus 架构¶
┌─────────────────────────────────────────────────────────────────────┐
│ Prometheus 生态 │
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Pushgateway │ │ Exporters │ │ Targets │ │
│ │ (推送网关) │ │ (数据导出器) │ │ (监控目标) │ │
│ └──────┬──────┘ └──────┬──────┘ └──────┬──────┘ │
│ │ │ │ │
│ └──────────────────┼──────────────────┘ │
│ │ Pull │
│ ▼ │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ Prometheus Server │ │
│ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ │
│ │ │ Retrieval │ │ TSDB │ │ HTTP Server │ │ │
│ │ │ (数据采集) │ │ (时序存储) │ │ (查询接口) │ │ │
│ │ └─────────────┘ └─────────────┘ └─────────────┘ │ │
│ └─────────────────────────────────────────────────────────────┘ │
│ │ │
│ ┌──────────────────┼──────────────────┐ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ PromQL │ │ Alertmanager│ │ Grafana │ │
│ │ (查询) │ │ (告警) │ │ (可视化) │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
└─────────────────────────────────────────────────────────────────────┘
核心组件¶
1. Prometheus Server¶
核心服务,负责: - 采集时间序列数据 - 存储数据到本地 TSDB - 执行 PromQL 查询 - 检测告警规则
2. Exporter¶
数据导出器,将应用指标转换为 Prometheus 格式:
# 常用 Exporter
node_exporter # 主机监控
mysqld_exporter # MySQL 监控
redis_exporter # Redis 监控
nginx_exporter # Nginx 监控
blackbox_exporter # 黑盒监控
3. Pushgateway¶
推送网关,用于: - 短生命周期任务推送指标 - 批量任务指标收集 - 不支持 Pull 模式的场景
4. Alertmanager¶
告警管理器,负责: - 去重和分组 - 路由和通知 - 静默和抑制
数据模型¶
指标格式¶
示例¶
# 计数器
http_requests_total{method="GET", endpoint="/api/users"} 1234
# 仪表盘
node_memory_usage_bytes{host="web-01"} 8589934592
# 直方图
http_request_duration_seconds_bucket{le="0.1"} 100
http_request_duration_seconds_bucket{le="0.5"} 200
http_request_duration_seconds_bucket{le="+Inf"} 300
# 摘要
http_request_duration_seconds_sum 150.5
http_request_duration_seconds_count 300
指标类型¶
1. Counter(计数器)¶
只增不减的累积值:
2. Gauge(仪表盘)¶
可增可减的瞬时值:
3. Histogram(直方图)¶
观测值的分布统计:
# 请求延迟分布
http_request_duration_seconds_bucket{le="0.1"} 100
http_request_duration_seconds_bucket{le="0.5"} 200
http_request_duration_seconds_bucket{le="1.0"} 280
http_request_duration_seconds_bucket{le="+Inf"} 300
http_request_duration_seconds_sum 150.5
http_request_duration_seconds_count 300
4. Summary(摘要)¶
类似 Histogram,但计算分位数:
# 请求延迟分位数
http_request_duration_seconds{quantile="0.5"} 0.12
http_request_duration_seconds{quantile="0.9"} 0.35
http_request_duration_seconds{quantile="0.99"} 0.85
Pull vs Push¶
Pull 模式(Prometheus)¶
优点: - 统一采集控制 - 易于发现问题 - 目标健康检查
Push 模式(其他系统)¶
服务发现¶
静态配置¶
文件服务发现¶
scrape_configs:
- job_name: 'nodes'
file_sd_configs:
- files:
- /etc/prometheus/targets/*.json
refresh_interval: 5m
Kubernetes 服务发现¶
scrape_configs:
- job_name: 'kubernetes-pods'
kubernetes_sd_configs:
- role: pod
relabel_configs:
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
action: keep
regex: true
安装方式¶
Docker¶
docker run -d \
--name prometheus \
-p 9090:9090 \
-v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml \
prom/prometheus
Docker Compose¶
version: '3.8'
services:
prometheus:
image: prom/prometheus:latest
ports:
- "9090:9090"
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
- prometheus-data:/prometheus
command:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.path=/prometheus'
node-exporter:
image: prom/node-exporter:latest
ports:
- "9100:9100"
volumes:
- /proc:/host/proc:ro
- /sys:/host/sys:ro
- /:/rootfs:ro
command:
- '--path.procfs=/host/proc'
- '--path.sysfs=/host/sys'
volumes:
prometheus-data:
Kubernetes¶
apiVersion: apps/v1
kind: Deployment
metadata:
name: prometheus
spec:
replicas: 1
selector:
matchLabels:
app: prometheus
template:
metadata:
labels:
app: prometheus
spec:
containers:
- name: prometheus
image: prom/prometheus:latest
ports:
- containerPort: 9090
volumeMounts:
- name: config
mountPath: /etc/prometheus
volumes:
- name: config
configMap:
name: prometheus-config
快速开始¶
# 1. 创建配置文件
cat > prometheus.yml << 'EOF'
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
EOF
# 2. 启动 Prometheus
docker run -d \
--name prometheus \
-p 9090:9090 \
-v $(pwd)/prometheus.yml:/etc/prometheus/prometheus.yml \
prom/prometheus
# 3. 访问 Web UI
open http://localhost:9090
# 4. 查询指标
# 在 Web UI 中输入: up
Prometheus vs 其他监控¶
| 特性 | Prometheus | Zabbix | Nagios | Datadog |
|---|---|---|---|---|
| 数据模型 | 多维 | 扁平 | 扁平 | 多维 |
| 查询语言 | PromQL | 无 | 无 | 专有 |
| 采集模式 | Pull | Push/Pull | Push | Agent |
| 存储 | TSDB | RDBMS | 文件 | 云存储 |
| 开源 | 是 | 是 | 是 | 否 |
| 云原生 | 是 | 部分 | 否 | 是 |
小结¶
本章学习了:
- ✅ Prometheus 概念和特性
- ✅ Prometheus 架构和组件
- ✅ 数据模型和指标类型
- ✅ Pull 模式和服务发现
- ✅ 安装和快速开始
下一章¶
第二章:Prometheus 安装与配置 - 学习详细配置和部署。