使用Chaos Mesh模拟Prometheus节点宕机并验证联邦高可用的实验YAML
解读
国内云原生面试中,高可用是“必考题”。Prometheus 联邦(Federation)方案因落地简单、对存量改动小,被大量金融、运营商、电商公司采用。面试官抛出“Chaos Mesh + 联邦高可用”组合,既考察你对 Chaos 工程工具的掌握,也验证你是否真正理解联邦架构的“采集链路冗余、数据断点自愈、告警零丢失”三大核心指标。答题时必须给出可直接 kubectl apply 的 YAML,并说明如何观测实验结果,否则会被认为“纸上谈兵”。
知识点
- 联邦层级:常规两级——Global Prometheus(聚合) + 边缘 Region Prometheus(分片采集)。
- 高可用关键点:
– 边缘至少双实例(AZ 级反亲和),Global 至少双实例 + 层间 LB。
– 采集目标通过 Kubernetes EndpointSlice 实现自动 failover,无需人工改配置。
– 告警路径:边缘实例本地 Alertmanager 集群 → 全局 Alertmanager 集群,防止“断链即静默”。 - Chaos Mesh 1.2+ 核心 CRD:PodChaos(杀 Pod)、NetworkChaos(断网)、StressChaos(打满 CPU/内存),国内镜像源可用
registry.cn-hangzhou.aliyuncs.com/chaos-mesh加速。 - 观测指标:
– up{job="prometheus"} 瞬时值为 0 表示采集端宕机;
– prometheus federate 接口 5xx 比例;
– alertmanager_notifications_failed_total 是否堆积;
– 实验前后数据断档通过count_over_time函数对比验证。
答案
以下实验基于“边缘双副本 + Global 双副本”联邦架构,默认已安装 Chaos Mesh(helm 安装即可)。实验分三步:①制造宕机;②验证采集链路自愈;③验证告警不丢。
- 边缘 Prometheus 双副本 Deployment(精简版,生产需加 PVC、Thanos Sidecar 等)
apiVersion: apps/v1
kind: Deployment
metadata:
name: prometheus-edge
namespace: monitoring
spec:
replicas: 2
selector:
matchLabels: {app: prometheus-edge}
template:
metadata:
labels: {app: prometheus-edge}
spec:
affinity: # 反亲和,必须跨节点
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- topologyKey: kubernetes.io/hostname
labelSelector:
matchLabels: {app: prometheus-edge}
containers:
- name: prometheus
image: prom/prometheus:v2.45.0
args:
- --config.file=/etc/prometheus/prometheus.yml
- --storage.tsdb.path=/prometheus
- --web.enable-lifecycle
- --web.enable-admin-api
ports:
- containerPort: 9090
volumeMounts:
- name: config
mountPath: /etc/prometheus
- name: data
mountPath: /prometheus
volumes:
- name: config
configMap:
name: prometheus-edge-cm
- name: data
emptyDir: {} # 演示用,生产用 PVC
- 边缘配置 ConfigMap(prometheus-edge-cm)
apiVersion: v1
kind: ConfigMap
metadata:
name: prometheus-edge-cm
namespace: monitoring
data:
prometheus.yml: |
global:
scrape_interval: 15s
external_labels: {cluster: 'edge', replica: '$(HOSTNAME)'}
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
# 暴露 federate 接口给 Global 采集
- job_name: 'federate'
honor_labels: true
metrics_path: /federate
params:
match[]:
- '{__name__=~"job:.*"}'
- '{__name__=~"up"}'
static_configs:
- targets: ['prometheus-edge.monitoring.svc:9090']
-
Global Prometheus 双副本 Deployment(略,与边缘类似,scrape 配置指向边缘的 ClusterIP 即可)。
-
Chaos Mesh 实验 YAML:随机杀掉一个边缘 Prometheus Pod,持续 60 秒,观察联邦链路是否断档。
apiVersion: chaos-mesh.org/v1alpha1
kind: PodChaos
metadata:
name: kill-prometheus-edge
namespace: monitoring
spec:
action: pod-kill
mode: one
duration: '60s'
selector:
namespaces: ['monitoring']
labelSelectors:
app: prometheus-edge
scheduler:
cron: '@every 5m' # 每 5 分钟做一次,面试演示可改 once
- 验证脚本(可直接在 Global Prometheus 执行)
# 1. 实验前记录数据点总量
before=$(curl -s 'http://global-prometheus:9090/api/v1/query?query=count(up)' | jq -r '.data.result[0].value[1]')
# 2. 等待 Chaos 注入完成
sleep 70
# 3. 实验后再次查询
after=$(curl -s 'http://global-prometheus:9090/api/v1/query?query=count(up)' | jq -r '.data.result[1].value[1]')
# 4. 对比差值,理论上应相等(双副本冗余)
echo "before=$before, after=$after"
- 告警验证 在边缘 Prometheus 预置一条永远触发的告警:
groups:
- name: test
rules:
- alert: AlwaysFiring
expr: vector(1)
labels: {severity: critical}
annotations: {summary: 'chaos test'}
实验过程中通过 Alertmanager 的 /api/v1/alerts 接口持续拉取,确认 60 秒内告警未出现“gap”。
拓展思考
- 如果边缘节点挂掉 50% 以上,联邦仍可用吗?
答:需引入“多集群联邦 + Thanos Receive 哈希环”方案,把边缘数据先写入对象存储,Global 端通过 StoreAPI 聚合,实现跨 Region 容灾。 - 国内金融合规要求“数据不可丢”,如何改造?
答:边缘 Prometheus 增加 WAL 远程写入到 Kafka(使用 prometheus-remote-write-kafka 插件),再由 Kafka→InfluxDB/TDengine 做异构备份,满足 7×24 审计。 - Chaos Mesh 能否模拟“磁盘只读”故障?
答:用 IOChaos 将 prometheus data 目录置为 fault-type: attrOverride,权限改为 444,可验证 Prometheus 在磁盘异常时的 WAL 自愈与告警延迟。