1. 程式人生 > >kafka叢集擴容(Topic遷移)

kafka叢集擴容(Topic遷移)

文章新地址

文章開始前推薦使用 kafka-manager 工具,可以實時檢視kafka同步狀態,broker實時讀寫流量,topic分割槽情況等資訊,提供重選最優leader等操作。

kafka的叢集擴容實際上就是把 topic 的 partition 移動到新加的叢集上。

生成 topic 移動 json檔案有兩種方式:

  1. 通過 --topics-to-move-json-file--broker-list 批量生成新的topic分割槽資訊,然後根據該資訊執行轉移操作。

  2. 手動寫要移動的topic資訊,更靈活,但是在大量 topic 和 partition 的情況下非常繁瑣並且容易出錯。

1. 啟動新節點

將原節點上的 kafka 目錄通過 scp 命令拷貝到新節點,只需要修改配置檔案中的 broker_id 和 ip 地址,然後依次啟動 kafka 服務。

2. 移動topic

移動 topicA , topicB

sudo vim topics-to-move.json

{"topics": [{"topic": "topicA"},
            {"topic": "topicB"}],
"version":1
}

將上述 topic 移動到 broker 3,4,5上,用 generate 命令生成 partition 分配 json 串。

bin/kafka-reassign-partitions.sh --zookeeper zk1:2181 
--topics-to-move-json-file topics-to-move.json --broker-list "3,4,5" --generate 

將看到如下:

Current partition replica assignment
{"version":1,"partitions":[{"topic":"topicA","partition":2,"replicas":[0,2]},
{"topic":"topicA","partition":1,"replicas":[1,0]},
{"topic":"topicB","partition":1,"replicas":[0,1]},
{"topic":"topicA","partition":0,"replicas":[2,1]},
{"topic":"topicB","partition":0,"replicas":[2,0]},
{"topic":"topicB","partition":2,"replicas":[1,2]}]}

Proposed partition reassignment configuration

{"version":1,"partitions":[{"topic":"topicA","partition":2,"replicas":[3,4]},
{"topic":"topicA","partition":1,"replicas":[5,3]},
{"topic":"topicB","partition":1,"replicas":[3,4]},
{"topic":"topicA","partition":0,"replicas":[4,5]},
{"topic":"topicB","partition":0,"replicas":[5,3]},
{"topic":"topicB","partition":2,"replicas":[4,5]}]}

Proposed partition reassignment configuration 下方的資料寫入 expand-cluster-reassignment.json

上文說的第二種方式其實就是手動寫該檔案,而不用命令生成的。

vim expand-cluster-reassignment.json

{"version":1,"partitions":[{"topic":"topicA","partition":2,"replicas":[3,4]},
{"topic":"topicA","partition":1,"replicas":[5,3]},
{"topic":"topicB","partition":1,"replicas":[3,4]},
{"topic":"topicA","partition":0,"replicas":[4,5]},
{"topic":"topicB","partition":0,"replicas":[5,3]},
{"topic":"topicB","partition":2,"replicas":[4,5]}]}

execute 命令開始正式執行,將會把上述2個topic按 expand-cluster-reassignment 中的設定進行分配,該過程可能會影響其它topic的讀取(延時)

bin/kafka-reassign-partitions.sh --zookeeper zk1:2181 
--reassignment-json-file expand-cluster-reassignment.json --execute

執行狀態查詢

查詢執行狀態

bin/kafka-reassign-partitions.sh --zookeeper zk1:2181 
--reassignment-json-file expand-cluster-reassignment.json --verify

主要有幾種狀態

Reassignment of partition [topicA,4] is still in progress # 轉移中
Reassignment of partition [topicB,2] completed successfully # 轉移結束

如果你看到一大堆的 ERROR 資訊:

ERROR: Assigned replicas (0,5,1,2,3,4) don't match the list of replicas for reassignment (5,3,4) for partition [topicA,3]
Reassignment of partition [topicA,3] failed

不要方,不要關閉節點,過會再看看就正常了。