kafka-新增topic副本数

发布于:2025-02-27 ⋅ 阅读:(13) ⋅ 点赞:(0)

一. 简述:

  在 Kafka 中增加主题(Topic)的副本数可以提高数据的可靠性和容错能力。在创建topic时,如果设置的不合理,或者创建后有新的需求变动,就需要调整了。 

二. 方法: 

可通过官方的指令:kafka-reassign-partitions.sh

1. 通过kafka-topics.sh 脚本查看topic信息。 

#./kafka-topics.sh --describe --bootstrap-server kafka-server:9092 --topic xxxx_heart_ping

Topic: xxxx_heart_ping	PartitionCount: 12	ReplicationFactor: 3	Configs: min.insync.replicas=2,message.format.version=2.6-IV0,unclean.leader.election.enable=true
	Topic: xxxx_heart_ping	Partition: 0	Leader: 4	Replicas: 4,8	Isr: 4,8
	Topic: xxxx_heart_ping	Partition: 1	Leader: 8	Replicas: 8,6	Isr: 8,6
	Topic: xxxx_heart_ping	Partition: 2	Leader: 6	Replicas: 6,9	Isr: 9,6

2. 不动原有的副本配置,新增一个第三副本(json文件方式):

#cat increase-replication-factor.json
{"version":1,
  "partitions":[
    {"topic":"xxxx_heart_ping","partition":0,"replicas":[4,8,5]},
    {"topic":"xxxx_heart_ping","partition":1,"replicas":[8,6,7]},
    {"topic":"xxxx_heart_ping","partition":2,"replicas":[6,9,4]},
]}

3. 通过kafka-reassign-partitions.sh 重新分配数据:

/kafka-reassign-partitions.sh --bootstrap-server kafka-server:9092 --reassignment-json-file increase-replication-factor.json --execute

4. 可通过--verify查看分配进度:

/kafka-reassign-partitions.sh --bootstrap-server kafka-server:9092 --reassignment-json-file increase-replication-factor.json --verify


 ----------------------------------------------------------------------------------------------

深耕运维行业多年,擅长linux、容器云原生、运维自动化等方面。
承接各类运维环境部署、方案设计/实施、服务代运维工作,欢迎沟通交流!

(V: xiaoxiangbj2013 ) !