当前位置: 首页 > news >正文

Prometheus 监控 Elasticsearch 全栈实战:从集群健康到慢查询的终极可观测性

Prometheus 监控 Elasticsearch 全栈实战:从集群健康到慢查询的终极可观测性


Elasticsearch 作为分布式搜索和分析引擎,其集群状态节点资源消耗索引吞吐量JVM 堆内存压力以及查询与索引延迟,直接决定了日志分析、全文检索等业务的体验。Prometheus 借助官方推荐的elasticsearch_exporter,将 Elasticsearch 的_cluster/stats_nodes/stats等 API 返回的 JSON 转化为标准化指标,让运维和开发团队能够以统一的可观测栈透视 ES 集群的内部运行状况。本文将带你从零搭建监控、理解关键指标、创建可视化大屏并配置精准告警。


1. 部署 elasticsearch_exporter

使用prometheus-community/elasticsearch_exporter(Go 语言编写,支持 ES 5.x/6.x/7.x/8.x,兼容 Basic Auth、Bearer Token 与 TLS)。

1.1 创建监控用户(Elasticsearch 8.x+ 默认启用安全)

在 Elasticsearch 中创建一个只读监控角色和用户:

# 使用 elasticsearch-create-enrollment-token 或手动通过 API# 下面以 Dev Tools 为例POST /_security/role/prometheus_monitor{"cluster":["monitor"],"indices":[{"names":["*"],"privileges":["monitor"]}]}POST /_security/user/prometheus{"password":"StrongPassword","roles":["prometheus_monitor"]}
1.2 启动 Exporter

Docker 部署(推荐):

dockerrun-d\--namees_exporter\-p9114:9114\-eES_URI="http://prometheus:StrongPassword@192.168.1.80:9200"\quay.io/prometheuscommunity/elasticsearch-exporter:latest

若 Elasticsearch 启用 HTTPS,则使用https://...,并可根据需要设置--es.ssl-skip-verify

二进制部署:

wgethttps://github.com/prometheus-community/elasticsearch_exporter/releases/download/v1.7.0/elasticsearch_exporter-1.7.0.linux-amd64.tar.gztarxzf elasticsearch_exporter-1.7.0.linux-amd64.tar.gzcdelasticsearch_exporter-1.7.0.linux-amd64exportES_URI="http://prometheus:StrongPassword@localhost:9200"./elasticsearch_exporter --web.listen-address=:9114

访问http://localhost:9114/metrics,看到以elasticsearch_开头的指标即成功。


2. 配置 Prometheus 抓取

scrape_configs:-job_name:'elasticsearch'scrape_interval:30sstatic_configs:-targets:['10.0.0.90:9114']labels:cluster:'es-prod'env:'production'

若监控多个集群,添加多个 target 并分别打标签。


3. 核心监控指标与 PromQL

elasticsearch_exporter暴露的指标极为丰富,重点关注以下类别:

分类关键指标含义PromQL 示例
集群健康elasticsearch_cluster_health_status{color="green"}1 = 健康(绿)直接判断,若green=0 表示黄/红
节点数量elasticsearch_cluster_health_number_of_nodes
elasticsearch_cluster_health_number_of_data_nodes
总节点数与数据节点数与预期值比较
活跃分片elasticsearch_cluster_health_active_shards当前活跃分片数结合unassigned_shards判断是否有未分配分片
未分配分片elasticsearch_cluster_health_unassigned_shards未分配分片数> 0 应立即排查
节点状态elasticsearch_nodes_up(exporter 内部计算)可达节点数结合elasticsearch_cluster_health_number_of_nodes
JVM 堆内存elasticsearch_jvm_memory_used_bytes{area="heap"}
elasticsearch_jvm_memory_max_bytes{area="heap"}
堆内存使用与上限elasticsearch_jvm_memory_used_bytes{area="heap"} / elasticsearch_jvm_memory_max_bytes{area="heap"}
JVM GCelasticsearch_jvm_gc_collection_seconds_count
elasticsearch_jvm_gc_collection_seconds_sum
GC 次数与累计时间rate(elasticsearch_jvm_gc_collection_seconds_sum[5m]) / rate(elasticsearch_jvm_gc_collection_seconds_count[5m])得平均 GC 耗时
索引速率elasticsearch_indices_indexing_index_total索引文档总数(计数器)rate(elasticsearch_indices_indexing_index_total[1m])得索引 TPS
查询速率elasticsearch_indices_search_query_total查询请求总数rate(elasticsearch_indices_search_query_total[1m])得 QPS
查询延迟elasticsearch_indices_search_query_time_seconds_total查询累计耗时平均查询延迟 =rate(elasticsearch_indices_search_query_time_seconds_total[5m]) / rate(elasticsearch_indices_search_query_total[5m])
索引延迟elasticsearch_indices_indexing_index_time_seconds_total索引累计耗时平均索引延迟同上计算
磁盘使用elasticsearch_filesystem_data_available_bytes
elasticsearch_filesystem_data_size_bytes
数据目录可用/总大小使用率:1 - elasticsearch_filesystem_data_available_bytes / elasticsearch_filesystem_data_size_bytes
段内存elasticsearch_indices_segment_memory_bytesLucene 段内存占用结合fielddataquery_cache等分析内存压力
断路器elasticsearch_breakers_tripped断路器触发次数> 0 表示曾因内存不足拒绝请求

