1. 集群健康状况处理

当集群处于yellow或者red状态的时候,整体处理步骤如下:

(1) 首先查看集群状态

localhost:/_cluster/health?pretty

{
  "cluster_name": "elasticsearch",
  "status": "yellow",
  "timed_out": false,
  "number_of_nodes": 1,
  "number_of_data_nodes": 1,
  "active_primary_shards": 278,
  "active_shards": 278,
  "relocating_shards": 0,
  "initializing_shards": 0,
  "unassigned_shards": 278,
  "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": 50
}

主要关注其中的unassigned_shards指标,其代表已经在集群状态中存在的分片,但是实际在集群里又找不着。通常未分配分片的来源是未分配的副本。比如,一个有 5 分片和 1 副本的索引,在单节点集群上,就会有 5 个未分配副本分片。如果你的集群是 red 状态,也会长期保有未分配分片(因为缺少主分片)。其他指标解释:

(1) initializing_shards 是刚刚创建的分片的个数。比如,当你刚创建第一个索引,分片都会短暂的处于 initializing 状态。这通常会是一个临时事件,分片不应该长期停留在 initializing 状态。你还可能在节点刚重启的时候看到 initializing 分片:当分片从磁盘上加载后,它们会从 initializing 状态开始。

(2) number_of_nodes 和 number_of_data_nodes 这个命名完全是自描述的。

(3) active_primary_shards 指出你集群中的主分片数量。这是涵盖了所有索引的汇总值。

(4) active_shards 是涵盖了所有索引的_所有_分片的汇总值,即包括副本分片。

(5) relocating_shards 显示当前正在从一个节点迁往其他节点的分片的数量。通常来说应该是 0,不过在 Elasticsearch 发现集群不太均衡时,该值会上涨。比如说:添加了一个新节点,或者下线了一个节点。

(2)查找问题索引

curl -XGET 'localhost:9200/_cluster/health?level=indices'

