Elasticsearch学习之集群常见状况处理(干货)
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学习之集群常见状况处理(干货)的更多相关文章
- 原创 | 手摸手带您学会 Elasticsearch 单机、集群、插件安装(图文教程)
欢迎关注笔者的公众号: 小哈学Java, 每日推送 Java 领域干货文章,关注即免费无套路附送 100G 海量学习.面试资源哟!! 个人网站: https://www.exception.site/ ...
- 全文搜索引擎 Elasticsearch 入门:集群搭建
本文主要介绍什么是 ElasticSearch 以及为什么需要它,如何在本机安装部署 ElasticSearch 实例,同时会演示安装 ElasticSearch 插件,以及如何在本地部署多实例集群, ...
- hadoop 集群常见错误解决办法
hadoop 集群常见错误解决办法 hadoop 集群常见错误解决办法: (一)启动Hadoop集群时易出现的错误: 1. 错误现象:Java.NET.NoRouteToHostException ...
- Docker学习-Kubernetes - 集群部署
Docker学习 Docker学习-VMware Workstation 本地多台虚拟机互通,主机网络互通搭建 Docker学习-Docker搭建Consul集群 Docker学习-简单的私有Dock ...
- 使用Spring Data ElasticSearch+Jsoup操作集群数据存储
使用Spring Data ElasticSearch+Jsoup操作集群数据存储 1.使用Jsoup爬取京东商城的商品数据 1)获取商品名称.价格以及商品地址,并封装为一个Product对象,代码截 ...
- Elasticsearch高级之-集群搭建,数据分片
目录 Elasticsearch高级之-集群搭建,数据分片 一 广播方式 二 单播方式 三 选取主节点 四 什么是脑裂 五 错误识别 Elasticsearch高级之-集群搭建,数据分片 es使用两种 ...
- 1.ElasticSearch系列之集群部署
第一步:安装JDK JDK要求jdk1.8+,不安装也可以,ES自带JDK 第二步:系统配置 2.1 禁用交换区 sudo swapoff -a 2.2 开最大文件数的限制 编辑文件 /etc/sec ...
- Elasticsearch 6.x版本全文检索学习之集群调优建议
1.系统设置要到位,遵照官方建议设置所有的系统参数. https://www.elastic.co/guide/en/elasticsearch/reference/6.7/setup.html 部署 ...
- Elasticsearch 教程--分布式集群
集群 补充章节 正如前文提到的,这就是第个补充的章节,这里会介绍 Elasticsearch 如何在分布式环境中运行. 本章解释了常用术语,比如 集群 (cluster), 节点 (node) 以及 ...
随机推荐
- 在linux系统中如何通过shell脚本批量设置redis键值对
业务逻辑:批量设置redis中手机号的验证码为888888: 准备shell脚本如下:将18888888100~18888888110的手机号验证码设置为888888: #!/bin/bash ;i& ...
- JavaScript数组常用操作方法
ES5操作数组的方法 1.concat() concat() 方法用于连接两个或多个数组.该方法不会改变现有的数组,仅会返回被连接数组的一个副本. var arr1 = [1,2,3]; var ar ...
- js 正则表达式 贪婪与惰性
首先引入一个介绍比较详细的网站 http://www.jb51.net/article/31491.htm 接下来是本人的简介 其实贪婪和惰性很容易理解,从字面意思我们就可以知道,所谓的"贪 ...
- windows命令查看端口占用情况
打开cmd 查看端口占用情况:netstat -aon 查看PID对应的进程:tasklist /FI "PID eq 16948"
- Pychram中使用reduce()函数报错:Unresolved reference 'reduce'
python3不能直接使用reduce()函数,因为reduce() 函数已经被从全局名字空间里移除了,它现在被放置在fucntools 模块里,所以要使用reduce函数得先饮用fucntools ...
- vue cli 常见问题汇总
以下是本人在用vue cli 开发项目里遇到的最基本的问题及解决方案汇总.没啥很多技术性的东西,各位看个乐呵就行~ 1.vue-cli 创建的项目各文件夹的含义 注意:通过vue-cli 4 创建的项 ...
- PinPoint使用教程
选择该应用的展示边界 InBound:3 OutBound:3 基本概念 APM (Application Performance Management/应用性能管理)工具 为大规模分布式系统. 开发 ...
- 验证码破解 | Selenium模拟登陆微博
模拟登陆微博相对来说,并不难.验证码是常规的5个随机数字字母的组合,识别起来也比较容易.主要是用到许多Selenium中的知识,如定位标签.输入信息.点击等.如对Selenium的使用并不熟悉,请先移 ...
- [Android] Android studio gradle 插件的版本号和 gradle 的版本号 的对应关系
[Android] Android studio gradle 插件的版本号和 gradle 的版本号 的对应关系 本博客地址: wukong1688 本文原文地址:https://www.cnblo ...
- eclipse中的maven插件
导入一个maven项目,一直报错:org.codehaus.plexus.archiver.jar.Manifest.write(java.io.PrintWriter)的错误 Description ...