首页 > 数据库 > 数据库开发技术 > Elasticsearch 主从同步之跨集群复制
跨集群复制(Cross-cluster replication,简称:CCR)指的是:索引数据从一个 Elasticsearch 集群复制到另一个 Elasticsearch 集群。
对于主集群的索引数据的任何修改都会直接复制同步到从索引集群。
Elasticsearch 6.7 版本。

图片来源:opster.com




图片来自:elastic官方文档
● 集群A:远端集群,remote cluster leader
Elasticsearch: 172.21.0.14:19203
kibana:172.21.0.14:5613
● 集群B:本地集群,local cluster follower
Elasticsearch: 172.21.0.14:19202
kibana:172.21.0.14:5612
index.soft_deletes.enabled:true
xpack.security.enabled: true
Changed password for user apm_system
PASSWORD apm_system = m5ob2a8OvoKuYpPPsiRd
Changed password for user kibana_system
PASSWORD kibana_system = xwdrhpVPSsbxxY1l0b50
Changed password for user kibana
PASSWORD kibana = xwdrhpVPSsbxxY1l0b50
Changed password for user logstash_system
PASSWORD logstash_system = 1zweZhAVEnqwh1flHBkz
Changed password for user beats_system
PASSWORD beats_system = 7Fo3bvmLISshjvHXTqAY
Changed password for user remote_monitoring_user
PASSWORD remote_monitoring_user = EvB4FkFs88gsCP073YGt
Changed password for user elastic
PASSWORD elastic = c7KmLqGTm6cyl2ABJPBY
否则会报错如下:
{
"error" : {
"root_cause" : [
{
"type" : "exception",
"reason" : "Security must be explicitly enabled when using a [trial] license. Enable security by setting [xpack.security.enabled] to [true] in the elasticsearch.yml file and restart the node."
}
],
"type" : "exception",
"reason" : "Security must be explicitly enabled when using a [trial] license. Enable security by setting [xpack.security.enabled] to [true] in the elasticsearch.yml file and restart the node."
},
"status" : 500
}
PUT /_cluster/settings
{
"persistent": {
"cluster": {
"remote": {
"leader": {
"seeds": [
"172.21.0.14:19303"
]
}
}
}
}
}
从集群监测一下remote配置是否成功。
GET /_remote/info
检测是否配置成功。
POST /_security/role/remote-replication
{
"cluster": [
"read_ccr"
],
"indices": [
{
"names": [
"kibana_sample_data_logs"
],
"privileges": [
"monitor",
"read"
]
}
]
}
POST /_security/role/remote-replication
{
"cluster": [
"manage_ccr"
],
"indices": [
{
"names": [
"kibana_sample_data_logs_follower"
],
"privileges": [
"monitor",
"read",
"write",
"manage_follow_index"
]
}
]
}

步骤2:配置 follower index。

Remote cluster, 从集群对leader 的设置。
Leader index,主集群的索引。
Follower index,从集群的索引名称,与 Leader index 是一一对应的关系,是从 Leader 索引复制过来的数据。
执行成功后截图如下:

检查是否成功:
GET /kibana_sample_data_logs_from_leader/_ccr/stats
以上,跨集群同步设置成功之后,可以进一步做很多验证。
比如:主集群 leader 索引删除两条数据,从集群查看结果。对比发现,从集群也会跟着变化,这说明了跨集群复制已生效。



更多权限设置,推荐阅读:https://www.elastic.co/guide/en/elasticsearch/reference/current/security-privileges.html
包含但不限于:检查复制进度、暂停和恢复复制、重新创建跟随者索引和终止复制。

GET /kibana_sample_data_logs_from_leader/_ccr/stats

POST kibana_sample_data_logs_from_leader/_ccr/pause_follow
POST kibana_sample_data_logs_from_leader/_ccr/resume_follow
{
}
分三步骤:
#暂停
POST /follower_index/_ccr/pause_follow
#关闭
POST /follower_index/_close?wait_for_active_shards=0
#重建
PUT /follower_index/_ccr/follow?wait_for_active_shards=1
{
"remote_cluster" : "remote_cluster",
"leader_index" : "leader_index"
}

POST kibana_sample_data_logs_from_leader/_ccr/unfollow
实战出真知,由于这部分是收费功能,可能会用的少。这块一直是新知盲点,实战一把,才知道究竟!
针对data stream 数据流的处理,跨集群也是支持的,限于篇幅原因,本文没有展开,更多内容推荐阅读官方文档。
耗时12小时+,希望对你有帮助!
1、https://www.elastic.co/cn/blog/follow-the-leader-an-introduction-to-cross-cluster-replication-in-elasticsearch
2、https://opendistro.github.io/for-elasticsearch/blog/releases/2021/02/announcing-ccr/
3、https://www.elastic.co/guide/en/elasticsearch/reference/current/xpack-ccr.html
4、https://www.elastic.co/cn/blog/bi-directional-replication-with-elasticsearch-cross-cluster-replication-ccr
5、https://opster.com/blogs/elasticsearch-cross-cluster-replication-overview/

更短时间更快习得更多干货!
已带领72位球友通过 Elastic 官方认证!
中国仅通过百余人

原文作者:铭毅天下Elasticsearch;如有侵权,请联系管理员删除。