在一次巡檢中發現一個 Elasticsearch 集羣處於 red 狀態:
$ curl --user xxx:xxx -X GET 'localhost:9200/_cat/health?v&pretty'
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1649831279 06:27:59 cluster-sk red 6 6 3155 3154 0 0 2 0 - 99.9%
開始 google,找到如下說法:
- red 狀態表示存在主分片未分配到任一節點;
- yellow表示存在副本分片未分配。
在上面集羣狀態中也有顯示 unassign 2
,那麼,先嚐試對未分配的分片重新分配一下:
$ curl --user xxxx:xxxx -X POST "localhost:9200/_cluster/reroute?retry_failed=true"
再次查看集羣狀態:
$ curl --user elastic:elastic -X GET 'localhost:9200/_cat/health?v&pretty'
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1649834617 07:23:37 cluster-sk green 6 6 3157 3156 0 0 0 0 - 100.0%
green!問題解決~
Elasticsearch 相關命令
-
查看 cluster 狀態
# 顯示title和結果 $ curl --user xxxx:xxxx -X GET 'localhost:9200/_cat/health?v&pretty' # 只顯示結果 $ curl --user xxxx:xxxx -X GET "localhost:9200/_cluster/health?pretty"
-
查看 cluster 詳情
$ curl --user xxxx:xxxx -X GET 'localhost:9200/_cluster/stats?human&pretty'
-
查看 shards 分片
$ curl --user xxxx:xxxx -X GET "localhost:9200/_cat/shards"
-
查看 node 狀態
$ curl --user xxxx:xxxx -X GET "localhost:9200/_cat/nodes?v"
-
查看 node 詳情
$ curl --user xxxx:xxxx -X GET "localhost:9200/_nodes/process?pretty"
-
查看 allocation 詳情:顯示錯誤詳細原因(結果可結合 json 解析器 分析)
$ curl --user xxxx:xxxx -X GET "localhost:9200/_cluster/allocation/explain"
-
對 unsigned 分片進行重新分配
$ curl --user xxxx:xxxx -X POST "localhost:9200/_cluster/reroute?retry_failed=true"
-
按日期刪除索引:https://juejin.cn/post/6844903472878321671
$ curl -u xxxx:xxxx -H'Content-Type:application/json' -d' { "query": { "range": { //範圍 "@timestamp": {//時間字段 "lt": "now-7d",//lt是小於(<),lte是小於等於(<=),gt是大於(>),gte是大於等於(>=),now-7d是當前時間減7天 "format": "epoch_millis" } } } } ' -XPOST "http://127.0.0.1:9200/*-*/_delete_by_query?pretty"
-
熱變更 es 配置:https://www.elastic.co/guide/en/elasticsearch/reference/7.6/cluster-update-settings.html