目录
水平扩展的痛点:
<1>单集群 – 当水平扩展时,节点数不能无限增加
- 当集群的 meta 信息(节点,索引,集群状态)过多,会导致更新压力变大,单 个 Active Master 会成为性能瓶颈,导致整个集群无法正常工作
<2>早期版本,通过 Tribe Node 可以实现多集群访问的需求,但是还存在一定的问题
- Tribe Node 会以 Client Node 的方式加入每个集群。 集群中 Master 节点的任 务变更需要 Tribe Node 的回应才能继续
- Tribe Node 不保存 Cluster State 信息,一旦重启,初始化很慢
- 当多个集群存在索引重名的情况时,只能设置一种 Prefer 规则
跨集群搜索 - Cross Cluster Search
- 早期 Tribe Node 的方案存在一定的问题,现已被 Deprecated
- Elasticsearch 5.3 引入了跨集群搜索的功能(Cross Cluster Search),推荐使用
- 允许任何节点扮演 federated 节点,以轻量的方式,将搜索请求进行代理
- 不需要以 Client Node 的形式加⼊入其他集群
配置及查询
<1>启动三个集群
bin/elasticsearch -E node.name=cluster0node -E cluster.name=cluster0 -E path.data=cluster0_data -E discovery.type=single-node -E http.port=9200 -E transport.port=9300
bin/elasticsearch -E node.name=cluster1node -E cluster.name=cluster1 -E path.data=cluster1_data -E discovery.type=single-node -E http.port=9201 -E transport.port=9301
bin/elasticsearch -E node.name=cluster2node -E cluster.name=cluster2 -E path.data=cluster2_data -E discovery.type=single-node -E http.port=9202 -E transport.port=9302
<2>配置
- 要执行跨集群搜索,必须配置至少一个远程集群
- 如下实例,添加了三个远程集群(在每个集群上设置动态)
PUT _cluster/settings
{
"persistent": {
"cluster": {
"remote":{
"cluster_one":{
"seeds":[
"127.0.0.1:9300"
]
},
"cluster_two":{
"seeds":[
"127.0.0.1:9301"
]
},
"cluster_three":{
"seeds":[
"127.0.0.1:9302"
]
}
}
}
}
}
#cURL
curl -XPUT "http://localhost:9200/_cluster/settings" -H 'Content-Type: application/json' -d'
{"persistent":{"cluster":{"remote":{"cluster0":{"seeds":["127.0.0.1:9300"],"transport.ping_schedule":"30s"},"cluster1":{"seeds":["127.0.0.1:9301"],"transport.compress":true,"skip_unavailable":true},"cluster2":{"seeds":["127.0.0.1:9302"]}}}}}'
curl -XPUT "http://localhost:9201/_cluster/settings" -H 'Content-Type: application/json' -d'
{"persistent":{"cluster":{"remote":{"cluster0":{"seeds":["127.0.0.1:9300"],"transport.ping_schedule":"30s"},"cluster1":{"seeds":["127.0.0.1:9301"],"transport.compress":true,"skip_unavailable":true},"cluster2":{"seeds":["127.0.0.1:9302"]}}}}}'
curl -XPUT "http://localhost:9202/_cluster/settings" -H 'Content-Type: application/json' -d'
{"persistent":{"cluster":{"remote":{"cluster0":{"seeds":["127.0.0.1:9300"],"transport.ping_schedule":"30s"},"cluster1":{"seeds":["127.0.0.1:9301"],"transport.compress":true,"skip_unavailable":true},"cluster2":{"seeds":["127.0.0.1:9302"]}}}}}'
<3>创建测试数据
curl -XPOST "http://localhost:9200/users/_doc" -H 'Content-Type: application/json' -d'
{"name":"user1","age":10}'
curl -XPOST "http://localhost:9201/users/_doc" -H 'Content-Type: application/json' -d'
{"name":"user2","age":20}'
curl -XPOST "http://localhost:9202/users/_doc" -H 'Content-Type: application/json' -d'
{"name":"user3","age":30}'
<4>查询
GET /cluster0:users,cluster1:users,cluster2:users/_search
{
"query": {
"range": {
"age": {
"gte": 0,
"lte": 40
}
}
}
}
<5>结果
跳过不可用集群
默认情况下,如果请求中的任何集群不可用,跨集群搜索将返回一个错误
跳过不可用集群,请将skip_unavailable集群设置设置为true