本文接上一篇《centos7.4为Elasticsearch6.5.0安装elasticsearch-head并使其以内置服务器方式运行》。安装完elasticsearch-head后,ES里面的索引数据什么就能通过web界面可视化显示。默认ES里面并没有任何数据,集群状态也为green。但是当我们尝试创建一个索引并向其中插入一条数据的时候,发现索引及集群的状态就变为yellow了,并且es-head上面显示有分片Unassigned。这里我们来模拟一下这个过程,并记录下怎么来解决这个问题。

一、测试还原故障现象

1.1、初始情况下es-head没有任何数据,并且集群状态为green

1.1.1 通过elasticsearch-head查看集群状态

es索引重命名 es索引unassigned_数据

1.1.2 通过ES Restful API接口查看集群状态

[root@imzcy ~]# curl -X GET 'http://localhost:9200/_cluster/health?pretty'


1.2、我们尝试使用ES Restful API接口插入一条数据(不存在的索引默认会自动创建)

//命令:
curl -X PUT "http://localhost:9200/zcy-20181204/_doc/2?pretty" -H 'Content-Type: application/json' -d '
{
"user" : "zhangsan",
"age" : "19",
"post_date" : "2018-12-04T17:41:33",
"message" : "www.imzcy.cn"
}
'
//执行结果:
[root@imzcy ~]# curl -X PUT "http://localhost:9200/zcy-20181204/_doc/2?pretty" -H 'Content-Type: application/json' -d '
> {
> "user" : "zhangsan",
> "age" : "19",
> "post_date" : "2018-12-04T17:41:33",
> "message" : "www.imzcy.cn"
> }
> '
{
"_index" : "zcy-20181204",
"_type" : "_doc",
"_id" : "2",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
[root@imzcy ~]#


//命令:
curl-XPUT"http://localhost:9200/zcy-20181204/_doc/2?pretty"-H'Content-Type: application/json'-d'
{
"user" : "zhangsan",
"age" : "19",
"post_date" : "2018-12-04T17:41:33",
"message" : "www.imzcy.cn"
}
'
//执行结果:
[root@imzcy~]# curl -X PUT "http://localhost:9200/zcy-20181204/_doc/2?pretty" -H 'Content-Type: application/json' -d '
>{
>"user":"zhangsan",
>"age":"19",
>"post_date":"2018-12-04T17:41:33",
>"message":"www.imzcy.cn"
>}
>'
{
"_index":"zcy-20181204",
"_type":"_doc",
"_id":"2",
"_version":1,
"result":"created",
"_shards":{
"total":2,
"successful":1,
"failed":0
},
"_seq_no":0,
"_primary_term":1
}
[root@imzcy~]#

1.3、再次查看集群和索引的状态,发现已经变为yellow并且提示Unassigned

1.3.1 通过elasticsearch-head查看集群状态

es索引重命名 es索引unassigned_数据_02

1.3.2 通过Restful API接口查看集群及刚才创建的索引状态

查看集群状态

[root@imzcy ~]# curl -X GET 'http://localhost:9200/_cluster/health?pretty'
{
"cluster_name" : "imzcy",
"status" : "yellow",
"timed_out" : false,
"number_of_nodes" : 1,
"number_of_data_nodes" : 1,
"active_primary_shards" : 5,
"active_shards" : 5,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 5,
"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.0
}
[root@imzcy ~]#


[root@imzcy~]# curl -X GET 'http://localhost:9200/_cluster/health?pretty'
{
"cluster_name":"imzcy",
"status":"yellow",
"timed_out":false,
"number_of_nodes":1,
"number_of_data_nodes":1,
"active_primary_shards":5,
"active_shards":5,
"relocating_shards":0,
"initializing_shards":0,
"unassigned_shards":5,
"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.0
}
[root@imzcy~]#

查看当前节点所有索引

[root@imzcy ~]# curl -X GET 'http://localhost:9200/_cat/indices'
yellow open zcy-20181204 s27I07VGSxe9RV6blohEfA 5 1 1 0 5.9kb 5.9kb
[root@imzcy ~]#


二、分析问题原因及如何解决

之所以状态会变为yellow,我这里大概说明下(可能不能很精确):因为我这里测试是单节点安装的ES,但是ES 6.5默认配置中:shard分片数量为5,replicas副本数量为1;这就要求我必须再有一个节点,才能存放副本的数据,不然就会提示对应索引分片数据Unassigned未分配并且集群状态转为yellow。所以建议如果测试情况下单实例安装ES的时候:分片数量设置为1,副本数量设置为0即可。

解决方法:

在老版本的elasticsearch中,还可以通过修改elasticsearch.yml配置文件,通过index.number_of_shards和index.number_of_replices配置项设置好默认分片和副本数量来解决这个问题。但是新版本的elasticsearch中这两个配置项已经取消了。

目前对于新版本的elasticsearch我们有3两种方法来解决:

1. 创建索引的时候指定分片和副本数量

2. 如果创建索引的时候没有指定,也可以通过ES Restful API来修改指定单个索引或者节点所有索引的副本数量(分片在索引创建好后不能修改)

3. 创建ES的模板(templates),模板中指定默认分片和副本的数量以及使用通配符指定这个模板应用的索引名称,例如test-*表示所有以test开头的索引都会应用这个模板的配置(后期使用logstash的时候,因为会自动创建新的索引,就必须要配置模板了)。

本篇主要介绍下,如何在创建索引时指定该分片及副本数量 以及如果后期修改单个索引或者节点所有索引的副本数量。关于模板的配置将在以后文章中单独介绍。

2.1、创建索引时指定分片及副本数量

//命令:
curl -X PUT "localhost:9200/zcy-20181205?pretty" -H 'Content-Type: application/json' -d'
{
"settings" : {
"number_of_shards" : 1,
"number_of_replicas" : 0
}
}
'
//执行结果:
[root@imzcy ~]# curl -X PUT "localhost:9200/zcy-20181205?pretty" -H 'Content-Type: application/json' -d'
> {
> "settings" : {
> "number_of_shards" : 1,
> "number_of_replicas" : 0
> }
> }
> '
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "zcy-20181205"
}
[root@imzcy ~]#
//命令:
curl-XPUT"localhost:9200/zcy-20181205?pretty"-H'Content-Type: application/json'-d'
{
"settings" : {
"number_of_shards" : 1,
"number_of_replicas" : 0
}
}
'
//执行结果:
[root@imzcy~]# curl -X PUT "localhost:9200/zcy-20181205?pretty" -H 'Content-Type: application/json' -d'
>{
>"settings":{
>"number_of_shards":1,
>"number_of_replicas":0
>}
>}
>'
{
"acknowledged":true,
"shards_acknowledged":true,
"index":"zcy-20181205"
}
[root@imzcy~]#

