一、问题描述
- 往ES写入数据时,提示如下错误
文章来源(Source):https://www.dqzboy.com :[FORBIDDEN/12/index read-only / allow delete (api)];]}]
二、问题原因
如果我们只有一台机器,部署运行了es,但是却在index的settings中设置了replica为1(默认值),那么这个replicaunassigned shards
因为分片不能分配到已经存在分片副本的同一节点.
而且你在查看原因的时候,其会显示:
the shard cannot b
原文链接:https://www.dqzboy.com e allocated to the same node on which a copy of the shard already exists
即分片不能分配到已经存在分片副本的同一节点!!!
]# curl -XGET 'http://127.0.0.1:9200/_cluster/health?pretty'
{
"cluster_name" : "elk",
"status" : "yellow",
"timed_out" : false,
"number_of_nodes" : 1,
"number_of_data_nodes" : 1,
"active_primary_shards" : 600,
"active_shards" : 600,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 433, #未分配的分片数量,正常为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" : 58.08325266214908
}
- 所以可以看出问题还是出在了ES索引本身,应该是之前节点磁盘满过,然后索引状态无法自动切换过来
三、排查过程
(1)查看elasticsearch健康状态
~]# curl -XGET 'http://127.0.0.1:9200/_cluster/health?pretty'

(2)精确定位unassigned shard位置
~]# curl -XGET "http://127.0.0.1:9200/_cat/shards?h=index,shard,prirep,state,unassigned.reason" | grep UNASSIGNED

(3)通过以下语句查文章来源(Source):浅时光博客 看具体原因
- 可以看到下图中给出的具体原因是复制分片不能跟主分片在同一节点上
~]# curl -XGET "http://127.0.0.1:9200/_cluster/allocation/explain?pretty"

四、解决方法
方法1:切换索引状态
如果之前Elasticsearch 磁盘满过,处于只读状态,或者网络波动问题,处于只读状态的索引,只能被查询或者删除
curl -XPUT -H "Content-Type: application/json" http://127.0.0.1:9200/_all/_settings -d '{"index.blocks.read_only_allow_delete": null}'
方法2:直接删除索引
直接删除索引。注意:删除的前提是该索引已经是没在使用的,没有价值的索引
方法3:更改索引replica为0
(1)对正在使用的索引设置index的replica为0
~]# curl -H 'Content-Type: application/json' -s -XPUT "http://127.0.0.1:9200/xxxxx_log_2021_1/_settings" -d '{"number_of_replicas" : 0}'
(2)当数量多时,通过脚本循序处理
~]# vim unassigned_replicas.sh
#!/bin/bash
for index in $(curl -s 'http://127.0.0.1:9200/_cat/shards' | grep UNASSIGNED | awk '{print $1}' | sort | uniq); do
echo ${index}
curl -H 'Content-Type: application/json' -s -XPUT "http://127.0.0.1:9200/${index}/_settings" -d '{"number_of_replicas" : 0}'
done
#授予执行权限,并执行
~]# chmod +x unassigned_replicas.sh
~]# ./unassigned_replicas.sh
(3)再次检查集群状态跟unassigned_shards
~]# curl -XGET 'http://127.0.0.1:9200/_cluster/health?pretty'

以上就是本次文章所有知识点,如果文章对你有帮助就动动你那发财的小手,点个赞留个言,给本站加点热度。谢谢支持!
必须 注册 为本站用户, 登录 后才可以发表评论!