Elasticsearch集群健康 #

一、健康检查 #

1.1 集群健康API #

bash
GET /_cluster/health

响应:

json
{
  "cluster_name": "production",
  "status": "green",
  "timed_out": false,
  "number_of_nodes": 6,
  "number_of_data_nodes": 3,
  "active_primary_shards": 30,
  "active_shards": 60,
  "relocating_shards": 0,
  "initializing_shards": 0,
  "unassigned_shards": 0,
  "delayed_unassigned_shards": 0,
  "number_of_pending_tasks": 0,
  "number_of_in_flight_fetch": 0,
  "task_max_waiting_in_queue_millis": 0,
  "active_shards_percent_as_number": 100.0
}

1.2 健康指标说明 #

指标 说明
status 集群状态(green/yellow/red)
number_of_nodes 节点数量
number_of_data_nodes 数据节点数量
active_primary_shards 活跃主分片数
active_shards 活跃总分片数
relocating_shards 迁移中分片数
initializing_shards 初始化分片数
unassigned_shards 未分配分片数

1.3 索引级别健康 #

bash
GET /_cluster/health/products?level=shards

1.4 等待健康状态 #

bash
GET /_cluster/health?wait_for_status=green&timeout=30s

二、集群状态 #

2.1 查看集群状态 #

bash
GET /_cluster/state

2.2 过滤状态 #

bash
GET /_cluster/state/metadata,routing_table

2.3 集群统计 #

bash
GET /_cluster/stats

三、节点监控 #

3.1 节点列表 #

bash
GET /_cat/nodes?v

输出:

text
ip        heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
10.0.0.1  30           95          10  0.50    0.50    0.50     dilm      *      node-1

3.2 节点统计 #

bash
GET /_nodes/stats

GET /_nodes/node-1/stats

3.3 关键指标 #

指标 说明
heap.percent 堆内存使用率
ram.percent 系统内存使用率
cpu CPU使用率
load_1m 1分钟负载
node.role 节点角色

3.4 节点热线程 #

bash
GET /_nodes/hot_threads

四、索引监控 #

4.1 索引列表 #

bash
GET /_cat/indices?v

4.2 索引统计 #

bash
GET /products/_stats

GET /products/_stats/store,docs,indexing

4.3 关键指标 #

指标 说明
docs.count 文档数量
store.size 存储大小
indexing.index_total 索引总数
search.query_total 查询总数

4.4 段信息 #

bash
GET /_cat/segments?v

GET /products/_segments

五、分片监控 #

5.1 分片列表 #

bash
GET /_cat/shards?v

5.2 分片分配解释 #

bash
GET /_cluster/allocation/explain
{
  "index": "products",
  "shard": 0,
  "primary": true
}

5.3 分片统计 #

bash
GET /_stats

GET /products/_stats

六、性能指标 #

6.1 搜索性能 #

bash
GET /_nodes/stats/indices/search

关键指标:

指标 说明
query_total 查询总数
query_time_in_millis 查询总耗时
fetch_total 获取总数
fetch_time_in_millis 获取总耗时

6.2 索引性能 #

bash
GET /_nodes/stats/indices/indexing

关键指标:

指标 说明
index_total 索引总数
index_time_in_millis 索引总耗时
index_failed 索引失败数

6.3 缓存性能 #

bash
GET /_nodes/stats/indices/cache

关键指标:

指标 说明
query_cache.memory_size 查询缓存大小
fielddata.memory_size 字段数据缓存大小
request_cache.memory_size 请求缓存大小

6.4 JVM指标 #

bash
GET /_nodes/stats/jvm

关键指标:

指标 说明
mem.heap_used_percent 堆内存使用率
gc.collectors.old.collection_count 老年代GC次数
gc.collectors.old.collection_time 老年代GC时间

七、故障诊断 #

7.1 集群状态Red #

bash
GET /_cluster/health

GET /_cat/shards?v&state=UNASSIGNED

常见原因

  • 节点离线
  • 磁盘空间不足
  • 分片损坏

7.2 集群状态Yellow #

bash
GET /_cluster/health

GET /_cat/shards?v&state=UNASSIGNED

常见原因

  • 副本未分配
  • 节点数量不足

7.3 慢查询诊断 #

bash
GET /_nodes/stats/indices/search

GET /products/_search?explain=true
{
  "query": {
    "match": { "name": "iPhone" }
  }
}

7.4 内存问题 #

bash
GET /_nodes/stats/jvm

GET /_nodes/stats/indices/fielddata

解决方案

  • 增加堆内存
  • 减少字段数据缓存
  • 优化查询

八、告警配置 #

8.1 关键告警指标 #

text
告警指标
├── 集群状态
│   └── status != green
├── 节点状态
│   └── 节点离线
├── 堆内存
│   └── heap.percent > 85%
├── 磁盘空间
│   └── disk.percent > 85%
├── GC时间
│   └── gc.time > 阈值
└── 查询延迟
    └── query_time > 阈值

8.2 使用Kibana告警 #

在Kibana中配置告警规则:

  1. 进入 Stack Management > Alerts and Insights
  2. 创建告警规则
  3. 设置条件和动作

8.3 使用Elasticsearch Watcher #

bash
PUT /_watcher/watch/cluster_health
{
  "trigger": {
    "schedule": {
      "interval": "1m"
    }
  },
  "input": {
    "http": {
      "request": {
        "host": "localhost",
        "port": 9200,
        "path": "/_cluster/health"
      }
    }
  },
  "condition": {
    "compare": {
      "ctx.payload.status": {
        "not_eq": "green"
      }
    }
  },
  "actions": {
    "email_admin": {
      "email": {
        "to": "admin@example.com",
        "subject": "Cluster Health Alert",
        "body": "Cluster status is {{ctx.payload.status}}"
      }
    }
  }
}

九、监控最佳实践 #

9.1 监控层次 #

text
监控层次
├── 集群级别
│   ├── 集群状态
│   └── 节点状态
├── 节点级别
│   ├── 资源使用
│   └── JVM状态
├── 索引级别
│   ├── 文档数量
│   └── 存储大小
└── 查询级别
    ├── 查询延迟
    └── 查询吞吐

9.2 监控工具 #

text
监控工具
├── Kibana
│   └── Stack Monitoring
├── Elasticsearch Exporter
│   └── Prometheus + Grafana
├── Elastic Agent
│   └── Fleet管理
└── 自定义脚本
    └── API调用

十、总结 #

本章介绍了Elasticsearch集群健康:

  1. 集群健康API提供状态信息
  2. 节点和索引监控关键指标
  3. 性能指标反映系统状态
  4. 故障诊断需要系统方法
  5. 告警配置及时发现问题
  6. 分层监控全面覆盖

下一步,我们将学习安全配置。

最后更新:2026-03-27