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 主机监控和中间件监控,整个数据搜索层真正融入统一的可观测体系,为业务保驾护航。