{
"cluster_name": "elasticsearch",
"status": "yellow",
"timed_out": false,
"number_of_nodes": ,
"number_of_data_nodes": ,
"active_primary_shards": ,
"active_shards": ,
"relocating_shards": ,
"initializing_shards": ,
"unassigned_shards": ,
"delayed_unassigned_shards": ,
"number_of_pending_tasks": ,
"number_of_in_flight_fetch": ,
"task_max_waiting_in_queue_millis": ,
"active_shards_percent_as_number": ,
"indices": {
"gaczrk": {
"status": "yellow",
"number_of_shards": ,
"number_of_replicas": ,
"active_primary_shards": ,
"active_shards": ,
"relocating_shards": ,
"initializing_shards": ,
"unassigned_shards":
},
"special-sms-extractor_zhuanche_20200204": {
"status": "yellow",
"number_of_shards": ,
"number_of_replicas": ,
"active_primary_shards": ,
"active_shards": ,
"relocating_shards": ,
"initializing_shards": ,
"unassigned_shards":
},
"specialhtl201905": {
"status": "yellow",
"number_of_shards": ,
"number_of_replicas": ,
"active_primary_shards": ,
"active_shards": ,
"relocating_shards": ,
"initializing_shards": ,
"unassigned_shards":
},
        "v2": {
"status": "red",
"number_of_shards": 10,
"number_of_replicas": 1,
"active_primary_shards": 0,
"active_shards": 0,
"relocating_shards": 0,
"initializing_shards": 0,
"unassigned_shards": 20
},
       "sms20181009": { 
"status": "yellow",
"number_of_shards": ,
"number_of_replicas": ,
"active_primary_shards": ,
"active_shards": ,
"relocating_shards": ,
"initializing_shards": ,
"unassigned_shards":
},
......

这个参数会让 cluster-health API 在我们的集群信息里添加一个索引清单,以及有关每个索引的细节(状态、分片数、未分配分片数等等),一旦我们询问要索引的输出,哪个索引有问题立马就很清楚了:v2 索引。我们还可以看到这个索引曾经有 10 个主分片和一个副本,而现在这 20 个分片全不见了。可以推测,这 20 个索引就是位于从我们集群里不见了的那两个节点上。一般来讲,Elasticsearch是有自我分配节点功能的,首先查看这个功能是否开启:

curl -XGET 'localhost:9200/_cluster/settings?pretty' -d
'{
"persistent": {},
"transient": {
"cluster": {
"routing": {
"allocation": {
"enable": "all"
}
}
}
}
}'

level 参数还可以接受其他更多选项:

localhost:/_cluster/health?level=shards

{
"cluster_name": "elasticsearch",
"status": "yellow",
"timed_out": false,
"number_of_nodes": ,
"number_of_data_nodes": ,
"active_primary_shards": ,
"active_shards": ,
"relocating_shards": ,
"initializing_shards": ,
"unassigned_shards": ,
"delayed_unassigned_shards": ,
"number_of_pending_tasks": ,
"number_of_in_flight_fetch": ,
"task_max_waiting_in_queue_millis": ,
"active_shards_percent_as_number": ,
"indices": {
"gaczrk": {
"status": "yellow",
"number_of_shards": ,
"number_of_replicas": ,
"active_primary_shards": ,
"active_shards": ,
"relocating_shards": ,
"initializing_shards": ,
"unassigned_shards": ,
"shards": {
"": {
"status": "yellow",
"primary_active": true,
"active_shards": ,
"relocating_shards": ,
"initializing_shards": ,
"unassigned_shards":
},
"": {
"status": "yellow",
"primary_active": true,
"active_shards": ,
"relocating_shards": ,
"initializing_shards": ,
"unassigned_shards":
},
"": {
"status": "yellow",
"primary_active": true,
"active_shards": ,
"relocating_shards": ,
"initializing_shards": ,
"unassigned_shards":
},
"": {
"status": "yellow",
"primary_active": true,
"active_shards": ,
"relocating_shards": ,
"initializing_shards": ,
"unassigned_shards":
},
"": {
"status": "yellow",
"primary_active": true,
"active_shards": ,
"relocating_shards": ,
"initializing_shards": ,
"unassigned_shards":
}
}
},
......

shards 选项会提供一个详细得多的输出,列出每个索引里每个分片的状态和位置。这个输出有时候很有用,但是由于太过详细会比较难用。

(3) 手动分配未分配分片

查询未分配分片的节点以及未分配原因

localhost:/_cat/shards?v&h=index,shard,prirep,state,unassigned.reason

index                                   shard prirep state      unassigned.reason
gaczrk p STARTED
gaczrk r UNASSIGNED CLUSTER_RECOVERED
gaczrk p STARTED
gaczrk r UNASSIGNED CLUSTER_RECOVERED
gaczrk p STARTED

未分配原因说明:

INDEX_CREATED:  由于创建索引的API导致未分配。
CLUSTER_RECOVERED: 由于完全集群恢复导致未分配。
INDEX_REOPENED: 由于打开open或关闭close一个索引导致未分配。
DANGLING_INDEX_IMPORTED: 由于导入dangling索引的结果导致未分配。
NEW_INDEX_RESTORED: 由于恢复到新索引导致未分配。
EXISTING_INDEX_RESTORED: 由于恢复到已关闭的索引导致未分配。
REPLICA_ADDED: 由于显式添加副本分片导致未分配。
ALLOCATION_FAILED: 由于分片分配失败导致未分配。
NODE_LEFT: 由于承载该分片的节点离开集群导致未分配。
REINITIALIZED: 由于当分片从开始移动到初始化时导致未分配(例如,使用影子shadow副本分片)。
REROUTE_CANCELLED: 作为显式取消重新路由命令的结果取消分配。
REALLOCATED_REPLICA: 确定更好的副本位置被标定使用,导致现有的副本分配被取消,出现未分配。
 

然后执行命令手动分配:

curl -XPOST 'localhost:9200/_cluster/reroute' -d '{
"commands": [{
"allocate": {
"index": "gaczrk(索引名称)",
"shard": 4分片编号),
"node": "其他node的id",
"allow_primary": true
}
}]
}'

如果未分片较多的话,可以用如下脚本进行自动分派:

#!/bin/bash
array=( node1 node2 node3 )
node_counter=
length=${#array[@]}
IFS=$'\n'
for line in $(curl -s 'http://127.0.0.1:9200/_cat/shards'| fgrep UNASSIGNED); do
INDEX=$(echo $line | (awk '{print $1}'))
SHARD=$(echo $line | (awk '{print $2}'))
NODE=${array[$node_counter]}
echo $NODE
curl -XPOST 'http://127.0.0.1:9200/_cluster/reroute' -d '{
"commands": [
{
"allocate": {
"index": "'$INDEX'",
"shard": '$SHARD',
"node": "'$NODE'",
"allow_primary": true
}
}
]
}'
node_counter=$(((node_counter)%length +))
done

(4) 快速分配分片

在上面的命令执行输出结果中,假如所有的primary shards都是好的,所有replica shards有问题,有一种快速恢复的方法,就是强制删除掉replica shards,让elasticsearch自主重新生成。 首先先将出问题的index的副本为0

curl -XPUT '/问题索引名称/_settings?pretty' -d '{
"index" : {
"number_of_replicas" :
}
}'

