2.9 Elasticsearch-监控栈:Metricbeat + Elasticsearch Exporter + Grafana

在这里插入图片描述
2.9 Elasticsearch-监控栈:Metricbeat + Elasticsearch Exporter + Grafana
——让搜索集群自己“开口说话”


2.9.1 为什么需要专门的 ES 监控栈
Elasticsearch 暴露的 _stats、_cluster/stats、_nodes/stats 等端点已经足够丰富,但存在三个痛点:

  1. 指标维度多、JSON 嵌套深,Prometheus 无法直接解析;
  2. 部分关键指标(如 GC 次数、索引 throttle 时间)散落在不同端点,需要二次聚合;
  3. 日志、指标、链路分散,难以与业务监控统一展示。
    Metricbeat + Elasticsearch Exporter 的组合正好补齐短板:Metricbeat 负责“采”,Exporter 负责“转”,Grafana 负责“看”,三件套零入侵、全托管,5 分钟可落地。

2.9.2 架构总览

                    ┌------------------┐
                    │  Elasticsearch   │
                    │  7.x/8.x 集群     │
                    └--------┬---------┘
                             │9200
                             │
            ┌----------------┴----------------┐
            │        Metricbeat 8.x          │
            │  module: elasticsearch         │
            │  output: Prometheus remote_write│
            └----------------┬----------------┘
                             │10902
            ┌----------------┴----------------┐
            │ elasticsearch_exporter 1.7     │
            │  --es.uri=https://<user>:<pwd> │
            │  --es.all --es.indices --es.shards│
            └----------------┬----------------┘
                             │9090
            ┌----------------┴----------------┐
            │       Prometheus 2.45          │
            │  scrape_interval: 15s          │
            └----------------┬----------------┘
                             │
                    ┌--------┴---------┐
                    │ Grafana 10.x     │
                    │  Dashboard 14191 │
                    └------------------┘

说明:

  1. Metricbeat 与 Exporter 可同时运行,互不冲突;前者侧重“集群+节点”层,后者侧重“索引+分片”层。
  2. 若已部署 Elastic Agent,可直接使用 elasticsearch 集成,省去 Metricbeat 侧配置。
  3. Prometheus remote_write 支持 VictoriaMetrics、Thanos、Grafana Cloud,后续扩容无感。

2.9.3 安装与配置

  1. Elasticsearch Exporter(二进制方式,K8s 同理)

下载

wget https://github.com/prometheus-community/elasticsearch_exporter/releases/download/v1.7.0/elasticsearch_exporter-1.7.0.linux-amd64.tar.gz
tar xzf elasticsearch_exporter-1.7.0.linux-amd64.tar.gz

systemd 服务

cat > /etc/systemd/system/elasticsearch_exporter.service <<‘EOF’
[Unit]
Description=Elasticsearch Exporter
After=network.target

[Service]
Type=simple
User=elastic
ExecStart=/usr/local/bin/elasticsearch_exporter \
–es.uri=https://elastic:changeme@localhost:9200 \
–es.all --es.indices --es.shards --es.snapshots \
–es.ca=“” --es.client-private-key=“” --es.client-cert=“” \
–web.listen-address=:9090
Restart=always
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload && systemctl enable --now elasticsearch_exporter

  1. Metricbeat(采集节点、索引、分片、ML、CCR 等 47 类指标)

8.x 默认已带 elasticsearch module

