Elasticsearch(CCR)主从同步之跨集群复制

注: 部分概念介绍来源于网络

1.跨集群复制Cross-cluster replication (CCR)
跨集群复制最早发布版本Elasticsearch 6.7 版本
跨集群复制 (CCR) 功能支持将特定索引从一个 ElasticSearch 集群复制到一个或多个 ElasticSearch 集群。
复制索引是只读的
索引可以由一个或多个 Elasticsearch 集群进行复制。每个复制索引的集群都维护一份索引的只读副本。能够接受写操作的主动索引称为领导者。索引的被动只读副本称为追随者。并不存在选举新领导者的概念,因此,当领导者索引不可用(如集群/数据中心中断)时,应用程序管理员或集群管理员必须为写入显式选择另一个索引,而这个索引很可能位于另一个集群中。
CCR 是一项白金级功能,可通过 30 天试用许可证进行体验
试用许可证:curl -X POST "http://localhost:9200/_license/start_trial?acknowledge=true"

2.定义远程集群
elasticsearch-2集群上:
PUT /_cluster/settings
{
  "persistent" : {
    "cluster" : {
      "remote" : {
        "elasticsearch-1" : {
          "seeds" : [
            "127.0.0.1:9300"
          ]
        }
      }
    }
  }
}

3.创建要复制的索引
elasticsearch-1集群上:
PUT /elasticsearch-1-index
{
  "settings" : {
    "index" : {
      "number_of_shards" :1,
      "number_of_replicas" :0,
      "soft_deletes" : {
        "enabled" : true      
      }
    }
  }
}
soft_deletes设置,要使索引用作 CCR 的 Leader 索引,需要使用软删除:
soft_deletes:无论何时删除或更新现有文档,都会发生软删除。通过将这些软删除一直保留到可配置的限度,可以将操作历史记录保留在领导者分片上,并在重新处理操作历史记录时供追随者分片任务使用。
当追随者分片从领导者复制操作时,它会在领导者分片上留下标记,这样领导者就知道追随者在历史记录中的所在位置。这些标记下面的软删除操作可以逐渐消失。基于这些标记,领导者分片将会保留这些操作,直到分片历史记录保留租用期结束(默认为 12 个小时)。此期间确定了追随者在有可能远远落后并需要从领导者处重新引导之前可以离线的时间。
如果这个索引之前没有开启过这个参数,需要重新创建索引才行。

4.启动复制
elasticsearch-2集群上:
PUT /elasticsearch-1-index/_ccr/follow
{
  "remote_cluster" : "elasticsearch-1",
  "leader_index" : "elasticsearch-1-index"
}
elasticsearch-1-index这是elasticsearch-2集群中复制索引的名称。我们将从以前定义的elasticsearch-1集群中进行复制,并且要复制的索引名称在elasticsearch-1集群上称为elasticsearch-1-index。
请务必注意,复制索引是只读的,不能接受写操作。
两个集群索引名可以相同也可以不同。

5.启动索引模式的复制
CCR API 还包含定义自动跟随模式(即应复制哪些索引模式)的方法。
PUT /_ccr/auto_follow/beats
{
  "remote_cluster" : "elasticsearch-1",
  "leader_index_patterns" :
  [
    "elasticsearch-2-*",
    "index-*"
  ],
  "follow_index_pattern" : "{{leader_index}}-copy"
}
上面的 API 调用样例将复制开头为“elasticsearch-2”或“index”的索引。

6.测试复制设置
插入一个测试文档,验证它是否已被复制。
elasticsearch-1集群上:
POST /elasticsearch-1-index/_doc
{
  "name" :"My cool new product"
}
elasticsearch-2集群上:
GET /elasticsearch-1-index/_search
   "_source" : {
      "name" :"My cool new product"
   }

7.跨数据中心管理
我们下面介绍一下用于 CCR 的一些管理 API、可调设置,并概述在 Elasticsearch 中将复制索引转换为普通索引的方法。
用于复制的管理 API
在 Elasticsearch 中有许多有用的用于 CCR 的管理 API。它们可能有助于调试复制、修改复制设置或收集详细的诊断信息。

# Return all statistics related to CCR
GET /_ccr/stats
# Pause replication for a given index
暂停给索引的复制
POST //_ccr/pause_follow
# Resume replication, in most cases after it has been paused
暂停复制后再恢复复制
POST //_ccr/resume_follow
# Unfollow an index (stopping replication for the destination index), which first requires replication to be paused 
取消一个索引(停止目标索引的复制),这首先要求以下索引的复制被暂停
POST //_ccr/unfollow
# Statistics for a following index
以下索引的统计信息
GET //_ccr/stats
# Remove an auto-follow pattern
删除自动跟随模式
DELETE /_ccr/auto_follow/
# View all auto
查看所有
Read More

8.将追随者索引转换为普通索引
elasticsearch-2上复制elasticsearch-1-index索引是只读的,它不能接受任何写操作。如果希望将elasticsearch-1-index索引转换为 Elasticsearch 中的普通索引(能够接受写操作),我们可以执行下列命令。请记住,原始索引(elasticsearch-1-index)上的写操作可以继续,在将elasticsearch-1-index索引转换为普通 Elasticsearch 索引之前,可能需要先将写操作限定在elasticsearch-1-index索引上。
# Pause replication
POST /elasticsearch-2-index/_ccr/pause_follow
# Close the index
POST /elasticsearch-2-index/_close
# Unfollow
POST /elasticsearch-2-index/_ccr/unfollow
# Open the index
POST /elasticsearch-2-index/_open


9.CCR中leader和follower的身份可以互换么?
不可以。
ES CCR不支持在已存在的索引上建follow,只能新开一个follow。
我分析原因是这样的:
A->B这个方向的主从同步,由于是异步的,会存在一定的延时。CCR是基于seq顺序复制的,可能会出现B切为主时,A中有少部分数据没有复制到B。
B升为主后,B从复制断开的seq继续生成seq。
这时,如果把A配为B的从,就会有一部分seq冲突。ES底层机制暂时是解决不了这个问题的。

10.从集群监测一下remote配置是否成功
GET /_remote/info

11. 前置配置:xpack 设置true
因为需要配置角色、权限等,Elasitcsearch 设置了xpack,就意味着 kibana 端需要设置账号、密码。
在 elasticsearch.yml 文件中添加如下配置。
xpack.security.enabled: true

12.为跨集群复制配置权限
跨集群复制用户在远程集群和本地集群上需要不同的集群和索引权限。
使用以下请求在本地和远程集群上创建单独的角色,然后创建具有所需角色的用户。
remote 集群配置权限
前置条件:设置 xpack 为 true,kibana 端配置账号和密码。
POST /_security/role/remote-replication
{
  "cluster": [
    "read_ccr"
  ],
  "indices": [
    {
      "names": [
        "kibana_sample_data_logs"
      ],
      "privileges": [
        "monitor",
        "read"
      ]
    }
  ]
}
local 集群配置权限
在本地集群上创建从索引。
POST /_security/role/remote-replication
{
  "cluster": [
    "manage_ccr"
  ],
  "indices": [
    {
      "names": [
        "kibana_sample_data_logs_follower"
      ],
      "privileges": [
        "monitor",
        "read",
        "write",
        "manage_follow_index"
      ]
    }
  ]
}

13.Elasticsearch的CCR过程及其参数解读
过程解析:
复制过程主要分为两个阶段(对应ES写入的两个阶段,复制落地到磁盘的segment和复制写入缓存区还未落地到磁盘的数据)
Step1--复制远程leader集群的segment到 本地 follower集群
Step2--复制远程leader集群的 operator records (存在内存缓冲区和translog )到 本地follower集群。

Step1的过程(复制segment到从集群)
通过remote recovery的功能进行复制,复制远程集群的segment到本地集群,是一个网络密集型的过程。可以通过以下参数控制复制过程对网络的压力。
ccr.indices.recovery.max_bytes_per_sec :限制每个节点上的总入站和总出站的远程流量(速度)。(默认值是40mb),当主集群和从集群都设置这个值后,先达到限制值的先其作用。即如果主集群设置20mb,从集群设置60mb,那么主集群也只会想从集群发送20mb。这个参数常用
ccr.indices.recovery.max_concurrent_file_chunks:复制文件的并行度,即可以同时复制接个文件,默认值是5,最大值为10.
ccr.indices.recovery.chunk_size:复制文件一次请求的最大限制,默认值是1mb
ccr.indices.recovery.recovery_activity_timeout:leader等待follower恢复请求的时间,默认值是60s
ccr.indices.recovery.internal_action_timeout: 在远程恢复过程中单个网络请求的超时值,默认值是60s

Step2的执行过程(复制缓存区数据到从集群)
max_read_request_operation_count : 每次request读取的最大operate records的条数。默认值是5120
max_read_request_size : 每次request读取的最大operate records的size。默认值是32mb
max_outstanding_read_requests : 最多可以有多少个request同时读取,默认值是12
read_poll_timeout : 等待新的操作记录的超时时间,默认值是1min
max_retry_delay : 如果request失败了,等待重试的时间,默认值是"500ms",
max_write_buffer_count" : writer buffer的最大记录数,默认值是2147483647,
max_write_buffer_size" :  writer buffer的最大size,默认值是"512mb",
max_write_request_operation_count : 一次bulk最大写入条数,默认值是5120,
max_write_request_size" : 一次buld最大写入size,默认值是9223372036854775807b
max_outstanding_write_requests" : 最多可以有多少个bulk writer , 默认值是9
The follower shard task updates some statistics and immediately sends another read request to the leader shard.意味着,每次请求返回的条件是3个满足一个就返回(读取最大size,读取最大的条数records或者读取等待的时间timeout).
 

你可能感兴趣的:(ElasticSearch,elasticsearch)