查看未分配分片(unassigned shard)的原因及刪除包含未分配分片的索引(index)

查看未分配分片的原因:

#!/bin/bash

# 查詢所有未分配的分片
unassigned_shards=$(curl -s -X GET -uxxxx:xxxxxx "http://xxxxxx:9200/_cat/shards?v" | grep UNASSIGNED)

# 打印未分配的分片信息
echo "未分配的分片信息:"
echo "$unassigned_shards"

# 提取索引和分片信息
echo "$unassigned_shards" | while read -r line; do
  index=$(echo $line | awk '{print $1}')
  shard=$(echo $line | awk '{print $2}')
  echo "解釋索引 $index 分片 $shard 未分配的原因:"
  curl -X GET "http://localhost:9200/_cluster/allocation/explain" -H 'Content-Type: application/json' -d"
  {
    \"index\": \"$index\",
    \"shard\": $shard,
    \"primary\": true
  }
  "
  echo ""
done

原因是其中一個節點宕機后索引損壞,類似如下:

解釋索引 app-log-2024.05.24.10 分片 1 未分配的原因:
{"error":{"root_cause":[{"type":"index_not_found_exception","reason":"no such index","index_uuid":"_na_","index":"app-log-2024.05.24.10"}],"type":"index_not_found_exception","reason":"no such index","index_uuid":"_na_","index":"app-log-2024.05.24.10"},"status":404}
解釋索引 app-log-2024.05.24.10 分片 2 未分配的原因:
{"error":{"root_cause":[{"type":"index_not_found_exception","reason":"no such index","index_uuid":"_na_","index":"app-log-2024.05.24.10"}],"type":"index_not_found_exception","reason":"no such index","index_uuid":"_na_","index":"app-log-2024.05.24.10"},"status":404}
解釋索引 app-log-2024.05.24.10 分片 0 未分配的原因:
{"error":{"root_cause":[{"type":"index_not_found_exception","reason":"no such index","index_uuid":"_na_","index":"app-log-2024.05.24.10"}],"type":"index_not_found_exception","reason":"no such index","index_uuid":"_na_","index":"app-log-2024.05.24.10"},"status":404}
解釋索引 .monitoring-es-7-2024.05.24 分片 0 未分配的原因:
{"error":{"root_cause":[{"type":"index_not_found_exception","reason":"no such index","index_uuid":"_na_","index":".monitoring-es-7-2024.05.24"}],"type":"index_not_found_exception","reason":"no such index","index_uuid":"_na_","index":".monitoring-es-7-2024.05.24"},"status":404}
解釋索引 app-log-2024.02.26.07 分片 1 未分配的原因:
{"error":{"root_cause":[{"type":"index_not_found_exception","reason":"no such index","index_uuid":"_na_","index":"app-log-2024.02.26.07"}],"type":"index_not_found_exception","reason":"no such index","index_uuid":"_na_","index":"app-log-2024.02.26.07"},"status":404}
解釋索引 k8s-log-2024.05.23 分片 0 未分配的原因:
{"error":{"root_cause":[{"type":"index_not_found_exception","reason":"no such index","index_uuid":"_na_","index":"k8s-log-2024.05.23"}],"type":"index_not_found_exception","reason":"no such index","index_uuid":"_na_","index":"k8s-log-2024.05.23"},"status":404}

批量刪除包含未分配分片所在的索引

#!/bin/bash

# 獲取所有包含未分配分片的索引
unassigned_indices=$(curl -s -X GET -uxxx:xxxxxxxxx "http://xxxxxxxx:9200/_cat/shards?v" | grep UNASSIGNED | awk '{print $1}' | sort | uniq)

# 打印待刪除的索引
echo "待刪除的索引:"
echo "$unassigned_indices"

# 批量刪除包含未分配分片的索引
for index in $unassigned_indices; do
  echo "刪除索引: $index"
  curl -X DELETE -uxxx:xxxxxx "http://xxxxxxxx:9200/$index"
done
image.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容