【kafka】kafka 在线增加分区副本数
2024-04-09 18:30:33  阅读数 1458
  1. 创建 increase-replication-factor.json 文件

$ cat increase-replication-factor.json

{"version":1, "partitions":[
{"topic":"testTopic","partition":0,"replicas":[0,1,2]},
{"topic":"testTopic","partition":1,"replicas":[0,1,2]},
{"topic":"testTopic","partition":2,"replicas":[0,1,2]},
{"topic":"testTopic","partition":3,"replicas":[0,1,2]},
{"topic":"testTopic","partition":4,"replicas":[0,1,2]},
{"topic":"testTopic","partition":5,"replicas":[0,1,2]},
{"topic":"testTopic","partition":6,"replicas":[0,1,2]},
{"topic":"testTopic","partition":7,"replicas":[0,1,2]},
{"topic":"testTopic","partition":8,"replicas":[0,1,2]},
{"topic":"testTopic","partition":9,"replicas":[0,1,2]},
{"topic":"testTopic","partition":10,"replicas":[0,1,2]},
{"topic":"testTopic","partition":11,"replicas":[0,1,2]},
{"topic":"testTopic","partition":12,"replicas":[0,1,2]},
{"topic":"testTopic","partition":13,"replicas":[0,1,2]},
{"topic":"testTopic","partition":14,"replicas":[0,1,2]},
{"topic":"testTopic","partition":15,"replicas":[0,1,2]}]
}
  1. 执行增加副本操作
$  bin/kafka-reassign-partitions.sh --zookeeper xx.xx.xx.xx:2181  --reassignment-json-file increase-replication-factor.json  --execute
  1. 查看执行进度
$ /bin/kafka-reassign-partitions.sh --zookeeper xx.xx.xx.xx:2181  --reassignment-json-file  increase-replication-factor.json  --verify

Status of partition reassignment:
Reassignment of partition testTopic-15 completed successfully
Reassignment of partition testTopic-2 completed successfully
Reassignment of partition testTopic-7 completed successfully
Reassignment of partition testTopic-12 completed successfully
Reassignment of partition testTopic-4 completed successfully
Reassignment of partition testTopic-11 completed successfully
Reassignment of partition testTopic-10 completed successfully
Reassignment of partition testTopic-8 completed successfully
Reassignment of partition testTopic-5 completed successfully
Reassignment of partition testTopic-13 completed successfully
Reassignment of partition testTopic-1 completed successfully
Reassignment of partition testTopic-14 completed successfully
Reassignment of partition testTopic-9 completed successfully
Reassignment of partition testTopic-6 completed successfully
Reassignment of partition testTopic-0 completed successfully
Reassignment of partition testTopic-3 completed successfully

要熟练使用 Kafka 自带的 kafka-reassign-partitions.sh 脚本工具来完成对 topic 的分区分配、分区副本增加等操作。
该脚本有三个参数:

--generate:配合着 --topics-to-move-json-file 可以生成分区分配策略,该参数适用于分区多的情况
--execute: 配合着 --reassignment-json-file   可以执行分区分配策略
--verify:  配合着 --reassignment-json-file   可以检查分区分配进度

通过以上命令,既可以分配分区,也可以增加分区副本数,非常方便。

参考

kafka 迁移分区创建规则json
https://www.orchome.com/454#item-8

kafka命令大全
https://www.orchome.com/454#item-8

kafka集群中有多个offsets.topic.replication.factor?
https://cloud.tencent.com/developer/ask/sof/1540657/answer/2102855

kafka高可用失败问题 3broker,单杀一个broker就不能消费的问题探讨
https://www.orchome.com/805