Shard allocation issues

Shard allocation issues

Ques - My cluster is red due to one or more unassigned shards. How do I bring it back to green ?
Ans - ES provides an explain API to give more information on unassinged shards. For example, when you find some shards in UNASSIGNED state for a very long time, you may use the following command to check the reason.

curl -s -H Content-Type:application/json http://localhost:9200/_cluster/allocation/explain?pretty -d '

{
"index": "my-index-000001",
"shard": 0,
"primary": true,
"current_node": "my-node"
}'
  
You can also give without any arguments where ES will report information for an arbitrary unassigned shard.

In general the following can be reasons

  1. The reported shard data could be corrupted
  2. The ES did not find a matching node for allocating the shard. This can happen if you have custom routing rules for allocation of shards.
  3. Cluster node unavailability issues.
  4. Replica enabled for the index however no replication node is available.
You can actually try force assign an empty shard by using the following command if its #1 or #2 above.  Please note that previous shard data for the index will be lost.

curl -XPOST -H Content-Type:application/json "localhost:9200/_cluster/reroute?pretty" -H 'Content-Type: application/json' -d'
{
"commands" : [
{
"allocate_empty_primary" : {
"index" : "my-index-00001",
"shard" : 0,
"node" : "<NODE_NAME>",
"accept_data_loss" : "true"
}
}
]
}
'