背景
在Elasticsearch集群运维中你是否遇到过这样的问题,集群中某几个节点上聚集过多的索引,导致集节点压力过大,资源使用产生瓶颈,而其余节点资源过剩,集群内读写产生倾斜,一些节点分片数过多,另外一部分却含有较少的分片。本文将介绍如何通过分片过滤器合理的迁移分片。
基本概念
在Elasticsearch中可以使用分片分配过滤器将索引分配到指定的节点,通过这样的方式可以使索引的分片均匀分散在集群中。分片分配过滤器是基于节点自定义属性。在索引生命周期管理(ilm)中同样基于该属性决定索引该迁往哪些属性节点上。
分片路由分配配置可以动态设置,允许索引从一组节点移动到另一组节点上。shard只有在不打破另一个路由约定下才能被重新分配,比如同一个分片主副本分片不能在统一节点上。

路由设置
index.routng.allocation.include.{attribute}:将索引分配给包含此条件的任意一个节点。
index.routing.allocation.require.{attribute}:将索引分配给必须满足所有条件的节点。
index.routing.allocation.exclude.{attribute}:将索引分配给不包含此条件的所有节点。
attribute支持以下属性设置:_name、_host_ip、_publish_ip、_ip、_host、_tier
环境准备
测试环境准备
1、首先在集群中各个节点配置文件elasticsearch.yml中设置属性node.attr.{attribute},
或者在节点启动时指定节点属性:
./bin/elasticsearch -Enode.attr.box_type=hot
| node.name | box_type |
|---|---|
| node-1 | hot |
| node-2 | warm |
| node-3 | cold |
2、新建索引 allocation_shard_index,分片数3,副本数0
PUT allocation_shard_index
{
"index":{
"number_of_replicas":0,
"number_of_shards":3
}
}
}
当前索引分片分布
| index | shard | node |
|---|---|---|
| allocation_shard_index | 0 | node-1 |
| allocation_shard_index | 1 | node-1 |
| allocation_shard_index | 2 | node-1 |
分片路由测试
1、include标签,包含单个属性
PUT allocation_shard_index/_settings
{
"index.routing.allocation.include.box_type":"warm"
}
当前索引分片分布
| index | shard | node |
|---|---|---|
| allocation_shard_index | 0 | node-2 |
| allocation_shard_index | 1 | node-2 |
| allocation_shard_index | 2 | node-2 |
符合分配规则。
2、include标签,包含单个属性
PUT allocation_shard_index/_settings
{
"index.routing.allocation.include.box_type":"hot,cold"
}
当前索引分片分布
| index | shard | node |
|---|---|---|
| allocation_shard_index | 0 | node-1 |
| allocation_shard_index | 1 | node-1 |
| allocation_shard_index | 2 | node-1 |
索引分片只分配到include标签指定的节点hot属性节点。
3、require标签,要求box_type属性为cold,node.name为node-3
PUT allocation_shard_index/_settings
{
"index.routing.allocation.require.box_type":"cold",
"index.routing.allocation.require._name":"node-3",
}
当前索引分片分布
| index | shard | node |
|---|---|---|
| allocation_shard_index | 0 | node-3 |
| allocation_shard_index | 1 | node-3 |
| allocation_shard_index | 2 | node-3 |
node-3均满足以上两个条件,符合分配规则。
4、exclude标签,要求box_type属性不为cold
PUT allocation_shard_index/_settings
{
"index.routing.allocation.exclude.box_type":"cold"
}
当前索引分片分布
| index | shard | node |
|---|---|---|
| allocation_shard_index | 0 | node-2 |
| allocation_shard_index | 1 | node-2 |
| allocation_shard_index | 2 | node-2 |
符合分配规则。




