文章目录
- ES集群分片数据的高可用
- 1. 集群设置索引节点
- 2. 集群新增文档数据
- 3.查看集群中文档数据分片节点
- 4. 让节点9201宕机,查看其分片变化
- 5. 让节点9201,查看分片变化
ES集群分片数据的高可用
集群中的索引主分片和副分片在不同的计算机上,如果某个主分片所在的节点宕机,则原有的某个副分片会提成为主分片继续对外进行服务,如果宕机的节点恢复了服务并加入集群中,该节点主分片会降为副分片,缺少的数据会从别的节点上提升为主分片的分片进行补充。
1. 集群设置索引节点
一个主分片分散到三个节点上,每个节点存在一个副本分片
请求地址:http://localhost:9203/shopping
请求方式:PUT
请求参数:
{"settings":{"number_of_shards":3,"number_of_replicas":1}
}
2. 集群新增文档数据
请求地址:http://localhost:9201/shopping/_doc/1
请求方式:POST
请求参数:
{"name":"甜瓜"
}
3.查看集群中文档数据分片节点
请求地址:http://localhost:9202/shopping/_search
请求方式:GET
请求参数:
{"explain":true,"query":{"match_all":{}}
}
返回结果:
{"took": 698,"timed_out": false,"_shards": {"total": 3,"successful": 3,"skipped": 0,"failed": 0},"hits": {"total": {"value": 5,"relation": "eq"},"max_score": 1.0,"hits": [{"_shard": "[shopping][0]","_node": "_U66vmMhTtCOjJZiBd4g-A","_index": "shopping","_type": "_doc","_id": "5","_score": 1.0,"_source": {"name": "葡萄"},"_explanation": {"value": 1.0,"description": "*:*","details": []}},{"_shard": "[shopping][1]","_node": "ax3r-sCLTHCzJFKGx5XwfA","_index": "shopping","_type": "_doc","_id": "2","_score": 1.0,"_source": {"name": "香蕉"},"_explanation": {"value": 1.0,"description": "*:*","details": []}},{"_shard": "[shopping][1]","_node": "ax3r-sCLTHCzJFKGx5XwfA","_index": "shopping","_type": "_doc","_id": "3","_score": 1.0,"_source": {"name": "苹果"},"_explanation": {"value": 1.0,"description": "*:*","details": []}},{"_shard": "[shopping][1]","_node": "ax3r-sCLTHCzJFKGx5XwfA","_index": "shopping","_type": "_doc","_id": "4","_score": 1.0,"_source": {"name": "桔子"},"_explanation": {"value": 1.0,"description": "*:*","details": []}},{"_shard": "[shopping][2]","_node": "7PtbggywTNmT1Z9AnFZzkg","_index": "shopping","_type": "_doc","_id": "1","_score": 1.0,"_source": {"name": "甜瓜"},"_explanation": {"value": 1.0,"description": "*:*","details": []}}]}
}
分片0:葡萄(id=5)
分片1:香蕉(id=2),苹果(id=3),桔子(id=4)
分片2:甜瓜(id=1)
4. 让节点9201宕机,查看其分片变化
发现会把9202节点的副分片1提升为主分片1,一会之后会在9202节点上添加副分片0,9203节点添加副分片1.
5. 让节点9201,查看分片变化