介绍
You can use custom node attributes as awareness attributes to enable Elasticsearch to take your physical hardware configuration into account when allocating shards.
If Elasticsearch knows which nodes are on the same physical server, in the same rack, or in the same zone,
it can distribute the primary shard and its replica shards to minimise the risk of losing all shard copies in the event of a failure.
按官网介绍,Shard allocation awareness是es提高高可用的技术,通过配置节点的属性,让分片的分配把硬件也考虑进来。你可以配置节点所处的服务器、机架和地区,分布主备分片在不同区域,令分片不可用的风险降至最低。
启用配置
启动的时候给节点配置节点自定义属性loc
bin/elasticsearch -E node.name=node0 -E cluster.name=joker -E node.attr.loc=china bin/elasticsearch -E node.name=node1 -E cluster.name=joker -E node.attr.loc=china bin/elasticsearch -E node.name=node2 -E cluster.name=joker -E node.attr.loc=USA
发起集群强制分片感知
PUT _cluster/settings { "persistent": { "cluster.routing.allocation.awareness.attributes": "loc", "cluster.routing.allocation.awareness.force.zone.values": [ "china", "USA" ] } }
发起索引分片分配
PUT test/_settings { "index.routing.allocation.include.loc": "china" }
查看节点分片数据,可以看到主副分片都不会同时分布在同一个区域的节点
GET /_cat/shards