es版本:6.6.1
背景:部署服务器(都是windows)做迁移,es只是单机部署,所以就直接复制原来的data文件夹到新服务器的纯净es里了。
但是es启动后有报错Cluster health status changed from [RED] to [YELLOW] (reason: [shards started [[information][0]] ...])
,查看es的健康状态
http://192.168.1.33:9200/_cat/health?v
status是黄色。
http://192.168.1.33:9200/_cat/shards
可以看到某些索引对应的分片是UNASSIGNED(未分片)状态。经多方百度,得到解决方案:
修改索引settings的number_of_replicas
为0即可。
原因:分片太多,节点不够。单机es,1个节点但分片的副本数超过1个了,每个主分片的副本数少于群集中的节点数,所以要调整为0。
具体原因可参考:https://www.datadoghq.com/blog/elasticsearch-unassigned-shards/#reason-2-too-many-shards-not-enough-nodes
命令调整过程:
百度出来的命令如下
curl -XPUT http://192.168.1.33:9200/_settings?pretty -d '{ "index": { "number_of_replicas": 0 } }' -H "Content-Type: application/json"
但是,emm 这个windows服务器上没有curl命令,于是去https://curl.haxx.se/download.html#Win64下载了后,直接把curl.exe放到了C:\Windows\System32
里,然后重开cmd即可。
但是执行命令的时候一直报错
com.fasterxml.jackson.core.JsonParseException: Unexpected character (''' (code 39)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@34efbd73; line: 1, column: 2]
at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1702) ~[jackson-core-2.8.11.jar:2.8.11]
…………
经查证,在标准json中,要求键和值都要用双引号("")包括的,所以修改原来的命令为:curl -XPUT http://192.168.1.33:9200/_settings?pretty -d "{ "index": { "number_of_replicas": 0 } }" -H "Content-Type: application/json"
但是这时候又报别的错了(报错不可怕,不报错又不行才可怕www)
com.fasterxml.jackson.core.JsonParseException: Unexpected character ('c' (code 99)): was expecting double-quote to start field name
at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@169c7101; line: 1, column: 4]
at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1702) ~[jackson-core-2.8.11.jar:2.8.11]
…………
又查了下,然后才查到windows下curl有可能不支持单引号,如果有报错,还请改成双引号,内部使用转义字符转义。
然后就又调整了次命令,这次最终运行成功了。
curl -XPUT http://192.168.1.33:9200/_settings?pretty -d "{ \"index\": { \"number_of_replicas\": 0 } }" -H "Content-Type: application/json"
然后再查看es健康状态和索引状态,都变成了green
另外的坑
一般执行上面的语句就完事儿了。但是执行后会报错:
{"error":{"root_cause":[{"type":"cluster_block_exception","reason":"blocked by:
[FORBIDDEN/12/index read-only / allow delete (api)];"}],"type":"cluster_block_ex
ception","reason":"blocked by: [FORBIDDEN/12/index read-only / allow delete (api
)];"},"status":403}
最终解决方法是,先执行如下语句,修改单个索引只读状态,然后再修改该索引的分片副本集。
curl -XPUT 192.168.1.33:9200/address/_settings -d "{\"index.blocks.read_only_allow_delete\": null}" -H "Content-Type: application/json"
#执行成功后,继续执行
curl -XPUT 192.168.1.33:9200/address/_settings -d "{\"number_of_replicas\": 0}" -H "Content-Type: application/json"
此处具体可参考官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/disk-allocator.html