解决 Elasticsearch 集群状态报 RED 存在未分配主分片故障
分析 Elasticsearch 集群状态标红(RED)且主分片未分配的根因,详解 _cluster/allocation/explain 定位与 reroute 强制分配步骤。
问题现象
Elasticsearch 监控看板报警,执行 _cluster/health 查询显示集群健康度为 red:
{
"cluster_name": "production-es",
"status": "red",
"timed_out": false,
"number_of_nodes": 5,
"number_of_data_nodes": 5,
"active_primary_shards": 120,
"active_shards": 240,
"relocating_shards": 0,
"initializing_shards": 0,
"unassigned_shards": 2
}
对应受损索引的读写请求全部被阻断抛出异常。
触发根因
在 Elasticsearch 规范中,只要有任意一个主分片(Primary Shard)处于未分配(Unassigned)状态,集群整体状态就会被标记为 RED:
- 承载该主分片的节点物理宕机且没有副本分片可晋升。
- 磁盘空间触顶(达到 Flood Stage 95% 水位):ES 拒绝分配任何新分片。
- Lucene 索引损坏或分片元数据损坏。
诊断排查步骤
- 使用 Cluster Allocation Explain API 精准获取未分配的根本原因:
curl -s "http://localhost:9200/_cluster/allocation/explain?pretty"
响应中会详细列出 unassigned_info.reason(例如 ALLOCATION_FAILED 或 NODE_LEFT)以及 ES 为何拒绝分配到每个节点的原因。
解决步骤与操作命令
步骤一:优先恢复离线的数据节点
如果排查出某台持有分片数据的节点(如 node-3)意外断电,直接将其拉起:
sudo systemctl start elasticsearch
节点重新加入集群后,主分片会自动完成恢复,状态由 RED 变为 GREEN。
步骤二:清理节点磁盘水位(如因磁盘爆满)
如果是因为磁盘空间超过 95% 导致无法分配:
curl -X DELETE "http://localhost:9200/old-logs-*"
磁盘低于 85% 后,触发集群自动重新分片分配:
curl -X POST "http://localhost:9200/_cluster/reroute?retry_failed=true"
步骤三:强制分配空分片(数据已损毁时的终极恢复方案)
若原节点硬件彻底烧毁无法复原,为恢复业务索引读写,可以强制为索引分配一个新的空主分片(原分片数据会丢失):
curl -X POST "http://localhost:9200/_cluster/reroute" -H 'Content-Type: application/json' -d'
{
"commands": [
{
"allocate_empty_primary": {
"index": "orders-2026.11",
"shard": 0,
"node": "node-1",
"accept_data_loss": true
}
}
]
}'
步骤四:验证集群健康状态恢复
curl -s "http://localhost:9200/_cluster/health?pretty"
确认 status 恢复为 green 或 yellow,unassigned_shards 归零。


