介紹
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
