最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Prometheus 如何监控分布式数据库集群状态
时间:2026-08-17 20:38:49 编辑:袖梨 来源:一聚教程网
Prometheus 监控分布式数据库集群的核心是通过标准化 Exporter 统一采集指标、多维标签打标、PromQL 聚合分析,并结合拓扑关系判断整体健康度;支持 MongoDB、CockroachDB、Redis Cluster 等多种数据库,无需依赖其主从或分片架构。
Prometheus 监控分布式数据库集群状态,核心在于把集群各节点的运行指标统一采集、打标、聚合,并结合拓扑关系识别整体健康度。它不依赖数据库自身是否“主从”或“分片”,而是通过标准化 Exporter + 多维标签 + PromQL 聚合实现跨节点可观测。
关键步骤:Exporter 部署与指标暴露
每个数据库节点需运行对应 Exporter,将内部状态转化为 HTTP 接口暴露的 Prometheus 格式指标:
-
MongoDB:用
mongodb_exporter,自动发现副本集成员,暴露mongodb_mongod_replset_my_state(当前节点角色)、mongodb_mongod_replset_oplog_window(oplog 时间窗口)、mongodb_mongod_connections_current等 -
CockroachDB:原生支持
/health和/metrics端点,无需额外 Exporter;关键指标如crdb_sql_exec_queries_total、crdb_store_capacity_used_percent、crdb_node_liveness_status可直接抓取 -
Redis Cluster:用
redis_exporter连接每个 Redis 实例,通过redis_instance_role标签区分 master/slave,再用redis_cluster_nodes_count和redis_cluster_state判断集群整体状态
服务发现与动态打标
在 Kubernetes 环境中,避免硬编码 IP,改用声明式方式自动关联实例:
- 为数据库 Pod 创建
Service(ClusterIP 或 Headless),确保每个 Pod 有稳定 DNS 名称 - 定义
ServiceMonitor(配合 Prometheus Operator)或PrometheusRule,按 label 匹配服务,例如:matchLabels: { app: "mongodb-replicaset" } - 在 Exporter 启动参数中注入集群元信息,如:
--web.listen-address=:9216 --mongodb.global-labels="cluster=prod,shard=shard0",让所有指标自带上下文标签
集群级健康判断(PromQL 实战)
单看某个节点指标意义有限,必须升维到集群视角:
- 判断 MongoDB 副本集是否完整:
count by (cluster) (mongodb_mongod_replset_my_state{my_state="PRIMARY"} == 1) == 1 and count by (cluster) (mongodb_mongod_replset_my_state != 0) >= 3——要求每个 cluster 恰好有 1 个 PRIMARY,且总存活节点 ≥ 3
- 检测 CockroachDB 节点失联:
count by (job) (crdb_node_liveness_status == 0) > 0结合告警持续时间(如
for: 2m),避免瞬时抖动误报 - Redis Cluster 是否在线:
min_over_time(redis_cluster_state[5m]) == 1只要过去 5 分钟内任意时刻返回非 1,即触发异常
可视化与根因定位
Grafana 面板不能只堆砌图表,要体现集群结构:
- 用变量(
$cluster、$shard)实现下钻:先看集群整体延迟热力图 → 点击高延迟 shard → 查看该 shard 内各节点 CPU/网络/慢命令分布 - 在面板标题中嵌入状态表达式,例如:
⚠️ {{ $values | printf "%.0f" }} nodes offline,让运维一眼识别风险规模
- 对关键指标设置阈值着色:如
crdb_store_range_underreplicated_count > 0时整行变红,提示数据冗余不足