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:

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

Look for:

auto.leader.rebalance.enable=true

Example:

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:

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:

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:
kafka-reassign-partitions.sh \
  --bootstrap-server <broker>:9092 \
  --generate \
  --topics-to-move-json-file topics.json \
  --broker-list "1,2,3"

topics.json format:

{
  "topics": [{"topic": "your-topic-name"}],
  "version": 1
}
b) Review the proposed plan, then execute:
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

MethodUse WhenCommand
Auto Leader RebalanceMinor imbalanceauto.leader.rebalance.enable=true
Manual ElectionLeader drift after broker restartkafka-preferred-replica-election.sh
Full ReassignmentMajor imbalance or cluster expansionkafka-reassign-partitions.sh
Cruise ControlOngoing traffic managementCruise Control API/UI
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值