大数据

kafka topic副本扩容

🔧 步骤 1:创建副本分配方案文件

创建一个 JSON 文件(如 increase-replication.json),定义每个分区的新副本列表。

目标:每个分区有 3 个副本,分布在 Broker 1、2、3 上。

{
  "version": 1,
  "partitions": [
    { "topic": "tidb-linkman-batch", "partition": 0, "replicas": [1, 2, 3] },
    { "topic": "tidb-linkman-batch", "partition": 1, "replicas": [1, 2, 3] },
    { "topic": "tidb-linkman-batch", "partition": 2, "replicas": [1, 2, 3] }
  ]
}

💡 建议:
将原 Leader 保留在副本列表第一位(如分区 0 原 Leader 是 1,所以 [1,2,3]
副本顺序不影响功能,但第一位是优选 Leader

🔧 步骤 2:执行副本扩容

# 执行重分配(真正开始复制数据)
bin/kafka-reassign-partitions.sh \
  --bootstrap-server <broker_host>:9092 \
  --reassignment-json-file increase-replication.json \
  --execute

✅ 输出示例:

Current partition replica assignment: {...}
Save this to use as the --reassignment-json-file option during rollback
Successfully started reassignment of partitions.

📌 重要:命令会立即返回,但后台数据复制仍在进行

🔧 步骤 3:验证最终状态

bin/kafka-topics.sh \
  --bootstrap-server <broker_host>:9092 \
  --describe --topic tidb-linkman-batch

✅ 期望输出:

Partition: 0    Leader: 1    Replicas: 1,2,3    Isr: 1,2,3
Partition: 1    Leader: 2    Replicas: 1,2,3    Isr: 1,2,3
Partition: 2    Leader: 3    Replicas: 1,2,3    Isr: 1,2,3

ReplicasIsr 都应为 3 个 Broker。

⚠️ 注意事项

1. 不会中断生产/消费

  • Kafka 支持在线扩容副本,TiCDC 可继续写入,消费者可继续读取。
  • 但会增加磁盘 I/O 和网络带宽(因需同步数据)。

2. Leader 不会自动切换

  • 扩容后,Leader 仍是原来的 Broker(如分区 0 还是 Broker 1)。
  • 如果你想让 Leader 均衡分布,可额外执行 preferred replica election
bin/kafka-leader-election.sh \
  --bootstrap-server <broker>:9092 \
  --election-type preferred \
  --topic tidb-linkman-batch