然后观察集群状态,最后通过命令在恢复期索引副本数据

curl -XGET '/问题索引名称/_settings
{
"index" : {
"number_of_replicas" :
}
}

等待节点自动分配后,集群成功恢复成gree

(5)集群分片始终处于 INITIALIZING状态

curl -XGET 'localhost:9200/_cat/shards/7a_cool?v&pretty'

7a_cool   r STARTED       .4mb 10.2.4.21 pt01-pte----
7a_cool r INITIALIZING 10.2.4.22 pt01-pte---- 《==异常分片

解决办法:

1)首先关闭异常分片主机es 服务;

登陆pt01-pte---- 主机  ,/etc/init.d/elasticsearch  stop 
如果分片自动迁移至其它主机,状态恢复,则集群正常,如果状态还是在初始化状态,则说明问题依旧存在;则执行上面手动分配分片命令,如果问题依然存在,则将问题索引分片副本数置为0,让集群
自主调整集群分片,调整完成后集群状态变成:green

Elasticsearch学习之集群常见状况处理(干货)的更多相关文章

  1. 原创 | 手摸手带您学会 Elasticsearch 单机、集群、插件安装(图文教程)

    欢迎关注笔者的公众号: 小哈学Java, 每日推送 Java 领域干货文章,关注即免费无套路附送 100G 海量学习.面试资源哟!! 个人网站: https://www.exception.site/ ...

  2. 全文搜索引擎 Elasticsearch 入门:集群搭建

    本文主要介绍什么是 ElasticSearch 以及为什么需要它,如何在本机安装部署 ElasticSearch 实例,同时会演示安装 ElasticSearch 插件,以及如何在本地部署多实例集群, ...

  3. hadoop 集群常见错误解决办法

    hadoop 集群常见错误解决办法 hadoop 集群常见错误解决办法: (一)启动Hadoop集群时易出现的错误: 1.   错误现象:Java.NET.NoRouteToHostException ...

  4. Docker学习-Kubernetes - 集群部署

    Docker学习 Docker学习-VMware Workstation 本地多台虚拟机互通,主机网络互通搭建 Docker学习-Docker搭建Consul集群 Docker学习-简单的私有Dock ...

  5. 使用Spring Data ElasticSearch+Jsoup操作集群数据存储

    使用Spring Data ElasticSearch+Jsoup操作集群数据存储 1.使用Jsoup爬取京东商城的商品数据 1)获取商品名称.价格以及商品地址,并封装为一个Product对象,代码截 ...

  6. Elasticsearch高级之-集群搭建,数据分片

    目录 Elasticsearch高级之-集群搭建,数据分片 一 广播方式 二 单播方式 三 选取主节点 四 什么是脑裂 五 错误识别 Elasticsearch高级之-集群搭建,数据分片 es使用两种 ...

  7. 1.ElasticSearch系列之集群部署

    第一步:安装JDK JDK要求jdk1.8+,不安装也可以,ES自带JDK 第二步:系统配置 2.1 禁用交换区 sudo swapoff -a 2.2 开最大文件数的限制 编辑文件 /etc/sec ...

  8. Elasticsearch 6.x版本全文检索学习之集群调优建议

    1.系统设置要到位,遵照官方建议设置所有的系统参数. https://www.elastic.co/guide/en/elasticsearch/reference/6.7/setup.html 部署 ...

  9. Elasticsearch 教程--分布式集群

    集群 补充章节 正如前文提到的,这就是第个补充的章节,这里会介绍 Elasticsearch 如何在分布式环境中运行. 本章解释了常用术语,比如 集群 (cluster), 节点 (node) 以及  ...

