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
相关推荐
{⌐■_■}10 小时前
【Kafka】登录日志处理的三次阶梯式优化实践:从同步写入到Kafka多分区批处理
数据库·分布式·mysql·kafka·go
qq_5298353510 小时前
RabbitMQ的消息可靠传输
分布式·rabbitmq
CodeWithMe10 小时前
【Note】《Kafka: The Definitive Guide》 第九章:Kafka 管理与运维实战
运维·分布式·kafka
sql2008help10 小时前
1-Kafka介绍及常见应用场景
分布式·kafka
何苏三月15 小时前
SpringCloud系列 - Seata 分布式事务(六)
分布式·spring·spring cloud
工藤学编程16 小时前
分库分表之实战-sharding-JDBC绑定表配置实战
数据库·分布式·后端·sql·mysql
gtestcandle16 小时前
rabbitmq 的多用户、多vhost使用
分布式·rabbitmq
老纪的技术唠嗑局16 小时前
单机分布式一体化数据库的架构设计与优化
数据库·分布式
SkyrimCitadelValinor16 小时前
Git【开源分布式版本控制工具】安装-配置-常用指令-Git远程仓库-IDEA使用Git
分布式·git·gitee·开源·项目管理