ES出现unassigned_shards解决方法

1. 查看健康状态信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ curl -XGET http://localhost:9200/_cluster/health\?pretty
{
"cluster_name" : "docker-cluster",
"status" : "yellow",
"timed_out" : false,
"number_of_nodes" : 1,
"number_of_data_nodes" : 1,
"active_primary_shards" : 23,
"active_shards" : 23,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 6,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 79.3103448275862
}

2.找到未分片的索引

1
2
3
4
5
6
$ curl -s "http://localhost:9200/_cat/shards" | grep UNASSIGNED
test 0 r UNASSIGNED
logstash-2021.01.22 0 r UNASSIGNED
logstash-2021.01.23 0 r UNASSIGNED
logstash-2021.01.18 0 r UNASSIGNED
logstash-2021.01.19 0 r UNASSIGNED

3.强制分配主分片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#先查节点唯一标识
$ curl -XGET http://localhost:9200/_nodes/process?pretty
{
"_nodes" : {
"total" : 1,
"successful" : 1,
"failed" : 0
},
"cluster_name" : "docker-cluster",
"nodes" : {
"NOmRjiA6T3i_plKA340Ong" : {
"name" : "4abbcb382dc4",
"transport_address" : "172.17.0.2:9300",
"host" : "172.17.0.2",
"ip" : "172.17.0.2",
"version" : "7.4.2",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "2f90bbf7b93631e52bafb59b3b049cb44ec25e96",
"roles" : [
"ingest",
"master",
"data",
"ml"
],
"attributes" : {
"ml.machine_memory" : "2147483648",
"xpack.installed" : "true",
"ml.max_open_jobs" : "20"
},
"process" : {
"refresh_interval_in_millis" : 1000,
"id" : 1,
"mlockall" : false
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#强制分配主分片
$ curl -XPOST localhost:9200/_cluster/reroute -d '
{
"commands": [
{
"allocate_replica": {
"index": "logstash-2021.01.18",
"shard": 0,
"node": "NOmRjiA6T3i_plKA340Ong",
"allow_primary": "true"
}
}
]
}'

#或者在kibana页面执行
POST _cluster/reroute
{
"commands": [
{
"allocate_replica": {
"index": "logstash-2021.01.18",
"shard": 0,
"node": "NOmRjiA6T3i_plKA340Ong",
"allow_primary": "true"
}
}
]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#批量处理的脚本(当数量很多时, 注意替换node的名字)
#!/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' : 'Master',
'allow_primary' : true
}
}
]
}"

sleep 5
done
done

4.删除该索引

如果这个index 已经不用了,直接删除 index,这些 unassigned 的分片也会被干掉,集群恢复正常

1
$ curl -XDELETE localhost:9200/logstash-2021.01.18
-------------本文结束感谢您的阅读-------------