Prometheus 配置
学习目标
- 理解 Prometheus 的架构和工作原理
- 掌握使用 Helm 安装 Prometheus
- 学会配置 ServiceMonitor 和告警规则
核心概念
Prometheus 架构
┌─────────────────────────────────────────────────────────┐
│ Prometheus 架构 │
│ │
│ ┌──────────────┐ ┌──────────────┐ │
│ │ 应用/Pod │ │ Node │ │
│ │ /metrics │ │ Exporter │ │
│ └──────┬───────┘ └──────┬───────┘ │
│ │ │ │
│ └────────┬───────────┘ │
│ ▼ │
│ ┌──────────────┐ │
│ │ Prometheus │ ← Pull 模式拉取指标 │
│ │ Server │ │
│ └──────┬───────┘ │
│ │ │
│ ┌──────┴───────┐ │
│ ▼ ▼ │
│ ┌──────────────┐ ┌──────────────┐ │
│ │ TSDB 存储 │ │ Alertmanager│ │
│ │ 时序数据 │ │ 告警管理 │ │
│ └──────────────┘ └──────────────┘ │
│ │
│ ┌──────────────┐ │
│ │ Grafana │ ← 可视化 │
│ └──────────────┘ │
└─────────────────────────────────────────────────────────┘
核心组件
| 组件 | 说明 |
|---|---|
| Prometheus Server | 拉取和存储指标数据 |
| Exporters | 暴露指标的采集器 |
| Pushgateway | 短生命周期任务推送指标 |
| Alertmanager | 处理和发送告警 |
| Grafana | 可视化仪表盘 |
PromQL 查询语言
# 基础查询
up # 目标是否在线
http_requests_total # HTTP 请求总数
container_memory_usage_bytes # 容器内存使用
# 常用函数
rate(http_requests_total[5m]) # 5 分钟内请求速率
increase(http_requests_total[1h]) # 1 小时内增量
sum(rate(http_requests_total[5m])) # 总请求速率
# 聚合
sum by (pod) (rate(http_requests_total[5m])) # 按 Pod 聚合
avg by (namespace) (container_memory_usage_bytes) # 按 Namespace 平均
实践练习
练习 1:使用 Helm 安装 Prometheus
# 添加 Helm 仓库
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
# 安装 Prometheus Stack(包含 Prometheus + Grafana + Alertmanager)
helm install prometheus prometheus-community/kube-prometheus-stack \
--namespace monitoring \
--create-namespace
# 查看安装状态
kubectl get pods -n monitoring
练习 2:访问 Prometheus UI
# 端口转发
kubectl port-forward svc/prometheus-kube-prometheus-prometheus 9090:9090 -n monitoring
# 访问 http://localhost:9090
练习 3:创建 ServiceMonitor
# service-monitor.yaml
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: my-app-monitor
namespace: monitoring
labels:
release: prometheus
spec:
namespaceSelector:
matchNames:
- default
selector:
matchLabels:
app: my-app
endpoints:
- port: http
path: /metrics
interval: 15s
练习 4:配置告警规则
# alert-rules.yaml
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
name: my-app-alerts
namespace: monitoring
labels:
release: prometheus
spec:
groups:
- name: my-app
rules:
- alert: HighErrorRate
expr: |
sum(rate(http_requests_total{code=~"5.."}[5m]))
/
sum(rate(http_requests_total[5m]))
> 0.05
for: 5m
labels:
severity: critical
annotations:
summary: "高错误率告警"
description: "错误率超过 5% 已持续 5 分钟"
练习 5:自定义应用暴露指标
# Python 示例 - 使用 prometheus_client
from prometheus_client import Counter, Histogram, generate_latest
from flask import Flask, Response
app = Flask(__name__)
# 定义指标
REQUEST_COUNT = Counter(
'http_requests_total',
'Total HTTP requests',
['method', 'endpoint', 'status']
)
REQUEST_LATENCY = Histogram(
'http_request_duration_seconds',
'HTTP request latency',
['method', 'endpoint']
)
@app.route('/metrics')
def metrics():
return Response(generate_latest(), mimetype='text/plain')
@app.route('/')
def index():
REQUEST_COUNT.labels('GET', '/', '200').inc()
return 'Hello World'
常见问题
1. Prometheus 无法抓取目标
# 检查 ServiceMonitor 是否正确
kubectl get servicemonitor -n monitoring
# 检查目标状态
# Prometheus UI → Status → Targets
# 常见原因:
# - 标签不匹配
# - 端口配置错误
# - 网络不通
2. 存储空间不足
# 增加存储或配置数据保留
prometheus:
prometheusSpec:
retention: 30d
storageSpec:
volumeClaimTemplate:
spec:
storageClassName: standard
resources:
requests:
storage: 50Gi
小结
| 要点 | 说明 |
|---|---|
| Prometheus | 云原生监控标准 |
| 工作模式 | Pull 模式拉取指标 |
| 查询语言 | PromQL |
| 告警 | Alertmanager |
| 可视化 | Grafana |
| 常用组件 | kube-prometheus-stack |
练习编辑器
bash
Loading...