重要说明:指标带有标签name(节点名)、clusterindex等,可灵活聚合。


4. Grafana 仪表盘推荐

  • Elasticsearch Cluster Overview:Dashboard ID2322(经典且全面,适配 elasticsearch_exporter 1.x),展示集群状态、节点健康、索引与搜索速率、JVM 内存等。
  • ES Metrics Exporter:ID4358,更现代,适合较新版本。
  • Elasticsearch by Prometheus:ID14191,针对 elasticsearch_exporter 优化。

导入后修改变量clusterinstance,即可呈现全集群健康视图。


5. 告警规则实战

groups:-name:elasticsearch_alertsrules:-alert:ElasticsearchClusterRedexpr:elasticsearch_cluster_health_status{color="red"}== 1for:1mlabels:severity:criticalannotations:summary:"Elasticsearch 集群状态为 Red,存在丢失数据风险"-alert:ElasticsearchClusterYellowexpr:elasticsearch_cluster_health_status{color="yellow"}== 1for:15mlabels:severity:warningannotations:summary:"Elasticsearch 集群状态为 Yellow,存在未分配副本分片"-alert:ElasticsearchUnassignedShardsexpr:elasticsearch_cluster_health_unassigned_shards>0for:5mlabels:severity:warningannotations:summary:"集群中存在 {{ $value }} 个未分配分片"-alert:ElasticsearchNodeDownexpr:elasticsearch_nodes_up < elasticsearch_cluster_health_number_of_nodesfor:1mlabels:severity:criticalannotations:summary:"一个或多个 Elasticsearch 节点离线"-alert:ElasticsearchHighJvmHeapUsageexpr:(elasticsearch_jvm_memory_used_bytes{area="heap"}/ elasticsearch_jvm_memory_max_bytes{area="heap"})>0.85for:10mlabels:severity:warningannotations:summary:"节点 {{ $labels.node }} JVM 堆内存使用率超过 85%"-alert:ElasticsearchHighSearchLatencyexpr:rate(elasticsearch_indices_search_query_time_seconds_total[5m]) / rate(elasticsearch_indices_search_query_total[5m])>1for:5mlabels:severity:warningannotations:summary:"平均搜索延迟超过 1 秒"-alert:ElasticsearchHighIndexingLatencyexpr:rate(elasticsearch_indices_indexing_index_time_seconds_total[5m]) / rate(elasticsearch_indices_indexing_index_total[5m])>0.5for:5mlabels:severity:warningannotations:summary:"平均索引延迟超过 500ms"-alert:ElasticsearchBreakerTrippedexpr:increase(elasticsearch_breakers_tripped[5m])>0labels:severity:criticalannotations:summary:"Elasticsearch 断路器被触发,内存压力过大"

根据集群规模和业务灵敏度调整阈值。


6. 进阶:多集群监控、TLS 认证与指标过滤

6.1 监控多个 Elasticsearch 集群

