环境:
- Ubuntu16.04
- elasticsearch 6.2.3
- 3个master节点,10个data节点
- 每个分片有一个副本
故障:
将一个数据节点的elasticsearch换成docker elasticsearch,分词器没有添加到plugins中。随后把分词器添加到plugins中后,发现有的分片没有被分配,但是ES集群启动正常,只不过一直是yellow状态。而且unassigned分片一直未被分配。
解决办法
首先执行:GET user/_recovery?active_only=true
发现集群并没有进行副本恢复。
点击未进行分配的分片,发现allocation_status: "no_attempt"
原因是:shard 自动分配 已经达到最大重试次数5次,仍然失败了,所以导致"shard的分配状态已经是:no_attempt"。这时在Kibana Dev Tools,执行命令:POST /_cluster/reroute?retry_failed=true
即可。由index.allocation.max_retries
参数来控制最大重试次数。
The cluster will attempt to allocate a shard a maximum of index.allocation.max_retries times in a row (defaults to 5), before giving up and leaving the shard unallocated.
当执行reroute
命令对分片重新路由后,ElasticSearch会自动进行负载均衡,负载均衡参数cluster.routing.rebalance.enable
默认为true。
It is important to note that after processing any reroute commands Elasticsearch will perform rebalancing as normal (respecting the values of settings such as cluster.routing.rebalance.enable) in order to remain in a balanced state.
总结
一般来说,ElasticSearch会自动分配 那些 unassigned shards,当发现某些shards长期未分配时,首先看下是否是因为:为索引指定了过多的primary shard 和 replica 数量,然后集群中机器数量又不够。另一个原因就是本文中提到的:由于故障,shard自动分配达到了最大重试次数了,这时执行 reroute 就可以了。