# /\_cluster/reroute automatically reroute and rebalance all primary shards

**URL:** <https://discuss.elastic.co/t/cluster-reroute-automatically-reroute-and-rebalance-all-primary-shards/161757>\
**Category:** Elasticsearch\
**Created:** [December 20, 2018, 10:02pm UTC](https://discuss.elastic.co/t/cluster-reroute-automatically-reroute-and-rebalance-all-primary-shards/161757 "2018-12-20T22:02:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![raulk89](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@raulk89](https://discuss.elastic.co/u/raulk89)\
**Post date:** [December 20, 2018, 10:02pm UTC](https://discuss.elastic.co/t/cluster-reroute-automatically-reroute-and-rebalance-all-primary-shards/161757/1 "2018-12-20T22:02:51Z")

</div>

Elasticsearch 6.5.4

I was wondering, if there is possibility that elasticsearch automatically rebalances shards on nodes.

For example, when I have 2 or 3 or or more node cluster, and I restart single node, then after service restart, this node has all the indexes replica shards only. And to get some primary shards routed to this node, I'd have to manually do the reroute, for example, I restarted **elasticnode2** , then I'd have to manually reroute some shards for every index like this:

```
POST /_cluster/reroute
{
   "commands": [
      {
         "move": {
            "index": "index_name1",
            "shard": 2,
            "from_node": "elasticnode1",
            "to_node": "elasticnode2"
         }
      }
   ]
}

```

[https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-reroute.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-reroute.html)

I am looking a way, that elasticsearch automatically allocates some primary shards to this restarted node, when it is back up and running.

Thanks.

Regards  
Raul

---

<div class="post-metadata">

**Author:** ![raulk89](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@raulk89](https://discuss.elastic.co/u/raulk89)\
**Post date:** [December 20, 2018, 10:23pm UTC](https://discuss.elastic.co/t/cluster-reroute-automatically-reroute-and-rebalance-all-primary-shards/161757/2 "2018-12-20T22:23:29Z")

</div>

I can't even manually achieve this, since **elasticnode2** has all the shards already allocated (replica shards), which get automatically allocated after node rejoin cluster (after service restart).

I get error when executing this cluster reroute command:

> ```
> {
> "decider" : "same_shard",
> "decision" : "NO",
> "explanation" : "the shard cannot be allocated to the same node on which a copy of the shard already exists [[index_name1][0], node[K81sF6LQRP6VJaFJOqnYFg], [R], s[STARTED], a[id=mFB2EofwRdW_Ec9YkMQkOQ]]"
> },
> 
> ```

On way of achieving this, is:

- curl -sXPUT `hostname`':9200/\_cluster/settings?pretty' -H 'Content-Type: application/json' -d '{"persistent": {"cluster.routing.allocation.enable": "none"}}'
- elasticnode2 service restart
- (shards won't get allocated to **elasticnode2** , since allocation is disabled)
- then start manually allocating shards.
- at the end curl -sXPUT `hostname`':9200/\_cluster/settings?pretty' -H 'Content-Type: application/json' -d '{"persistent": {"cluster.routing.allocation.enable": "null"}}'

Regards  
Raul

---

<div class="post-metadata">

**Author:** ![DavidTurner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidturner/32/22453_2.png) [@DavidTurner](https://discuss.elastic.co/u/DavidTurner)\
**Post date:** [December 21, 2018, 1:21pm UTC](https://discuss.elastic.co/t/cluster-reroute-automatically-reroute-and-rebalance-all-primary-shards/161757/3 "2018-12-21T13:21:26Z")

</div>

> [@raulk89](#):
>
> I am looking a way, that elasticsearch automatically allocates some primary shards to this restarted node, when it is back up and running.

Why do you want to do this? There should be no need to balance primaries vs replicas.

---

<div class="post-metadata">

**Author:** ![raulk89](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@raulk89](https://discuss.elastic.co/u/raulk89)\
**Post date:** [December 21, 2018, 1:44pm UTC](https://discuss.elastic.co/t/cluster-reroute-automatically-reroute-and-rebalance-all-primary-shards/161757/4 "2018-12-21T13:44:16Z")

</div>

Hi

Hmm, well, when I create a new index, then elasticsearch automatically balances primary shards between nodes, for example, with 2 node cluster, then one node has 2 and other has 3 primary shards.

I am thinking, that if this is elasticsearch default behaviour, then perhaps it is way to go..🙂

And would’nt the searxh operations be faster if primary shards are balanced between nodes..?

Regards

Raul

---

<div class="post-metadata">

**Author:** ![DavidTurner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidturner/32/22453_2.png) [@DavidTurner](https://discuss.elastic.co/u/DavidTurner)\
**Post date:** [December 21, 2018, 2:05pm UTC](https://discuss.elastic.co/t/cluster-reroute-automatically-reroute-and-rebalance-all-primary-shards/161757/5 "2018-12-21T14:05:54Z")

</div>

> [@raulk89](#):
>
> Hmm, well, when I create a new index, then elasticsearch automatically balances primary shards between nodes, for example, with 2 node cluster, then one node has 2 and other has 3 primary shards.

This is an interesting artefact of how Elasticsearch allocates shards: it will allocate the primaries first and then the replicas, one at a time, trying to maintain balance throughout the process, so when all the primaries are allocated and all of the replicas are unallocated then the balanced configuration is as you describe.

However it's not really necessary to maintain this balance, which is why it doesn't happen after a node restarts. Simpler just to leave the primaries where they are.

> [@raulk89](#):
>
> And would’nt the searxh operations be faster if primary shards are balanced between nodes..?

It shouldn't make a difference. All copies of a shard can service searches whether they be primary or replica.

---

<div class="post-metadata">

**Author:** ![raulk89](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@raulk89](https://discuss.elastic.co/u/raulk89)\
**Post date:** [December 21, 2018, 2:47pm UTC](https://discuss.elastic.co/t/cluster-reroute-automatically-reroute-and-rebalance-all-primary-shards/161757/6 "2018-12-21T14:47:22Z")

</div>

Hi

Ok, thanks for clearing this.

Regards

Raul

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 18, 2019, 2:47pm UTC](https://discuss.elastic.co/t/cluster-reroute-automatically-reroute-and-rebalance-all-primary-shards/161757/7 "2019-01-18T14:47:25Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