metricbeat modules enable elasticsearch
cat > /etc/metricbeat/modules.d/elasticsearch.yml <<‘EOF’

  • module: elasticsearch
    metricsets:
    • node # 节点 CPU、内存、磁盘
    • node_stats # JVM、线程池、GC
    • index # 索引级 docs、store、throttle
    • index_recovery # 恢复流量、耗时
    • index_summary # 集群级索引总量
    • shard # 分片分布、unassigned 原因
    • ml_job # 机器学习作业状态
    • ccr_stats # 跨集群复制延迟
      period: 10s
      hosts: [“https://localhost:9200”]
      username: “elastic”
      password: “changeme”
      ssl.verification_mode: “none”
      EOF

输出到 Prometheus remote_write(需启用 beat-exporter 或 remote_write 插件)

cat >> /etc/metricbeat/metricbeat.yml <<‘EOF’
output.prometheus:
hosts: [“localhost:9201”]
namespace: “metricbeat”
EOF
systemctl restart metricbeat

  1. Prometheus 抓取
    scrape_configs:
  • job_name: ‘elasticsearch_exporter’
    static_configs:
    • targets: [‘es-master01:9090’,‘es-data01:9090’,‘es-data02:9090’]
  • job_name: ‘metricbeat’
    static_configs:
    • targets: [‘es-master01:9201’,‘es-data01:9201’,‘es-data02:9201’]
  1. Grafana 导入
  • 官方 Dashboard ID 14191(Elasticsearch Exporter Full),再叠加自建“Metricbeat ES Overview”(ID 2324)。
  • 建议变量模板化:cluster、node、index、shard,方便多集群横向对比。

2.9.4 核心指标与告警规则

层级指标说明推荐阈值
集群elasticsearch_cluster_health_status{color=“red”}分片无法分配==1 立即告警
集群elasticsearch_cluster_health_number_of_unassigned_shards未分配分片数>0 持续 5min
节点elasticsearch_node_stats_jvm_mem_percentJVM 堆使用率>85% 警告,>95% 严重
节点elasticsearch_node_stats_process_cpu_percent节点 CPU>80% 持续 10min
索引elasticsearch_index_store_size_bytes / elasticsearch_index_docs_count单文档平均大小突增相比 1h 前 +300%
索引elasticsearch_index_indexing_throttle_time_seconds_total索引限速时间5min 内增量 >30s
线程池elasticsearch_thread_pool_queue_count{name=“write”}write 队列堆积>1000
GCelasticsearch_jvm_gc_collection_seconds_sum{gc=“young”}Young GC 累计耗时1min 速率 >5s
GCelasticsearch_jvm_gc_collection_seconds_sum{gc=“old”}Old GC 累计耗时1min 速率 >2s

PrometheusRule 示例:
groups:

  • name: elasticsearch
    interval: 15s
    rules:
    • alert: ESClusterRed
      expr: elasticsearch_cluster_health_status{color=“red”}==1
      for: 0m
      labels:
      severity: critical
      annotations:
      summary: “ES cluster {{ $labels.cluster }} status RED”
    • alert: ESHeapUsageHigh
      expr: elasticsearch_node_stats_jvm_mem_percent > 95
      for: 2m
      labels:
      severity: critical
      annotations:
      summary: “Node {{ $labels.node }} heap usage > 95%”

2.9.5 索引级精细化监控

  1. 热温冷架构
    利用 elasticsearch_index_settings_routing_allocation_require_* 标签,在 Grafana 中按 node_role 分组,对比 hot/warm/cold 节点的写入速率、合并段大小、查询 QPS,验证 ILM 策略是否生效。

  2. 分片重平衡
    监控 elasticsearch_cluster_routing_table_shards_number{state=“RELOCATING”},配合 elasticsearch_indices_indexing_index_time_seconds_total 速率,判断重平衡是否影响写入。

  3. 慢查询 TopN
    Metricbeat 已采集 elasticsearch_index_search_query_time_seconds_sum,通过 Recording Rule 预聚合:

  • topk(10, rate(elasticsearch_index_search_query_time_seconds_sum[5m]) / rate(elasticsearch_index_search_query_total[5m]))
    在 Grafana 中做“慢查询榜”,直接下钻到对应索引与节点。

2.9.6 性能调优 checklist

  1. Exporter 自身耗时
    –es.timeout 默认 5s,集群大时调大到 30s;–es.snapshots 若无需快照指标可关闭,减少拉取时间。

  2. Prometheus 样本量
    单集群 3000 索引、10 节点时,exporter 暴露约 18 万样本,建议:

  • 丢弃高基数 label:metric_relabel_configs 丢弃 elasticsearch_index_settings_uuid、elasticsearch_shard_id;
  • 采样周期从 15s 放宽到 30s,样本量减半。
  1. Metricbeat 负载
    metricbeat 单实例可扛 50 节点,超过时拆分为多实例,按 node_attr 分组采集。

  2. 安全加固

  • exporter 只读账号:
    POST _security/role/elasticsearch_exporter
    { “cluster”: [“monitor”], “indices”: [{“names”: [“*”],“privileges”: [“monitor”]}]}
  • 开启 HTTPS + RBAC,Grafana 端使用 Token 访问 Prometheus。

2.9.7 小结

Metricbeat 做“厚”采集,Elasticsearch Exporter 做“深”转换,Grafana 做“炫”呈现,三件套互补,覆盖集群、节点、索引、分片、线程池、GC、快照、CCR 等 200+ 指标,配合 Prometheus 的告警生态,可在“业务感知”前提前发现:

  • 分片分配失败
  • 节点磁盘写满
  • 索引限速
  • 慢查询堆积
  • Young/Old GC 异常

落地过程零代码、零入侵,只需 systemd 或 Helm 一键部署,即可让 Elasticsearch 像普通中间件一样纳入云原生监控体系,为后续 SLA、容量预测、自动扩缩容提供数据底座。
更多技术文章见公众号: 大城市小农民

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乔丹搞IT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值