跨集群复制入门
使用跨集群复制,您可以将数据索引到主索引,而 OpenSearch 会将该数据复制到一个或多个只读的从属索引。对主索引的所有后续操作都会在从属索引上复制,例如创建、更新或删除文档。
先决条件
跨集群复制具有以下先决条件
- 主集群和从集群都必须安装复制插件。
-
如果您已在从集群的
opensearch.yml
中覆盖了node.roles
,请确保它也包含remote_cluster_client
角色node.roles: [<other_roles>, remote_cluster_client]
权限
确保安全插件在两个集群上都已启用或在两个集群上都已禁用。如果您禁用了安全插件,则可以跳过此部分。但是,我们强烈建议在生产场景中启用安全插件。
如果启用了安全插件,请确保将非管理员用户映射到适当的权限,以便他们可以执行复制操作。有关索引和集群级别权限的要求,请参阅跨集群复制权限。
此外,验证并在主集群上添加每个从集群节点的专有名称 (DN),以允许从从集群到主集群的连接。
首先,从每个从集群获取节点的 DN
curl -XGET -k -u 'admin:<custom-admin-password>' 'https://:9200/_opendistro/_security/api/ssl/certs?pretty'
{
"transport_certificates_list": [
{
"issuer_dn" : "CN=Test,OU=Server CA 1B,O=Test,C=US",
"subject_dn" : "CN=follower.test.com", # To be added under leader's nodes_dn configuration
"not_before" : "2021-11-12T00:00:00Z",
"not_after" : "2022-12-11T23:59:59Z"
}
]
}
然后验证它是否是 opensearch.yml
中主集群配置的一部分。否则,将其添加到以下设置下
plugins.security.nodes_dn:
- "CN=*.leader.com, OU=SSL, O=Test, L=Test, C=DE" # Already part of the configuration
- "CN=follower.test.com" # From the above response from follower
示例设置
要在同一网络上启动两个单节点集群,请将此示例文件另存为 docker-compose.yml
并运行 docker compose up
version: '3'
services:
replication-node1:
image: opensearchproject/opensearch:3.1.0
container_name: replication-node1
environment:
- cluster.name=leader-cluster
- discovery.type=single-node
- bootstrap.memory_lock=true
- "OPENSEARCH_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- opensearch-data2:/usr/share/opensearch/data
ports:
- 9201:9200
- 9700:9600 # required for Performance Analyzer
networks:
- opensearch-net
replication-node2:
image: opensearchproject/opensearch:3.1.0
container_name: replication-node2
environment:
- cluster.name=follower-cluster
- discovery.type=single-node
- bootstrap.memory_lock=true
- "OPENSEARCH_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- opensearch-data1:/usr/share/opensearch/data
ports:
- 9200:9200
- 9600:9600 # required for Performance Analyzer
networks:
- opensearch-net
volumes:
opensearch-data1:
opensearch-data2:
networks:
opensearch-net:
集群启动后,验证每个集群的名称
curl -XGET -u 'admin:<custom-admin-password>' -k 'https://:9201'
{
"cluster_name" : "leader-cluster",
...
}
curl -XGET -u 'admin:<custom-admin-password>' -k 'https://:9200'
{
"cluster_name" : "follower-cluster",
...
}
在此示例中,使用端口 9201 (replication-node1
) 作为主集群,端口 9200 (replication-node2
) 作为从集群。
要获取主集群的 IP 地址,请首先标识其容器 ID
docker ps
CONTAINER ID IMAGE PORTS NAMES
3b8cdc698be5 opensearchproject/opensearch:3.1.0 0.0.0.0:9200->9200/tcp, 0.0.0.0:9600->9600/tcp, 9300/tcp replication-node2
731f5e8b0f4b opensearchproject/opensearch:3.1.0 9300/tcp, 0.0.0.0:9201->9200/tcp, 0.0.0.0:9700->9600/tcp replication-node1
然后获取该容器的 IP 地址
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 731f5e8b0f4b
172.22.0.3
设置跨集群连接
跨集群复制遵循“拉取”模型,因此大多数更改发生在从集群上,而不是主集群上。
连接到远程集群的连接模式
连接模式包括嗅探模式和代理模式。
在嗅探模式下,从集群通过指定名称和来自主集群的种子节点列表来建立与主集群的远程连接。在连接设置期间,从集群从提供的种子节点之一检索主集群的状态。此模式要求可以从从集群访问主集群中种子节点的发布地址。嗅探模式是默认连接模式。
在从集群上,为每个种子节点添加 IP 地址(端口为 9300)。因为这是一个单节点集群,所以只有一个种子节点。为连接提供一个描述性名称,您将在启动复制的请求中使用该名称
curl -XPUT -k -H 'Content-Type: application/json' -u 'admin:<custom-admin-password>' 'https://:9200/_cluster/settings?pretty' -d '
{
"persistent": {
"cluster": {
"remote": {
"my-connection-alias": {
"seeds": ["172.22.0.3:9300"]
}
}
}
}
}'
在代理模式下,从集群通过指定名称和单个代理地址来建立与主集群的远程连接。在连接设置期间,会打开到提供的代理地址的可配置数量的套接字连接。代理的职责是将这些连接定向到主集群中的相应节点。与其他连接模式不同,代理模式不要求主集群中的节点具有可公开访问的发布地址
curl -XPUT -k -H 'Content-Type: application/json' -u 'admin:<custom-admin-password>' 'https://:9200/_cluster/settings?pretty' -d '
{
"persistent": {
"cluster": {
"remote": {
"my-connection-alias": {
"mode": "proxy"
"proxy_address": "172.22.0.3:9300"
}
}
}
}
}'
启动复制
要开始使用,请在主集群上创建一个名为 leader-01
的索引
curl -XPUT -k -H 'Content-Type: application/json' -u 'admin:<custom-admin-password>' 'https://:9201/leader-01?pretty'
然后从从集群启动复制。在请求正文中,提供要复制的连接名称和主索引,以及要使用的安全角色
curl -XPUT -k -H 'Content-Type: application/json' -u 'admin:<custom-admin-password>' 'https://:9200/_plugins/_replication/follower-01/_start?pretty' -d '
{
"leader_alias": "my-connection-alias",
"leader_index": "leader-01",
"use_roles":{
"leader_cluster_role": "all_access",
"follower_cluster_role": "all_access"
}
}'
如果安全插件被禁用,则省略 use_roles
参数。但是,如果启用,则必须指定 OpenSearch 将用于验证请求的主集群和从集群角色。此示例为简单起见使用 all_access
,但我们建议在每个集群上创建一个复制用户,并相应地映射它。
此命令在从集群上创建一个名为 follower-01
的相同只读索引,该索引会持续保持与主集群上的 leader-01
索引的更改同步。启动复制会从头开始创建一个从属索引——您不能将现有索引转换为从属索引。
确认复制
复制启动后,获取状态
curl -XGET -k -u 'admin:<custom-admin-password>' 'https://:9200/_plugins/_replication/follower-01/_status?pretty'
{
"status" : "SYNCING",
"reason" : "User initiated",
"leader_alias" : "my-connection-alias",
"leader_index" : "leader-01",
"follower_index" : "follower-01",
"syncing_details" : {
"leader_checkpoint" : -1,
"follower_checkpoint" : -1,
"seq_no" : 0
}
}
可能的状态有 SYNCING
、BOOTSTRAPPING
、PAUSED
和 REPLICATION NOT IN PROGRESS
。
主索引和从索引的检查点值最初是负数,反映了分片计数(一个分片为 -1,五个分片为 -5,依此类推)。这些值随着每次更改而递增,并说明从索引落后主索引多少更新。如果索引完全同步,则这些值是相同的。
要确认复制实际正在发生,请向主索引添加一个文档
curl -XPUT -k -H 'Content-Type: application/json' -u 'admin:<custom-admin-password>' 'https://:9201/leader-01/_doc/1?pretty' -d '{"The Shining": "Stephen King"}'
然后在从索引上验证复制的内容
curl -XGET -k -u 'admin:<custom-admin-password>' 'https://:9200/follower-01/_search?pretty'
{
...
"hits": [{
"_index": "follower-01",
"_id": "1",
"_score": 1.0,
"_source": {
"The Shining": "Stephen King"
}
}]
}
.replication-metadata-store
索引
.replication-metadata-store
索引是集群内复制相关元数据和自动关注规则的持久性数据存储。它存储从主集群复制到从集群的每个索引的复制元数据。
在第一个复制 API 触发后,.replication-metadata-store
索引将在从集群内创建。对复制作业或规则的任何更新或添加也会在索引中更新。这使插件能够维护集群中复制状态和规则的综合记录。
.replication-metdata-store
是一个隐藏索引。
暂停和恢复复制
如果您需要修复问题或减少主集群上的负载,可以暂时暂停索引复制
curl -XPOST -k -H 'Content-Type: application/json' -u 'admin:<custom-admin-password>' 'https://:9200/_plugins/_replication/follower-01/_pause?pretty' -d '{}'
要确认复制已暂停,请获取状态
curl -XGET -k -u 'admin:<custom-admin-password>' 'https://:9200/_plugins/_replication/follower-01/_status?pretty'
{
"status" : "PAUSED",
"reason" : "User initiated",
"leader_alias" : "my-connection-alias",
"leader_index" : "leader-01",
"follower_index" : "follower-01"
}
完成更改后,恢复复制
curl -XPOST -k -H 'Content-Type: application/json' -u 'admin:<custom-admin-password>' 'https://:9200/_plugins/_replication/follower-01/_resume?pretty' -d '{}'
当复制恢复时,从属索引会获取在复制暂停时对主索引所做的任何更改。
请注意,在暂停超过 12 小时后,您无法恢复复制。您必须停止复制,删除从属索引,然后重新启动主索引的复制。
停止复制
当您不再需要复制索引时,从从集群终止复制
curl -XPOST -k -H 'Content-Type: application/json' -u 'admin:<custom-admin-password>' 'https://:9200/_plugins/_replication/follower-01/_stop?pretty' -d '{}'
停止复制时,从属索引将取消关注主索引并成为您可以写入的标准索引。停止复制后,您无法重新启动复制。
获取状态以确认索引不再被复制
curl -XGET -k -u 'admin:<custom-admin-password>' 'https://:9200/_plugins/_replication/follower-01/_status?pretty'
{
"status" : "REPLICATION NOT IN PROGRESS"
}
您可以通过对主索引进行修改并确认它们没有显示在从属索引上来进一步确认复制已停止。