Elasticsearch 集群状态恢复(RED 与 YELLOW)

集群状态解读

  1. 绿色——最健康的状态,代表所有的主分片和副本分片都可用;
  2. 黄色——所有的主分片可用,但是部分副本分片不可用;
  3. 部分主分片不可用。(此时执行查询部分数据仍然可以查到,遇到这种情况,还是赶快解决比较好。)
curl localhost:9200/_cluster/health
查看 _cat/shards

查看集群中不同节点、不同索引的状态:

curl localhost:9200/_cat/shards?h=index,shard,prirep,state,ur
my_index                     4 p STARTED
my_index                     4 r UNASSIGNED CLUSTER_RECOVERED
my_index                     3 p STARTED
my_index                     3 r UNASSIGNED CLUSTER_RECOVERED
my_index                     2 p STARTED
my_index                     2 r UNASSIGNED CLUSTER_RECOVERED
my_index                     1 p STARTED
my_index                     1 r UNASSIGNED CLUSTER_RECOVERED
my_index                     0 p STARTED
my_index                     0 r UNASSIGNED CLUSTER_RECOVERED

查看可用的表头字段

curl localhost:9200/_cat/shards?help
index                | i,idx                  | index name
shard                | s,sh                   | shard name
prirep               | p,pr,primaryOrReplica  | primary or replica
state                | st                     | shard state
docs                 | d,dc                   | number of docs in shard
store                | sto                    | store size of shard (how much disk it uses)
ip                   |                        | ip of node where it lives
id                   |                        | unique id of node where it lives
node                 | n                      | name of node where it lives
sync_id              | sync_id                | sync id
unassigned.reason    | ur                     | reason shard is unassigned
 其余略...
 

分片未被分配原因有下列类型:

  • INDEX_CREATED
    由于 create index api 创建索引导致,索引创建过程中,把索引的全部分片分配完毕需要一个过程,在全部分片分配完毕之前,该索引会处于短暂的 RED 或 YELLOW 状态。因此监控系统如果发现集群 RED,不一定代表出现了故障。

  • CLUSTER_RECOVERED
    集群完全重启时,所有分片都被标记为未分配状态,因此在集群完全重启时的启动阶段,reason 属于此种类型。

  • INDEX_REOPENED
    open 一个之前 close 的索引, reopen 操作会将索引分配重新分配。

  • DANGLING_INDEX_IMPORTED
    正在导入一个 dangling index,什么是 dangling index?
    磁盘中存在,而集群状态中不存在的索引称为 dangling index,例如从别的集群拷贝了一个索引的数据目录到当前集群,Elasticsearch 会将这个索引加载到集群中,因此会涉及到为 dangling index 分配分片的过程。

  • NEW_INDEX_RESTORED
    从快照恢复到一个新索引。

  • EXISTING_INDEX_RESTORED
    从快照恢复到一个关闭状态的索引。

  • REPLICA_ADDED
    增加分片副本。

  • ALLOCATION_FAILED
    由于分配失败导致。

  • NODE_LEFT
    由于节点离线。

  • REROUTE_CANCELLED
    由于显式的 cancel reroute 命令。

  • REINITIALIZED
    分片从 started 状态转换到 initializing 状态。

  • REALLOCATED_REPLICA
    由于迁移分片副本。

  • PRIMARY_FAILED
    初始化副分片时,主分片失效。

  • FORCED_EMPTY_PRIMARY
    强制分配一个空的主分片。

  • MANUAL_ALLOCATION
    手工强制分配分片。

修复 unassigned 分片问题

  1. 极端情况,删除整个索引,ES中没有直接删除分片的接口
    curl -XDELETE ‘localhost:9200/index_name
  2. 集群中节点数量 < 集群中所有索引的最大副本数量 +1
    主节点不会将主分片分配给与其副本相同的节点,也不会将同一分片的两个副本分配给同一个节点
    可以通过增加节点数,或者减少分片解决改中情况
    curl -XPUT http://localhost:9200/index1,index2.../_settings -H 'Content-Type: application/json' -d '{"number_of_replicas":1}'
  3. 使用 /_cluster/reroute 主动重新分配

常用命令

  1. 定位 UNASSIGNED 的节点和分片
curl -s 'localhost:9200/_cat/shards' | fgrep UNASSIGNED
  1. 分配分配详情
curl -XGET localhost:9200/_cluster/allocation/explain?pretty -H 'Content-Type: application/json' -d '{"index":"index_name","shard":2,"primary":true}'

  1. 查看每个节点分片的分配数量以及它们所使用的硬盘空间大小
curl localhost:9200/_cat/allocation?v

参考

  1. 参考1
  2. 官网文档地址:http://t.cn/RlttuVY
  3. Elasticsearch unassigned shards 应急处理方案 :http://t.cn/Rlwub5s
  4. 解决Unassigned Shards大探讨:http://t.cn/RlwuVFn
  5. 快照&重新存储数据方案:http://t.cn/RlwuXmm
  6. https://www.datadoghq.com/blog/elasticsearch-unassigned-shards/

你可能感兴趣的:(elasticsearch,集群,程序人生)