13 - 联邦集群
13 - 联邦集群
13.1 概述
联邦(Federation)允许一个 Prometheus Server 从另一个 Prometheus Server 抓取数据,实现跨集群、分层聚合的监控架构。
使用场景
| 场景 | 说明 |
|---|---|
| 跨集群监控 | 多个 K8s 集群的统一监控 |
| 分层聚合 | 从叶子节点聚合到全局视图 |
| 数据中心监控 | 多机房/多区域数据汇总 |
| 权限隔离 | 不同团队/环境各自独立 Prometheus |
13.2 联邦架构
基本架构
┌──────────────────────────────────────────────────────┐
│ Global Prometheus │
│ (联邦聚合层 - 从子 Prometheus 拉取聚合数据) │
│ │
│ /federate 端点 ◄── 子 Prometheus 的查询结果 │
└──────────────┬───────────────────────────────────────┘
│ /federate 抓取
┌──────┴──────┐
▼ ▼
┌──────────────┐ ┌──────────────┐
│ Prometheus │ │ Prometheus │
│ Cluster A │ │ Cluster B │
│ (DC 北京) │ │ (DC 上海) │
│ │ │ │
│ Node/MySQL/ │ │ Node/Redis/ │
│ App metrics │ │ App metrics │
└──────────────┘ └──────────────┘
三层联邦架构
┌─────────────────────────────────────────────────────────┐
│ 全局聚合层 (Global) │
│ 聚合所有区域的关键指标 (SLO, 全局容量规划) │
└─────────────────────────┬───────────────────────────────┘
│ /federate
┌─────────────────┼─────────────────┐
▼ ▼ ▼
┌──────────────┐ ┌──────────────┐ ┌──────────────┐
│ 区域聚合层 │ │ 区域聚合层 │ │ 区域聚合层 │
│ (北京) │ │ (上海) │ │ (广州) │
│ 聚合区域级别 │ │ │ │ │
│ 关键指标 │ │ │ │ │
└──────┬───────┘ └──────────────┘ └──────────────┘
│ /federate
┌───┴────┐
▼ ▼
┌──────┐ ┌──────┐
│ K8s │ │ K8s │ 叶子节点:采集原始数据
│ 集群1 │ │ 集群2 │
└──────┘ └──────┘
13.3 配置联邦
子 Prometheus 配置
子 Prometheus 不需要特殊配置,只需要暴露 /federate 端点(默认启用)。
全局 Prometheus 配置
# global-prometheus.yml
global:
scrape_interval: 30s
external_labels:
region: 'global'
scrape_configs:
# 联邦抓取 - 从子 Prometheus 获取聚合指标
- job_name: 'federate-cluster-a'
honor_labels: true
metrics_path: '/federate'
params:
'match[]':
- '{job="prometheus"}' # 基础指标
- 'job:http_requests:rate5m' # 录制规则结果
- 'job:http_duration:p99_5m' # 录制规则结果
- 'instance:node_cpu_usage:ratio' # 录制规则结果
- 'up' # 健康状态
static_configs:
- targets:
- 'prometheus-a:9090'
labels:
cluster: 'cluster-a'
- job_name: 'federate-cluster-b'
honor_labels: true
metrics_path: '/federate'
params:
'match[]':
- '{job="prometheus"}'
- 'job:http_requests:rate5m'
- 'job:http_duration:p99_5m'
- 'instance:node_cpu_usage:ratio'
- 'up'
static_configs:
- targets:
- 'prometheus-b:9090'
labels:
cluster: 'cluster-b'
match[] 参数详解
params:
'match[]':
# 精确匹配
- 'up'
# 选择器匹配
- '{job="api"}'
# 使用函数匹配(只匹配聚合结果)
- 'job:http_requests:rate5m'
# 正则匹配
- '{__name__=~"job:.*"}'
# 复杂匹配
- '{__name__=~"instance:node_.*:ratio",job="node"}'
13.4 联邦查询
/federate 端点
# 查询所有 up 指标
curl 'http://prometheus:9090/federate?match[]=up'
# 查询多个指标
curl 'http://prometheus:9090/federate?match[]={job="api"}&match[]=up'
# 查询录制规则结果
curl 'http://prometheus:9090/federate?match[]={__name__=~"job:.*"}'
联邦 vs Remote Read
| 特性 | 联邦(Federation) | Remote Read |
|---|---|---|
| 数据来源 | /federate API | /api/v1/read |
| 数据格式 | Text/OpenMetrics | Protocol Buffers |
| 数据过滤 | match[] 参数 | PromQL 查询 |
| 适用场景 | 聚合指标 | 完整历史数据 |
| 性能 | 中等 | 高 |
13.5 最佳实践
只聚合必要的指标
# ❌ 抓取所有指标(数据量巨大)
params:
'match[]':
- '{__name__=~".+"}'
# ✅ 只抓取聚合指标和关键指标
params:
'match[]':
- '{__name__=~"job:.*"}'
- 'up'
- 'prometheus_tsdb_head_series'
使用录制规则预聚合
# 子 Prometheus 上的录制规则
groups:
- name: federation_recording
interval: 30s
rules:
# 聚合到 job 级别
- record: job:http_requests:rate5m
expr: sum by(job) (rate(http_requests_total[5m]))
- record: job:http_duration:p99_5m
expr: |
histogram_quantile(0.99,
sum by(job, le) (rate(http_request_duration_seconds_bucket[5m]))
)
- record: cluster:node_cpu_usage:avg
expr: avg by(job) (instance:node_cpu_usage:ratio)
外部标签
# 子 Prometheus A
global:
external_labels:
cluster: 'cluster-a'
region: 'beijing'
environment: 'production'
# 子 Prometheus B
global:
external_labels:
cluster: 'cluster-b'
region: 'shanghai'
environment: 'production'
13.6 多集群告警
# 全局 Prometheus 上的告警规则
groups:
- name: global_alerts
rules:
# 实例宕机(带集群信息)
- alert: InstanceDown
expr: up == 0
for: 1m
labels:
severity: critical
annotations:
summary: "集群 {{ $labels.cluster }} 实例 {{ $labels.instance }} 宕机"
# 跨集群错误率对比
- alert: ClusterErrorRateDeviation
expr: |
abs(
job:http_error_ratio:ratio - scalar(avg(job:http_error_ratio:ratio))
) > 0.1
for: 15m
labels:
severity: warning
annotations:
summary: "集群 {{ $labels.cluster }} 错误率偏离全局平均值"
13.7 本章小结
| 要点 | 说明 |
|---|---|
| 用途 | 跨集群、分层聚合 |
| 端点 | /federate |
| 最佳实践 | 只聚合必要的指标(使用录制规则) |
| 外部标签 | 区分不同集群/区域 |
| 层级 | 叶子 → 区域聚合 → 全局聚合 |
扩展阅读
上一章:12 - Pushgateway 下一章:14 - Thanos