kafka leader traffic 重平衡

To rebalance broker traffic after a leader imbalance in Apache Kafka, you need to redistribute partition leaders more evenly across your brokers. This ensures that no broker becomes a bottleneck due to hosting too many partition leaders (which handle all client requests).


🧭 Why Leader Imbalance Happens

Leader imbalance can occur when:

  • New brokers are added
  • Broker restarts or failures occur
  • Uneven topic partition assignments
  • Manual reassignment was skipped or misconfigured

✅ Solutions to Rebalance Leader Traffic

✅ Option 1: Use Kafka's Built-In Auto Balancer (Kafka 2.4+)

Kafka has an auto leader rebalance feature.

Check if auto rebalance is enabled:

bash 复制代码
kafka-configs.sh --bootstrap-server <broker>:9092 \
  --entity-type brokers \
  --entity-name <broker-id> \
  --all \
  --describe | grep leader

Look for:

text 复制代码
auto.leader.rebalance.enable=true

Example:

bash 复制代码
bash-4.4$ kafka-configs --bootstrap-server localhost:9092 --entity-type brokers --entity-name 1 --all  --describe | grep leader
  auto.leader.rebalance.enable=true sensitive=false synonyms={DEFAULT_CONFIG:auto.leader.rebalance.enable=true}
  leader.imbalance.check.interval.seconds=300 sensitive=false synonyms={DEFAULT_CONFIG:leader.imbalance.check.interval.seconds=300}
  leader.imbalance.per.broker.percentage=10 sensitive=false synonyms={DEFAULT_CONFIG:leader.imbalance.per.broker.percentage=10}
  unclean.leader.election.enable=false sensitive=false synonyms={DEFAULT_CONFIG:unclean.leader.election.enable=false}

This is the default setting, but if it's false, enable it:

bash 复制代码
kafka-configs.sh --bootstrap-server <broker>:9092 \
  --entity-type brokers \
  --entity-name <broker-id> \
  --alter \
  --add-config auto.leader.rebalance.enable=true

Kafka will then periodically rebalance leaders using leader.imbalance.check.interval.seconds.

重平衡过程:

系统会自动将其他broker上的一些leader转移到新broker,

前提是新broker必须已经是该partition的follower。

转移是渐进式的,不会一次性全部迁移。

自动 Leader 重平衡 仅调整 Leader 角色,不会自动调整 Partition 的副本分配(即不会自动把 Follower 迁移到新 Broker)。


✅ Option 2: Trigger Manual Leader Reassignment

Use the built-in script:

bash 复制代码
kafka-preferred-replica-election.sh --bootstrap-server <broker>:9092

This command triggers preferred leader election . Kafka will try to move partition leaders to their preferred replicas (usually the first broker in the replica list).

This is useful if leader imbalance occurred due to broker failures or restarts.


✅ Option 3: Full Partition Reassignment (Advanced)

If imbalance persists, you may need a full partition reassignment to redistribute both partition leaders and replicas.

Steps:

a) Generate current assignment:
bash 复制代码
kafka-reassign-partitions.sh \
  --bootstrap-server <broker>:9092 \
  --generate \
  --topics-to-move-json-file topics.json \
  --broker-list "1,2,3"

topics.json format:

json 复制代码
{
  "topics": [{"topic": "your-topic-name"}],
  "version": 1
}
b) Review the proposed plan, then execute:
bash 复制代码
kafka-reassign-partitions.sh \
  --bootstrap-server <broker>:9092 \
  --execute \
  --reassignment-json-file reassignment.json

✅ Option 4: Use Cruise Control (Recommended for Large Clusters)

LinkedIn's Cruise Control is a powerful tool to automate:

  • Traffic balancing
  • Leader and replica assignment
  • Self-healing after broker failure

📌 Summary

Method Use When Command
Auto Leader Rebalance Minor imbalance auto.leader.rebalance.enable=true
Manual Election Leader drift after broker restart kafka-preferred-replica-election.sh
Full Reassignment Major imbalance or cluster expansion kafka-reassign-partitions.sh
Cruise Control Ongoing traffic management Cruise Control API/UI
相关推荐
问道飞鱼11 小时前
【分布式技术】RustFS 非 Docker 部署完整指南:从单机到生产集群
分布式·docker·容器·rustfs
DJ斯特拉11 小时前
Redisson分布式锁
分布式
学到头秃的suhian12 小时前
消息队列发送消息场景分析
kafka
heimeiyingwang14 小时前
【架构实战】分布式ID生成方案(雪花/Leaf/美团)
分布式·架构
yxy___1 天前
达梦分布式集群DPC_重做副本-操作指南(DEM)_yxy
运维·分布式
里欧跑得慢1 天前
Flutter 三方库 ethereum 鸿蒙分布式区块链数字资产上链钱包适配突破:接通 JSON-RPC 加密管线深入打通智能合约闭环实现高价值数字加密交互-适配鸿蒙 HarmonyOS ohos
分布式·flutter·harmonyos
zs宝来了1 天前
Kafka 存储原理:索引文件与日志段管理
kafka·存储·索引·源码解析·日志段
2501_933329551 天前
技术深度拆解:Infoseek舆情系统的全链路架构与核心实现
开发语言·人工智能·分布式·架构
辣机小司1 天前
【生产级 Kafka (KRaft) 双中心容灾演练:MirrorMaker 2.0 (MM2) 核心参数配置与回切踩坑指南】
分布式·kafka·集群同步·kafka双集群
softshow10261 天前
SpringCloud Redis与分布式
redis·分布式·spring cloud