首先確保elasticsearch處於啟動狀態,啟動elasticsearch的命令為/etc/init.d/elasticsearch start 。啟動后我們基於9200端口驗證es是否啟動成功。具體如下圖所示。
然后我們輸入命令"curl -XGET 'http://localhost:9200/_cluster/health?pretty'" 查看集群狀態信息,從下圖我們可以看到集群處於yellow狀態,且存在unassigned_shards信息。具體如下圖所示。
然后我們可以執行命令"curl -XGET 'http://localhost:9200/_cat/shards' | grep UNASSIGNED" 查看狀態為unassigned的分片信息。具體展示如下圖所示,可以看到出現了5條記錄信息。
然后我們執行命令"curl -XGET 'http://localhost:9200/_nodes/stats?pretty'"查看節點的名稱,這個名稱后面處理未注冊的shards時候要用到。具體結果如下圖所示。
然后我們執行分片的強制reroute 具體命令格式為:"
curl -XPOST 'localhost:9200/_cluster/reroute' -d '{
"commands" : [ {
"allocate" : {
"index" : "winlogbeat-2020.09.14",
"shard" : 4,
"node" : "oYZfyZ7MR2C996c58Wl9nw",
"allow_primary" : true
}
}
]
}'"
其中 index代表要處理的索引名稱,shard代表要處理的分配序號,node代表節點名稱。
具體如下圖所示。
待我們手動循環執行以上腳本,處理未注冊的shard信息后,我們再次驗證集群的狀態,同樣輸入命令"curl -XGET 'http://localhost:9200/_cluster/health?pretty'",查看集群狀態,可以看到集群狀態已經為green了。
最后在給大家提供一種腳本,批量處理未注冊的shard信息的方法。腳本內容如下:
#!/bin/bash
for index in $(curl -s 'http://localhost:9200/_cat/shards' | grep UNASSIGNED | awk '{print $1}' | sort | uniq); do
for shard in $(curl -s 'http://localhost:9200/_cat/shards' | grep UNASSIGNED | grep $index | awk '{print $2}' | sort | uniq); do
echo $index $shard
curl -XPOST 'localhost:9200/_cluster/reroute' -d "{
'commands' : [ {
'allocate' : {
'index' : $index,
'shard' : $shard,
'node' : 'oYZfyZ7MR2C996c58Wl9nw',
'allow_primary' : true
}
}
]
}"
sleep 5
done
done
當然其中的node對應的值需要各位更改為自己節點的名稱。