elasticsearch可以通過reroute api來手動進行索引分片的分配。
不過要想完全手動,必須先把cluster.routing.allocation.disable_allocation參數設置爲true,禁止es進行自動索引分片分配,否則你從一節點把分片移到另外一個節點,那麼另外一個節點的一個分片又會移到那個節點。
一共有三種操作,分別爲:移動(move),取消(cancel)和分配(allocate)。下面分別介紹這三種情況:
移動(move)
把分片從一節點移動到另一個節點。可以指定索引名和分片號。
取消(cancel)
取消分配一個分片。可以指定索引名和分片號。node參數可以指定在那個節點取消正在分配的分片。allow_primary參數支持取消分配主分片。
分配(allocate)
分配一個未分配的分片到指定節點。可以指定索引名和分片號。node參數指定分配到那個節點。allow_primary參數可以強制分配主分片,不過這樣可能導致數據丟失。
下面是個簡單的例子:
curl -XPOST 'localhost:9200/_cluster/reroute' -d '{
"commands" : [ {
"move" :
{
"index" : "test", "shard" : 0,
"from_node" : "node1", "to_node" : "node2"
}
},
"cancel" :
{
"index" : "test", "shard" : 0, "node" : "node1"
}
},
{
"allocate" : {
"index" : "test", "shard" : 1, "node" : "node3"
}
}
]
}'