在多个地域运维多套云消息队列 Confluent 版集群时,保持 Topic、Consumer Group 位点和访问控制列表(ACL)的同步通常需要人工操作和自定义工具。Cluster Linking 通过连接两个集群,将数据从 Source 集群实时复制到 Destination 集群,消除了这些额外开销。
Cluster Linking 通过两个抽象实现这一能力:
集群链接(cluster link):在 Destination 集群上发起,建立与 Source 集群的连接。
Mirror Topic:位于 Destination 集群上,从其源 Topic 复制数据。
除 Topic 数据外,Cluster Linking 还会同步 Consumer Group 位点和 ACL,因此适用于跨地域复制、容灾和集群迁移等场景。
本文介绍完整的操作流程:创建配置文件、准备测试数据、配置过滤器并创建集群链接、验证迁移。所有步骤均使用 Confluent Platform 命令行工具(CLI)。
前提条件
开始之前,请确认已具备以下条件:
已准备好数据源(Source)集群和数据目标(Destination)集群。
已准备好用于连接 Source 集群与 Destination 集群的机器。本文以 ECS 为例,实例的创建和使用,请参见创建与管理ECS实例。
已安装 Confluent Platform 7.0.0 及以上版本的客户端,安装方法请参见Confluent。
已安装 Java 8 或 11,安装方法请参见安装 JDK。
创建配置文件
登录 ECS 实例,创建配置文件,用于通过身份验证连接 Source 集群与 Destination 集群。
本文示例假设 Source 集群和 Destination 集群均使用 SASL_SSL 方式登录,且连接时通过证书校验域名。
本文示例代码中使用以下占位符,执行前请替换为实际配置:
| 占位符 | 说明 | 示例 |
<username> | 集群的 SASL 用户名 | admin |
<password> | 集群的 SASL 密码 | pa$$w0rd |
<source-cluster-address:port> | Source 集群的 bootstrap server 地址 | source-kafka.example.com:9093 |
<destination-cluster-address:port> | Destination 集群的 bootstrap server 地址 | destination-kafka.example.com:9093 |
Source 集群配置
创建连接 Source 集群的配置文件 /tmp/source.config,内容如下。该配置开启自动创建 Mirror Topic、同步 Consumer Group 位点和同步 ACL 条目。
security.protocol=SASL_SSL
sasl.mechanism=PLAIN
sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="<username>" password="<password>";
bootstrap.servers=<source-cluster-address:port>
auto.create.mirror.topics.enable=true
consumer.offset.sync.enable=true
acl.sync.enable=true| 配置项 | 用途 |
auto.create.mirror.topics.enable | 在 Destination 集群上为匹配的源 Topic 自动创建 Mirror Topic |
consumer.offset.sync.enable | 将 Consumer Group 位点从 Source 集群同步到 Destination 集群 |
acl.sync.enable | 将 ACL 条目从 Source 集群同步到 Destination 集群 |
Destination 集群配置
创建连接 Destination 集群的配置文件 /tmp/destination.config,内容如下:
security.protocol=SASL_SSL
sasl.mechanism=PLAIN
sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="<username>" password="<password>";此处使用 /tmp 目录仅为简化示例。生产环境中,请将配置文件保存到安全且持久化的目录。/tmp 目录下的文件可能在系统维护时被清除,导致集群链接处于不一致状态。
准备测试数据
使用 Confluent Platform CLI 在 Source 集群上准备测试数据:创建包含示例数据的 Topic、Consumer Group 和 ACL 条目。
创建测试 Topic
执行以下命令,在 Source 集群上创建名为
test-topic的单分区 Topic,用于后续镜像复制。使用单分区便于观察复制消息的顺序。
kafka-topics --create --topic test-topic --partitions 1 \
--bootstrap-server <source-cluster-address:port> \
--command-config /tmp/source.config使用 list topic 和 describe topic 命令查看 Topic 详情,验证 Topic 创建成功。
#list topic
kafka-topics --list --bootstrap-server <source-cluster-address:port> \
--command-config /tmp/source.config
#describe topic
kafka-topics --describe --topic test-topic \
--bootstrap-server <source-cluster-address:port> \
--command-config /tmp/source.config生产测试消息
执行以下命令,向 Source 集群上的
test-topic发送消息。
seq 1 5 | kafka-console-producer --topic test-topic \
--bootstrap-server <source-cluster-address:port> \
--producer.config /tmp/source.config消费并验证消息
从开头消费 Source 集群中
test-topic上的数据,并将消费者指定到名为test-group的 Consumer Group。
#consume
kafka-console-consumer --topic test-topic --from-beginning \
--bootstrap-server <source-cluster-address:port> --group test-group \
--consumer.config /tmp/source.config
#list consumer groups
kafka-consumer-groups --bootstrap-server <source-cluster-address:port> --list \
--command-config /tmp/source.config
#describe offsets of consumer groups
kafka-consumer-groups --bootstrap-server <source-cluster-address:port> \
--group test-group --describe --offsets \
--command-config /tmp/source.config成功消费消息后,预期输出如下:
1
2
3
4
5
添加 ACL 条目
新增 ACL 条目,授予用户
test-user对test-topic的读取(READ)权限。
#add READ ACL for test-user
kafka-acls --bootstrap-server <source-cluster-address:port> \
--command-config /tmp/source.config --add --allow-principal User:test-user \
--operation READ --topic test-topic
#list
kafka-acls --list --bootstrap-server <source-cluster-address:port> \
--command-config /tmp/source.config配置过滤器并创建集群链接
创建 JSON 过滤器文件,指定需要复制的 Topic、Consumer Group 和 ACL 条目。这些过滤器将传入下一步的集群链接创建命令。
Topic 过滤器
创建用于筛选迁移 Topic 的配置文件
/tmp/topic_filter.json,选择复制test-topic。
{
"topicFilters": [
{
"name": "test-topic",
"patternType": "LITERAL",
"filterType": "INCLUDE"
}
]
}Consumer Group 过滤器
创建用于筛选迁移 Consumer Group 的配置文件
/tmp/group.json,选择同步test-group的位点。
{
"groupFilters": [
{
"name": "test-group",
"patternType": "LITERAL",
"filterType": "INCLUDE"
}
]
}ACL 过滤器
创建用于筛选迁移 ACL 条目的配置文件
/tmp/acl.json,同步所有 ACL 条目。
{
"aclFilters": [
{
"resourceFilter": {
"resourceType": "any",
"patternType": "any"
},
"accessFilter": {
"operation": "any",
"permissionType": "any"
}
}
]
}创建集群链接并同步数据
执行以下命令,在 Destination 集群上创建名为
test-cluster-link的集群链接,并复制 Topic、Consumer Group 和 ACL 条目。该命令应用上一步创建的 Topic、Consumer Group 和 ACL 过滤器。
kafka-cluster-links --create --link test-cluster-link \
--config-file /tmp/source.config \
--topic-filters-json-file /tmp/topic_filter.json \
--consumer-group-filters-json-file /tmp/group.json \
--acl-filters-json-file /tmp/acl.json \
--bootstrap-server <destination-cluster-address:port> \
--command-config /tmp/destination.config集群链接创建完成后,Destination 集群开始从 Source 集群复制匹配的 Topic、Consumer Group 位点和 ACL 条目。
提升 Mirror Topic
数据同步完成后,执行以下命令提升 Mirror Topic,将其转换为可读写的普通 Topic。提升完成后,该 Topic 不再从源 Topic 同步消息。
kafka-mirrors --promote --topics test-topic \
--bootstrap-server <destination-cluster-address:port> \
--command-config /tmp/destination.config预期输出:
Calculating max offset and ms lag for mirror topics: [test-topic]
Finished calculating max offset lag and max lag ms for mirror topics: [test-topic]
Request for stopping topic test-topic's mirror was successfully scheduled. Please use the describe command with the --pending-stopped-only option to monitor progress.验证迁移
完成数据同步和提升后,确认所有数据已复制到 Destination 集群。
检查 Topic、Consumer Group 和 ACL
执行以下命令,查看 Destination 集群的 Topic、Consumer Group 和 ACL 条目是否已同步。
#list topic
kafka-topics --list --bootstrap-server <destination-cluster-address:port> \
--command-config /tmp/destination.config
#list consumer group
kafka-consumer-groups --bootstrap-server <destination-cluster-address:port> \
--list --command-config /tmp/destination.config
#list acl
kafka-acls --list --bootstrap-server <destination-cluster-address:port> \
--command-config /tmp/destination.config确认输出中包含 test-topic、test-group 以及 test-user 的 ACL 条目。
验证生产与消费
验证提升后的 Topic 可接收新消息,且消费正常。
#produce
kafka-console-producer --topic test-topic \
--bootstrap-server <destination-cluster-address:port> \
--producer.config /tmp/destination.config
#consume
kafka-console-consumer --topic test-topic \
--bootstrap-server <destination-cluster-address:port> \
--consumer.config /tmp/destination.config集群链接管理
初始设置完成后,可以使用以下命令管理 Destination 集群上的集群链接。
查看集群链接列表
执行以下命令,查看集群链接列表。
kafka-cluster-links --bootstrap-server <destination-cluster-address:port> \
--list --command-config /tmp/destination.config查看集群链接详情
执行以下命令,查看集群链接详情。
kafka-configs --describe --cluster-link test-cluster-link \
--bootstrap-server <destination-cluster-address:port> \
--command-config /tmp/destination.config删除集群链接
执行以下命令,删除集群链接。
kafka-cluster-links --delete --link test-cluster-link \
--bootstrap-server <destination-cluster-address:port> \
--command-config /tmp/destination.config预期输出:
Cluster link 'test-cluster-link' deletion successfully completed.相关文档
Cluster Linking 的详细介绍,请参见Cluster Linking for Confluent Platform。
确认集群是否可以使用 Cluster Linking,请参见支持的集群类型。
ECS 实例的创建和使用,请参见创建与管理 ECS 实例。