随机推荐

  1. PAT 乙级 1022.D进制的A+B C++/Java

    1022 D进制的A+B (20 分) 题目来源 输入两个非负 10 进制整数 A 和 B (≤),输出 A+B 的 D (1)进制数. 输入格式: 输入在一行中依次给出 3 个整数 A.B 和 D. ...

  2. Laravel5.4框架中视图共享数据的方法详解

    本文实例讲述了Laravel5.4框架中视图共享数据的方法.分享给大家供大家参考,具体如下: 每个人都会遇到这种情况:某些数据还在每个页面进行使用,比如用户信息,或者菜单数据,最基本的做法是在每个视图 ...

  3. 日志分析查看——grep,sed,sort,awk运用

    概述 我们日常应用中都离不开日志.可以说日志是我们在排查问题的一个重要依据.但是日志并不是写了就好了,当你想查看日志的时候,你会发现线上日志堆积的长度已经超越了你一行行浏览的耐性的极限了.于是,很有必 ...

  4. hiveserver2和zookeeper的HA搭建(转)

    最近公司新项目申请资源,虚拟机资源打开时候使用source login.sh的脚本来进行登录注册,好奇心驱使下看了看里面的shell脚本,使用到了hiveserver2的zookeeper连接,百度一 ...

  5. 如何保护你的 Python 代码 (一)—— 现有加密方案

    https://zhuanlan.zhihu.com/p/54296517 0 前言 去年11月在PyCon China 2018 杭州站分享了 Python 源码加密,讲述了如何通过修改 Pytho ...

  6. You Can Customize Synthesized Instance Variable Names @property

    As mentioned earlier, the default behavior for a writeable property is to use an instance variable c ...

  7. How do I fix "selector not recognized" runtime exceptions when trying to use category methods from a static library?

    https://developer.apple.com/library/content/qa/qa1490/_index.html A: If you're seeing a "select ...

  8. Object archiving

    https://developer.apple.com/library/content/documentation/General/Conceptual/DevPedia-CocoaCore/Arch ...

  9. What is Babel?---JSX and React

    Babel is a JavaScript compiler Babel is a toolchain that is mainly used to convert ECMAScript 2015+ ...

  10. bzoj1935: [Shoi2007]Tree 园丁的烦恼lowbit 离散化

    链接 bzoj 最好不要去luogu,数据太水 思路 一个询问转化成四个矩阵,求起点\((0,0)到(x,y)\)的矩阵 离线处理,离散化掉y,x不用离散. 一行一行的求,每次处理完一行之后下一行的贡 ...