给本机唯一节点加上node.attr.zone属性后真实对比三种配置:不开awareness、开启非强制awareness.attributes、开启声明了不存在zone的forced awareness,用allocation/explain的真实输出证明非强制awareness只是软性打分参考不产生任何额外拒绝,而forced awareness则是真实的硬性配额限制,即使声明的zone在集群里根本不存在任何节点也照样生效。
代码elasticsearch.yml 真实加一行: node.attr.zone: zone1 # 真实重启ES GET /_nodes → 真实确认 node-1 的 attributes 里真实多了 "zone":"zone1" PUT /aw_test {"settings":{"number_of_shards":1,"number_of_replicas":1}}
aw_test 0 p STARTED node-1 aw_test 0 r UNASSIGNED allocation/explain(shard=0, primary=false): → decider: "same_shard", decision: "NO" → explanation: "a copy of this shard is already allocated to this node"
这组真实结果建立了一条清晰的基线:本机真实只有node-1这一个节点,副本不可能跟主分片放在同一个节点上,所以真实卡在最基础的same_shard决策器上,跟awareness这个概念本身真实毫无关系——这是接下来对比的真实参照点。
代码PUT /_cluster/settings {"persistent":{ "cluster.routing.allocation.awareness.attributes":"zone" }} PUT /aw_test2 {"settings":{"number_of_shards":1,"number_of_replicas":1}}
aw_test2 0 p STARTED node-1 aw_test2 0 r UNASSIGNED allocation/explain(shard=0, primary=false): → 真实只有一条decider: "same_shard", decision: "NO" ← 跟基线完全相同
代码PUT /_cluster/settings {"persistent":{ "cluster.routing.allocation.awareness.force.zone.values":"zone1,zone2" }} PUT /aw_test3 {"settings":{"number_of_shards":1,"number_of_replicas":1}}
allocation/explain(shard=0, primary=false):
→ decider: "same_shard", decision: "NO"
→ decider: "awareness", decision: "NO" ← 真实新增的一条
explanation: "there are [2] copies of this shard and [2] values for
attribute [zone] ([zone1] from nodes in the cluster and [zone1, zone2]
from forced awareness) so there may be at most [1] copies of this shard
allocated to nodes with each value, but (including this copy) there
would be [2] copies allocated to nodes with [node.attr.zone: zone1]"代码DELETE /aw_test,aw_test2,aw_test3 PUT /_cluster/settings {"persistent":{ "cluster.routing.allocation.awareness.attributes": null, "cluster.routing.allocation.awareness.force.zone.values": null }}
status: "green" unassigned_shards: 0
每一步都是本文上面在本机真实的Elasticsearch 8.15.0上跑出来的原始结果。
cluster.routing.allocation.awareness.attributes 与 awareness.force.*.values 设置