【es实战-分片分配失败解决方案】

分片分配失败的原因

1、线上集群突然断电或者使用了kill -9 pid进行集群关闭

2、磁盘信道损坏

3、使用了错误的配置(小问题,排查副本/分片路由等配置即可)

排查解决方案

1、查看未分配的原因

GET /_cluster/allocation/explain
GET _cat/indices?v&health=red
GET /_cat/shards?v&h=n,index,shard,prirep,state,sto,sc,unassigned.reason,unassigned.details

ALLOCATION_FAILED:由于分片分配失败而未分配。
CLUSTER_RECOVERED:由于集群恢复而未分配。
DANGLING_INDEX_IMPORTED:由于导入了悬空索引导致未分配。
EXISTING_INDEX_RESTORED:由于恢复为已关闭的索引导致未分配。
INDEX_CREATED:由于API创建索引而未分配。
INDEX_REOPENED:由于打开已关闭索引而未分配。
NEW_INDEX_RESTORED:由于恢复到新索引而未分配。
NODE_LEFT:由于托管的节点离开集群而未分配。
REALLOCATED_REPLICA:确定了更好的副本位置,并导致现有副本分配被取消。
REINITIALIZED:当分片从开始移动回初始化,导致未分配。
REPLICA_ADDED:由于显式添加副本而未分配。
REROUTE_CANCELLED:由于显式取消重新路由命令而未分配。

2、尝试重新分配失败的分片

POST /_cluster/reroute?retry_failed=true 

默认索引的尝试次数为5,可以将此参数调大尝试reroute,也许有奇效:

PUT /indexname/_settings 
{ "index": { "allocation": { "max_retries": 20 } } }

3、重新启停集群尝试

4、重新关开索引尝试

POST /index/_close(_open) 

5、将副本分片提升为主分片
如果确定了主分片已经损坏,可以尝试将副本分片提升为主(会丢部分数据):

POST /_cluster/reroute?pretty
{
  "commands": [
    {
      "allocate_stale_primary": {
        "index": "indexname",//索引名
        "shard": 3,//操作的分片id
        "node": "node1",//此分片副本位于的节点
        "accept_data_loss": true//提示数据可能会丢失
      }
    }
  ]
}

此方案存在一个问题是需要提前知道此分片的副本位于哪个节点用以指定,可以通过如果api获取副本分片位置:

GET indexname/_shard_stores?pretty

判断当前es进程使用的数据目录:通过pid和yml配置的目录去匹配,如data

ll /proc/pid/fd |grep data

如果索引损坏导致api失效,则需要人工去数据目录进行查找副本分片位置,目录结构如下:

data/nodes/0/indices/Z60wvPOWSP6Qbk79i757Vg/0

数据目录下为节点号 -> 索引文件夹 -> 索引ID -> 分片号
6、将此分片置为空分片
如果此分片的主副都已经损坏,则可将此分片置为空以保留索引其他分片数据:

{
  "commands": [
    {
      "allocate_empty_primary": {
        "index": "indexname",//索引名
        "shard": 3,//操作的分片id
        "node": "node1",//空分片要分配的节点
        "accept_data_loss": true//提示数据可能会丢失
      }
    }
  ]
}

如果集群存在大量索引分片无法恢复,则可以使用脚本将全部分片置空,可以基于下面的脚本修改:

#!/bin/bash
master=$(curl -s 'http://localhost:9200/_cat/master?v' | grep -v ' ip ' | awk '{print $1}')
for index in $(curl  -s 'http://localhost:9200/_cat/shards' | grep UNASSIGNED | awk '{print $1}' | sort | uniq); do
    for shard in $(curl  -s 'http://localhost:9200/_cat/shards' | grep UNASSIGNED | grep $index | awk '{print $2}' | sort | uniq); do
        echo  $index $shard
        curl -XPOST -H 'Content-Type: application/json'  'http://localhost:9200/_cluster/reroute' -d '{
            "commands" : [ {
                  "allocate_empty_primary" : {
                      "index" : "'$index'",
                      "shard" : "'$shard'",
                      "node" : "'$master'",
	              "accept_data_loss" : true
                  }
                }
            ]
        }'
        sleep 1
    done
done
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值