为每个集群启动一个 exporter 实例(可部署在同一主机不同端口),在 Prometheus 中配置多个 target 并打上cluster标签。也可使用 exporter 的多目标模式(--es.multi-target),通过 Prometheus 的__param_target动态传参,适合集中监控大量集群。

6.2 安全连接

若 Elasticsearch 使用自签名或内部 CA 证书,需挂载证书或跳过验证:

dockerrun-d\-eES_URI="https://prometheus:pass@es-host:9200"\-eES_SSL_SKIP_VERIFY=true\...

更严格的做法是提供 CA 证书路径(--es.ca-cert)。

6.3 指标性能优化
  • 通过--es.indices=false可禁用索引级别指标收集,大幅减少指标数量,适合仅需集群和节点监控的场景。
  • 适当调整scrape_interval为 30~60 秒,避免对 Elasticsearch 的_statsAPI 造成压力。

部署完成后,Elasticsearch 的集群健康、节点负载、JVM 压力、索引与搜索性能将一览无余。任何节点掉线、集群变红、搜索延迟飙升都会第一时间触发告警,配合之前落地的 Linux 主机监控和中间件监控,整个数据搜索层真正融入统一的可观测体系,为业务保驾护航。

http://www.jsqmd.com/news/1177031/

相关文章:

  • Apache DolphinScheduler 数据源架构深度解析:现代数据编排平台的多源集成与性能优化
  • 2026年7月最新青岛泰格豪雅官方售后维修服务网点地址与客服电话 - 亨得利官方服务中心
  • 别再用模板糊弄客户了,专业网站才是企业的第一张名片
  • 从零构建AI代理服务:LangGraph与FastAPI的完美结合指南
  • OpenWhispr隐私保护机制:你的语音数据如何被加密和安全存储
  • 2026成都装修公司排名口碑榜前10,旧房翻新别墅新房整装都适用 - 推荐官
  • 从硬件限制到自由安装:Rufus如何重新定义Windows启动盘制作
  • Midjourney批量生成如何做到“零人工干预”?揭秘头部AIGC工作室正在用的Python+Discord Webhook+Redis任务队列架构
  • Oracle SQL Developer 23.1 实战:从零配置连接到完成3个核心数据库操作
  • 高效实用的CS2内存偏移提取工具:5分钟快速上手指南
  • 公有继承与私有继承的区别
  • morss vs 其他RSS工具:为什么它是获取全文的最佳选择
  • 2026北京老房改造装修公司推荐:费用构成与选型避坑指南 - 优企名品
  • 新e选品牌烤火罩拿国家标准说话!
  • RDP Wrapper完全配置指南:解锁Windows远程桌面多用户并发连接
  • 你的 @staticmethod 为何“认不出”自己的类?——Python 静态方法与类方法的根本区别与选择法则
  • Android 开发问题:/storage/emulated/0/...: open failed: EACCES (Permission denied)
  • 2026成都装修公司施工口碑实测榜:从真实工地、节点验收到住后反馈,业主推荐十家装企 - 推荐官
  • 挣脱长文本算力枷锁,MLA多头隐式注意力如何重构大模型推理底层逻辑
  • 从技术分析到认知智能:AI量化交易的技术演进与实践路径
  • 如何快速配置Android自动化:Tasker权限完整指南
  • 华为MetaERP 标准成本体系:P2P 延伸 P2C 生产核算 SAP vs Oracle EBS 全流程分录 + 核心差异前置统一规则:两套系统均采用固定标准成本,存货入库、工单产出、销售出库永
  • 定时任务“裂变”惨案:Spring Boot 分布式调度从互殴到握手言和
  • 2026 铜川印台黄金回收实地测评:三大 30 年老字号全覆盖,全城上门无套路 - 福金阁黄金回收
  • 当AI接管汽车:2026年,我们是否正在给黑客开“后门”?
  • 编写派生的赋值运算符重载函数
  • go2_ros2_sdk进阶:使用CycloneDDS实现Unitree GO2机器人的以太网通信
  • Quartus Prime 23.1 工程文件结构规划:4文件夹法管理 RTL/仿真/约束/文档
  • RapidOCR技术架构深度解析:跨平台多引擎OCR解决方案的设计哲学与实践
  • 从零构建AI量化交易系统:5大核心技术实战指南