Elasticsearch的UNASSIGNED shareds怎么恢复?

今天查看索引,发现很多索引分片都是UNASSIGNED的,而且都是replication的分片,具体情况如下: [code]# curl -s -XGET http://localhost:9200/_cat/shards|grep UNASSIGNED mobile_20161030 3 r UNASSIGNED mobile_20161030 1 r UNASSIGNED server_20161030 0 r UNASSIGNED server_20161030 4 r UNASSIGNED mobile_20161031 2 r UNASSIGNED mobile_20161031 5 r UNASSIGNED server_20161031 3 r UNASSIGNED server_20161031 4 r UNASSIGNED weblog_20161031 5 r UNASSIGNED weblog_20161031 0 r UNASSIGNED[/code]这种情况,应该怎么恢复,让集群恢复到green状态?
已邀请:

空心菜 - 心向阳光,茁壮成长

赞同来自: push

如果UNASSIGNED的分片较多的话,可以先找到master节点的唯一标识:
curl 'localhost:9200/_nodes/process?pretty'

{
"cluster_name" : "elasticsearch",
"nodes" : {
"AfUyuXmGTESHXpwi4OExxx" : {
"name" : "Master",
....
"attributes" : {
"master" : "true"
},
.....
然后利用脚本批量处理:
#!/bin/bash

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 'localhost:9200/_cluster/reroute' -d "{
'commands' : [ {
'allocate' : {
'index' : $index,
'shard' : $shard,
'node' : 'Master',
'allow_primary' : true
}
}
]
}"

sleep 5
done
done
批量处理的脚本(当数量很多的话, 注意替换node的名字)

空心菜 - 心向阳光,茁壮成长

如果你的索引是有副本的,可以直接把索引的分片数设置为0,然后索引肯定会是green状态:
curl -XPUT ‘http://localhost:9200/mobile_20161031/_settings’ -d '{ "number_of_replicas": 0 }';echo

然后当然你也可以强制reroute:
curl -XPOST 'http://localhost:9200/_cluster/reroute' -d '{
"commands" : [ {
"allocate" : {
"index" : "mobile_20161031",
"shard" : 2,
"node" : "crHfi-olQ5ObXp5YDuQ-0Q",
"allow_primary" : true
}
}]
}'
index就是索引的名称, node:就是在哪个节点上执行,shared:你需要reroute的分片的编号。

要回复问题请先登录注册