1. 程式人生 > >kafka-preferred-replica-election命令詳解

kafka-preferred-replica-election命令詳解

一、kafka-preferred-replica-election使用背景

在建立一個topic時,kafka儘量將partition均分在所有的brokers上,並且將replicas也均分在不同的broker上。

每個partitiion的所有replicas叫做"assigned replicas","assigned replicas"中的第一個replicas叫"preferred replica",剛建立的topic一般"preferred replica"是leader。leader replica負責所有的讀寫。

但隨著時間推移,broker可能會停機,會導致leader遷移,導致機群的負載不均衡。我們期望對topic的leader進行重新負載均衡,讓partition選擇"preferred replica"做為leader。
 

kafka-preferred-replica-election命令便是用於對Leader進行重新負載均衡

 

二、kafka-preferred-replica-election用法

用法1: 觸發對所有的topic Leader進行負載均衡

kafka-preferred-replica-election --zookeeper cdh-002/kafka

用法2: 對某個topic Leader觸發負載均衡

kafka-preferred-replica-election --zookeeper cdh-002/kafka --path-to-json-file xx.json

其中xx.json格式見如下解釋:
[
[email protected]
~]# kafka-preferred-replica-election This tool causes leadership for each partition to be transferred back to the 'preferred replica', it can be used to balance leadership among the servers Option Description ------ ----------- --path-to-json-file <String: list of The JSON file with the list of partitions for which preferred partitions for which preferred replica leader election needs to be replica leader election should be triggered> done, in the following format - {"partitions": [{"topic": "foo", "partition": 1}, {"topic": "foobar", "partition": 2}] }

三、生產環境常開啟topic Leader的自動負載均衡

可通過配置auto.leader.rebalance.enable=true實現topic Leader的自動負載均衡