[ElasticSearch] ES集群狀態由非正常狀態(red)恢復為正常狀態(green)的思路與實踐


1 場景描述

1.1 資源與原規划

三台主機組成ES集群的規划:
集群名: xxx_elastic

  • 172.15.3.7 es1 master
  • 172.15.3.8 es2 (非master)
  • 172.15.3.9 es3 (非master)

1.2 原集群狀態

https://172.15.3.7:9200/_cluster/health?pretty
{
  "cluster_name" : "xxx_elastic",
  "status" : "red",
  "timed_out" : false,
  "number_of_nodes" : 1,
  "number_of_data_nodes" : 1,
  "active_primary_shards" : 492,
  "active_shards" : 553,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 95,
  "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" : 85.3395061728395
}

集群的異常之處: number_of_nodes / cluster status / unassigned_shards

  • number_of_nodes: 3
    (正常情況下,應該是: 3)
  • cluster status: red
    (正常情況下,應該是: green)
red: 非健康狀態; 部分的分片可用,表明分片有一部分損壞。一般情況下,表明存在 unassigned 的索引分片(shards:碎片,分片)。
     此時執行查詢部分數據仍然可以查到,遇到這種情況,還是趕快解決比較好; 
     這種情況Elasticsearch集群至少一個主分片(以及它的全部副本)都在缺失中。
     這意味着你在缺少數據:搜索只能返回部分數據,而分配到這個分片上的寫入請求會返回一個異常。

yellow: 亞健康狀態;基本的分片可用,但是備份不可用(或者是沒有備份);  
        這種情況Elasticsearch集群所有的主分片已經分片了,但至少還有一個副本是缺失的。
        不會有數據丟失,所以搜索結果依然是完整的。
        不過,你的高可用性在某種程度上被弱化。
        如果更多的分片消失,就會丟數據了。
        把 yellow 想象成一個需要及時調查的警告。

green: 最健康狀態;說明所有的分片包括備份都可用; 這種情況Elasticsearch集群所有的主分片和副本分片都已分配, Elasticsearch集群是 100% 可用的。
  • unassigned_shards: 95
    (正常情況下,應該是: 0)

unssigned 即 未分配副本分片的問題

2 解決思路

以消除 unassigned_shards:0 為主要目標

step1 確保集群節點數達到理論節點數

即 恢復全部ES節點合為1個ES集群
(這能大幅度,乃至完全地消除 unassigned_shards 的數量)
本操作完成后,unsigned狀態的索引碎片(shards)由95個降低為25個

  • 修正為正確的ES節點名稱(node.name)
vi /etc/elasticsearch/elasticsearch.yml
    #
    # Use a descriptive name for the node:
    #
    node.name: es3
  • 確保集群所有節點: 啟動狀態 + 開機自啟
[CentOS6]
[root@es1 ~]# chkconfig elasticsearch on (開機自啟)
[root@es3 ~]# service elasticsearch start (啟動ES服務)

[CentOS7]
[root@es1 ~]# systemctl enable elasticsearch (開機自啟)
[root@es3 ~]# systemctl start  elasticsearch (啟動ES服務)
  • 新節點加入集群
    以 配置丟失的節點node8(es2)加入目標集群 為例
    elasticsearch.yml的配置項 推薦文獻: elasticsearch配置
[node7 / node8 / node9]
vi /etc/elasticsearch/elasticsearch.yml 
	# 配置向master節點單播通信的IP(默認通信端口為9200)
	# 單播配置下,節點向指定的主機發送單播請求
	# 默認配置中的主機對應的對外通信端口為9200;若該主機對外通信端口非9200端口時,需具體指定
	# 一般可只填寫master節點
	discovery.zen.ping.unicast.hosts: ["172.15.3.7"]
	# 設置master的個數
	discovery.zen.minimum_master_nodes: 1
	transport.tcp.port: 9300

[node8 / node9] 取消 node8 / node9 節點的默認(master)配置; 
vi /etc/elasticsearch/elasticsearch.yml 
	node.master: false

step2 消除剩余 unassigned 的 索引分片(shards)

red狀態的索引,要么reroute,要么刪除之

[瀏覽器] https://172.15.3.7:9200/_nodes?pretty
	找到node8節點的唯一主機標識 jprFXcCqRVGCSNU3M02ZbQ

[瀏覽器] https://172.15.3.7:9200/_cat/shards?v
	查找 UNASSIGNED 的索引
或者
[root@es1 ~]# curl --insecure -u admin:admin "https://172.15.3.9:9200/_cat/shards" | grep UNASSIGNED
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
sharedb_bae200fe08354928976fd075bb944a5e_20191223160406                              1 p UNASSIGNED  
sharedb_bae200fe08354928976fd075bb944a5e_20191223160406                               3 p UNASSIGNED
sharedb_01d4aa88707448dc9010030249a0b8ab_20200401151836                                p UNASSIGNED    
...
[root@es1]# curl -XDELETE -u admin:admin --insecure "https://172.15.3.7:9200/sharedb_bae200fe08354928976fd075bb944a5e_20191223160406 "
{"acknowledged":true}

(逐次刪除狀態為red的索引, sharedb_bae200fe08354928976fd075bb944a5e_20191223160406 為索引號)

Over~~

{
  "cluster_name" : "xxx_elastic",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 3,
  "number_of_data_nodes" : 3,
  "active_primary_shards" : 542,
  "active_shards" : 609,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0,
  "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" : 100.0
}

補充:ES的安裝/配置 文件的一般路徑

ps -ef | grep elasticsearch

/etc/elasticsearch/...
/etc/elasticsearch/elasticsearch.yml

/usr/share/elasticsearch/...
/usr/share/elasticsearch/logs/...
/usr/share/elasticsearch/plugins/search-guard-6/tools/hash.sh
/usr/share/elasticsearch/plugins/search-guard-6/tools/sgadmin.sh
/usr/share/elasticsearch/plugins/search-guard-6/sgconfig/sg_internal_users.yml

/opt/elsatic-6.4.1/...
/opt/elsatic-6.4.1/configure_file/elasticsearch.yml

/home/elasticsearch/...

X 文獻


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM