【最佳实践】使用Elasticsearch跨集群复制功能(CCR)

Get cross-cluster replication stats API

Get cross-cluster replication stats.

Request

GET /_ccr/stats

ConsolePHPC#PythonRubyGoJavaScriptCopy as curlView in Console 

Prerequisites

  • If the Elasticsearch security features are enabled, you must have monitor cluster privileges on the cluster that contains the follower index. For more information, see Security privileges.

Description

This API gets cross-cluster replication stats. This API will return all stats related to cross-cluster replication. In particular, this API returns stats about auto-following, and returns the same shard-level stats as in the get follower stats API.

Response body

auto_follow_stats

(object) An object representing stats for the auto-follow coordinator.Properties of auto_follow_statsnumber_of_failed_follow_indices(long) The number of indices that the auto-follow coordinator failed to automatically follow. The causes of recent failures are captured in the logs of the elected master node and in the auto_follow_stats.recent_auto_follow_errors field.number_of_failed_remote_cluster_state_requests(long) The number of times that the auto-follow coordinator failed to retrieve the cluster state from a remote cluster registered in a collection of auto-follow patterns.number_of_successful_follow_indices(long) The number of indices that the auto-follow coordinator successfully followed.recent_auto_follow_errors(array) An array of objects representing failures by the auto-follow coordinator.follow_stats(object) An object representing shard-level stats for follower indices; refer to the details of the response in the get follower stats API.

Examples

This example retrieves cross-cluster replication stats:

GET /_ccr/stats

ConsolePHPC#PythonRubyGoJavaScriptCopy as curlView in Console 

The API returns the following results:

{
  "auto_follow_stats" : {
    "number_of_failed_follow_indices" : 0,
    "number_of_failed_remote_cluster_state_requests" : 0,
    "number_of_successful_follow_indices" : 1,
    "recent_auto_follow_errors" : [],
    "auto_followed_clusters" : []
  },
  "follow_stats" : {
    "indices" : [
      {
        "index" : "follower_index",
        "shards" : [
          {
            "remote_cluster" : "remote_cluster",
            "leader_index" : "leader_index",
            "follower_index" : "follower_index",
            "shard_id" : 0,
            "leader_global_checkpoint" : 1024,
            "leader_max_seq_no" : 1536,
            "follower_global_checkpoint" : 768,
            "follower_max_seq_no" : 896,
            "last_requested_seq_no" : 897,
            "outstanding_read_requests" : 8,
            "outstanding_write_requests" : 2,
            "write_buffer_operation_count" : 64,
            "follower_mapping_version" : 4,
            "follower_settings_version" : 2,
            "follower_aliases_version" : 8,
            "total_read_time_millis" : 32768,
            "total_read_remote_exec_time_millis" : 16384,
            "successful_read_requests" : 32,
            "failed_read_requests" : 0,
            "operations_read" : 896,
            "bytes_read" : 32768,
            "total_write_time_millis" : 16384,
            "write_buffer_size_in_bytes" : 1536,
            "successful_write_requests" : 16,
            "failed_write_requests" : 0,
            "operations_written" : 832,
            "read_exceptions" : [ ],
            "time_since_last_read_millis" : 8
          }
        ]
      }
    ]
  }
}

2020-07-31 942简介: 当您需要将本地Elasticsearch集群中的索引数据迁移到一个远程集群中,或者将一个远程集群中的索引数据迁移到本地集群,可通过跨集群复制CCR(Cross Cluster Replication)功能实现。本文介绍具体的实现方法。https://developer.aliyun.com/article/769355

当您需要将本地Elasticsearch集群中的索引数据迁移到一个远程集群中,或者将一个远程集群中的索引数据迁移到本地集群,可通过跨集群复制CCR(Cross Cluster Replication)功能实现。本文介绍具体的实现方法。

背景信息


CCR是开源Elasticsearch在platinum版本中发布的一个商业特性。 只需要简单配置,即可使用CCR功能(目前仅支持单可用区6.7.0及以上版本的Elasticsearch实例)。CCR的应用场景如下:

  • 灾难恢复及高可用性对于分布在不同地域的Elasticsearch集群,您可以通过CCR进行数据备份。当其中一个集群发生故障时,您可以通过访问其他集群来获取故障集群的数据,保证数据不丢失。
  • 就近访问数据例如A集团下有多个子公司,各子公司所分布的地域不同。为了提高业务处理速度,可按照地理位置划分子公司要承担的业务,并通过CCR将业务数据分发给各地域中的Elasticsearch集群。子公司在处理业务时,可直接访问当前所在地域的集群。
  • 集中报告通过CCR,将多个数据量较小的集群中的数据复制到一个中央集群中,进行可视化分析与报告。

使用CCR功能,需要准备两种类型的集群。一个是远程集群,即提供源数据(Leader index)的集群;一个是本地集群,即订阅数据(Follower index)的集群。该功能为被动复制,即所有复制任务都是由本地集群执行。同时支持批量实时迁移数据,更多详情请参见Cross-cluster replication

本文以阿里云Elasticsearch为例,为您介绍跨集群复制功能(CCR)的使用方法。阿里云Elasticsearch兼容开源Elasticsearch的功能,以及Security、Machine Learning、Graph、APM等商业功能,致力于数据分析、数据搜索等场景服务。支持5.5.3、6.3.2、6.7.0、6.8.0和7.4.0等版本,并提供了商业插件X-Pack服务。在开源Elasticsearch的基础上提供企业级权限管控、安全监控告警、自动报表生成等功能。阿里云Elasticsearch为您提供1个月的免费试用活动,单击此处即可免费试用。

操作流程


  1. 准备工作

准备远程和本地集群,以及待迁移的索引。

  1. 步骤一:配置实例网络互通

连通远程和本地集群的网络。

  1. 步骤二:添加远程集群

在本地集群的Kibana控制台中,添加远程集群。

  1. 步骤三:配置跨集群复制

在本地集群的Kibana控制台中,配置待迁移和迁移后的索引。

  1. 步骤四:验证数据迁移结果

在远程集群中插入数据,在本地集群中,验证数据是否迁移成功。

准备工作


  1. 准备远程和本地Elasticsearch集群。{#cmd-ps8-i4x-dwf}

具体操作步骤请参见创建阿里云Elasticsearch实例。要求两个实例为相同版本(6.7.0及以上),可用区类型为单可用区,且在同一专有网络和虚拟交换机下。

  1. 参见登录Kibana控制台,在远程集群中创建待迁移的索引。 PUT myindex { "settings": { "index.soft_deletes.retention.operations": 1024, "index.soft_deletes.enabled": true } }

注意

对于7.0及以下版本的Elasticsearch实例,在创建索引时,需要开启soft_deletes属性,否则会报错。
如果您需要迁移已创建的索引,需要通过重建索引来开启soft_deletes属性。

  1. 关闭待迁移的索引的物理复制功能。

对于6.7.0版本的阿里云Elasticsearch实例,系统会默认为新建索引开启物理复制功能。使用CCR功能时,需要先关闭物理复制功能。

  1. 关闭索引。 POST myindex/_close
  2. 更新索引settings,关闭物理复制功能。 POST myindex/_settings { "index.replication.type" : null }
  3. 打开索引。{#cmd-c4f-nw3-dk6} POST myindex/_open

步骤一:配置实例网络互通


参见配置实例网络互通,在远程集群中添加需要进行网络互通的本地集群。最终配置如下。
在这里插入图片描述

步骤二:添加远程集群


  1. 登录本地集群的Kibana控制台。

具体操作步骤请参见登录Kibana控制台

  1. 在左侧导航栏,单击 Management 。
  2. 在 Elasticsearch 区域中,单击 Remote Clusters 。
  3. 单击 Add a remote cluster 。
  4. 在 Add remote cluster 页面中,输入远程集群信息。 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200730191817233.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ2Mzk2NTYz,size_16,color_FFFFFF,t_70)

Name :远程集群的名称,不可重复。
Seed nodes :需要配置为远程集群的主节点的IP地址:9300。远程集群的主节点的IP地址,可在远程集群的Kibana控制台中,使用GET /_cat/nodes?v命令获取。
在这里插入图片描述

 **注意** 由于CCR功能是Kibana通过数据节点之间的TCP端口(9300),访问数据节点IP的形式来进行网络互通,因此不支持HTTP端口(9200)访问。
  1. 单击 Save 。

保存后,系统会自动连接远程集群。连接成功后,显示 Connected 。
在这里插入图片描述

步骤三:配置跨集群复制


  1. 在本地集群Kibana控制台的 Management 页面,单击 Elasticsearch 区域中的 Cross Cluster Replication 。
  2. 单击 Create a follower index 。
  3. 在 Add follower index 页面,配置跨集群复制信息。
    在这里插入图片描述

| 参数 | 说明 |
|——————–|———————————————————————————————-|
Remote cluster | 选择您在步骤二:添加远程集群中添加的集群。 |
Leader index | 待迁移的索引。本文使用在准备工作中创建的 myindex 索引。 |
Follower index | 迁移数据生成的索引。索引名称不可重复。 |

  1. 单击 Create 。

创建成功后,索引的状态显示为 Active 。
在这里插入图片描述

步骤四:验证数据迁移结果


  1. 参见登录Kibana控制台,在远程集群中插入数据。 POST myindex/_doc/ { "name":"Jack", "age":40 }
  2. 在本地集群中,验证数据是否迁移成功。 GET myindex_follow/_search

迁移成功后,返回如下结果。
在这里插入图片描述

从以上结果可以看到,远程集群的Leader索引(myindex)中的数据,已通过CCR功能复制到了本地集群的Follower索引(myindex_follow)中。

  1. 在远程集群中,重新插入一条数据,随即在本地集群中进行查看,验证增量数据是否实时同步。 POST myindex/_doc/ { "name":"Pony", "age":50 }

数据插入后,立即在本地集群中进行查看,结果如下。
在这里插入图片描述

从以上结果可以看到,通过CCR可以实现增量数据的实时同步。
说明 您也可以通过CCR功能的API,进行跨集群复制相关操作,详情请参见Cross-cluster replication APIs

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注