ES宕机重启后,当某些分片自动分配失败时,可以手动进行分配
1.查看分配失败的原因
curl -u user:passwd -XGET localhost:9200/_cluster/allocation/explain?pretty
如果原因显示:
shard has exceeded the maximum number of retries [5] on failed allocation attempts - manually call [/_cluster/reroute?retry_failed=true] to retry, [unassigned_info[[reason=ALLOCATION_FAILED], at[2020-03-22T23:09:44.896Z], failed_attempts[5],delayed=false, details[failed shard on node [bsddxejzTzqKrgkRf7I1_A]: failed recovery, failure RecoveryFailedException[[logstash-statis-2020.03.22 xxxxxxxxxxx***
可尝试执行以下命令进行处理:
curl -u user:passwd -XPOST 'http://ip:9200/_cluster/reroute?retry_failed=true'
2.第1步的解决方法不成功时,可以尝试以下方法进行reroute
控制台直接执行
POST /_cluster/reroute
{
"commands":[
{
"allocate_replica": {
"index": "logstash-statis-2020.03.22", ##要reroute的索引
"shard": 2, ##查询出的shard编号
"node": "Logstash_ES1" ##节点名
}
}
]
}
可通过以下命令查找shard编号
curl -u user:passwd -XGET 'http://ip:9200/_cat/shards' | grep UNASSIGNED
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1096k 100 1096k 0 0 303k 0 0:00:03 0:00:03 --:–:-- 303k
logstash-statis-2020.03.22 2 p UNASSIGNED
logstash-statis-2020.03.22 2 r UNASSIGNED
3.如果重分配失败,而又要保留数据,还可以进行索引重建
控制台直接执行
POST /_reindex
{
"source": {
"index": "logstash-statis-2020.03.22"
},
"dest": {
"index": "logstash-statis-2020.03.22-bak",
"version_type": "external"
}
}
重建成功后,将原索引删除,如果索引名要与之前保持一致,可以给现索引添加一个别名
DELETE logstash-statis-2020.03.22
PUT logstash-statis-2020.03.22-bak/_alias/logstash-statis-2020.03.22
查看ES集群监控状态,看是否还有尚未分配的分片
curl -u user:passwd -XGET 'http://ip:9200/_cluster/health?pretty'
{
“cluster_name” : “Leniu-Elasticsearch”,
“status” : “green”, #监控健康状态为绿色
“timed_out” : false,
“number_of_nodes” : 3,
“number_of_data_nodes” : 3,
“active_primary_shards” : 4182,
“active_shards” : 8365,
“relocating_shards” : 0,
“initializing_shards” : 0,
“unassigned_shards” : 0, #未分配的分片数量为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
}