上一章节我们对ES的集群进行了搭建,有兴趣的朋友可以参考一下elasticSearch核心概念的介绍(十三):docker搭建ES集群 这里我们来介绍了ES集群索引的分片管理
ES集群索引分片管理介绍
- 分片(shard):因为ES是个分布式的搜索引擎,所以索引通常都会分解成不同部分,而这些分布在不同节点的数据就是分片,ES自动管理和组织分片,并在必要的时候对分片数据进行再平衡分配,所以用户基本上不用担心分片的处理细节。
- 副本(replica):ES默认为一个索引创建一个主分片,并分别为其创建一个副分片,也就是说每个索引都由一个主分片成本,而每个主分片都有相应的一个copy。
- ES7.x之后,如果不指定索引分片,默认会创建一个主分片和一个副分片,而7.x版本之前的比较,如6.x版本,默认是5个主分片
下面我们来看看分片如何使用?
创建索引(不指定分片数量)curl -X PUT "172.25.45.150:9200/nba" -H 'Content-Type:aplication/json' -d '
{
"mappings": {
"properties": {
"birthDay": {
"type": "date"
},
"birthDayStr": {
"type": "keyword"
},
"age": {
"type": "integer"
},
"code": {
"type": "text"
},
"country": {
"type": "text"
},
"countryEn": {
"type": "text"
},
"displayAffiliation": {
"type": "text"
},
"displayName": {
"type": "text"
},
"displayNameEn": {
"type": "text"
},
"draft": {
"type": "long"
},
"heightValue": {
"type": "float"
},
"jerseyNo": {
"type": "text"
},
"playYear": {
"type": "long"
},
"playerId": {
"type": "keyword"
},
"position": {
"type": "text"
},
"schoolType": {
"type": "text"
},
"teamCity": {
"type": "text"
},
"teamCityEn": {
"type": "text"
},
"teamConference": {
"type": "keyword"
},
"teamConferenceEn": {
"type": "keyword"
},
"teamName": {
"type": "keyword"
},
"teamNameEn": {
"type": "keyword"
},
"weight": {
"type": "text"
}
}
}
}
'
创建之后我们在获取索引来看看分片数量
curl -X GET "172.25.45.150:9200/nba"
返回结果如关注下面这部分
"settings": {
"index": {
"creation_date": "1646360464892",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "oUWzENVEQVW_hXZlCdfQhg",
"version": {
"created": "7040299"
},
"provided_name": "nba"
}
}
可以看到number_of_shards(主分片)和number_of_shards(副分片)的数量都是1。
将上述索引删除我们再来看看指定分片数量如何取创建
curl -X DELETE "172.25.45.150:9200/nba"
创建索引(指定分片数量)
- settings
"settings":{
"number_of_shards":3,
"number_of_replicas":1
},
- 创建索引
curl -X PUT "172.25.45.150:9200/nba" -H 'Content-Type:aplication/json' -d '
{
"settings":{
"number_of_shards":3,
"number_of_replicas":1
},
"mappings": {
"properties": {
"birthDay": {
"type": "date"
},
"birthDayStr": {
"type": "keyword"
},
"age": {
"type": "integer"
},
"code": {
"type": "text"
},
"country": {
"type": "text"
},
"countryEn": {
"type": "text"
},
"displayAffiliation": {
"type": "text"
},
"displayName": {
"type": "text"
},
"displayNameEn": {
"type": "text"
},
"draft": {
"type": "long"
},
"heightValue": {
"type": "float"
},
"jerseyNo": {
"type": "text"
},
"playYear": {
"type": "long"
},
"playerId": {
"type": "keyword"
},
"position": {
"type": "text"
},
"schoolType": {
"type": "text"
},
"teamCity": {
"type": "text"
},
"teamCityEn": {
"type": "text"
},
"teamConference": {
"type": "keyword"
},
"teamConferenceEn": {
"type": "keyword"
},
"teamName": {
"type": "keyword"
},
"teamNameEn": {
"type": "keyword"
},
"weight": {
"type": "text"
}
}
}
}
'
继续来看看索引的分片
curl -X GET "172.25.45.150:9200/nba"
"settings": {
"index": {
"creation_date": "1646361232608",
"number_of_shards": "3",
"number_of_replicas": "1",
"uuid": "m_z801WySBCVX-ujrGSv8g",
"version": {
"created": "7040299"
},
"provided_name": "nba"
}
}
可以看到现在我们创建的索引分片数量已经为我们设置的了
索引分片分配- 分片分配到哪个节点是由ES自动管理的,如果某个节点挂了,那分配又会重新分配到别的节点上。
- 在单机中,节点没有副分片,因为只有一个节点没必要生成分片,一个节点挂了,副分片也会挂掉,完全是单故障,没有存在的意义。
- 在集群中,同个分片它的主分片和副分片不会在同一个节点上,因为主分片和副分片在同个节点,节点挂了,副分片和主分片一样是挂了,不要把鸡蛋都放在同个篮子里。
- 可以手动移动分片,比如把某个分片移动从节点1移动到节点2.
- 创建索引时指定的主分片数以后是无法修改的,所以主分片数的数量要根据项目决定,如果真的要增加主分片只能重建索引了。副分片数以后是可以修改的。
按照上述我们所创建的分片,有3个主分片,然后每个主分片都1个副分片,那么我们就存在6个分片。
如果装了kibana的可以通过Monitoring查看分片分布的情况。
- Primary:主分片
- Replica:副分片
- Relocating:分配中
- Initializing:初始化
- Unassigned Primary:未分片的主分片
- Unassigned Replica:未分配的副分片
将分片2从节点1移动到节点3
curl -X PUT "172.25.45.150:9200/_cluster/reroute" -H 'Content-Type:aplication/json' -d '
{
"commands": [
{
"move": {
"index": "nba",
"shard": 2,
"from_node": "node-1",
"to_node": "node-3"
}
}
]
}
'
上面我们提到了主分片的数量在索引创建后是无法修改的,如果想要进行修改就得重新创建一下索引,那么我们可以修改副分片的数量的。
curl -X PUT "http://172.25.45.150:9200/nba/_settings" -H 'Content-Type:application/json' -d '
{
"number_of_replicas":2
}
'
查询所有
curl -X GET "http://172.25.45.150:9200/nba"
"settings": {
"index": {
"creation_date": "1646361232608",
"number_of_shards": "3",
"number_of_replicas": "2",
"uuid": "m_z801WySBCVX-ujrGSv8g",
"version": {
"created": "7040299"
},
"provided_name": "nba"
}
}
可以看到副分片的数量变成了2个,现在每个主分片都存在2个副分片,那么现在我们就存在了9个分片,我们来看一下分片的分布