我们查看索引状态:

[root@imzcy ~]# curl -X GET 'http://localhost:9200/_cat/indices'
green open zcy-20181205 1aGFJDLBQXSuTI4k6WSYAA 1 0 0 0 261b 261b
yellow open zcy-20181204 s27I07VGSxe9RV6blohEfA 5 1 1 0 5.9kb 5.9kb
[root@imzcy ~]#


因为zcy-20181204那个索引状态还是yellow,所以整个集群的状态也还是yellow。不过我们可以看到刚创建的zcy-20181205那个索引分片和副本数量已经设置成功了。

es索引重命名 es索引unassigned_elasticsearch_03

2.2、修改已经创建好的索引的副本数量

//命令1(修改单个索引):
curl -X PUT "http://localhost:9200/zcy-20181204/_settings?pretty" -H 'Content-Type: application/json' -d '
{
"index" : {
"number_of_replicas" : 0
}
}
'
//命令2(修改节点中所有索引):
curl -X PUT "http://localhost:9200/_settings?pretty" -H 'Content-Type: application/json' -d '
{
"index": {
"number_of_replicas":"0"
}
}
'
//执行结果:
[root@imzcy ~]# curl -X PUT "http://192.168.43.213:9200/zcy-20181204/_settings?pretty" -H 'Content-Type: application/json' -d '
> {
> "index" : {
> "number_of_replicas" : 0
> }
> }
> '
{
"acknowledged" : true
}
[root@imzcy ~]#


我们查看索引状态:

[root@imzcy ~]# curl -X GET 'http://localhost:9200/_cat/indices'
green open zcy-20181205 1aGFJDLBQXSuTI4k6WSYAA 1 0 0 0 261b 261b
green open zcy-20181204 s27I07VGSxe9RV6blohEfA 5 0 1 0 5.9kb 5.9kb
[root@imzcy ~]#


在查看集群状态,已经正常:

es索引重命名 es索引unassigned_数据_04

//创建索引
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html
//插入数据
https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html