新节点服役

新加入的 Kafka 节点,要求对之前的主题进行负载均衡

先按照之前 Kafka 在 Windows 下集群搭建 的方式,创建了一个新的 broker,id=3

接着查询主题详情,从图中我们可以看到,之前创建的主题副本并没有出现在新的 broker 上,因此我们希望可以实现对特定主题的负载均衡

1、创建要均衡的主题

创建 balance.json 文件,内容如下

1
2
3
4
{
"topics": [{ "topic": "firstTest" }, { "topic": "secondTest" }],
"version": 1
}

创建好以后放到了 %KAFKA_HOME%\bin\windows 目录下

2、生成负载均衡计划

%KAFKA_HOME%\bin\windows 目录下执行命令

1
kafka-reassign-partitions.bat --bootstrap-server localhost:9092 --topics-to-move-json-file balance.json --broker-list "0,1,2,3" --generate

上面的是当前的计划,下面的是生成的计划,如果符合要求的话,就可以直接拿来执行。

3、执行副本存储计划

将上面生成的 json 结果拷贝到新的文件:execute.json ,同样放到 %KAFKA_HOME%\bin\windows 目录下

生成的 json 结果即:Proposed partition reassignment configuration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
{
"version": 1,
"partitions": [
{
"topic": "firstTest",
"partition": 0,
"replicas": [0, 2, 3],
"log_dirs": ["any", "any", "any"]
},
{
"topic": "firstTest",
"partition": 1,
"replicas": [1, 3, 0],
"log_dirs": ["any", "any", "any"]
},
{
"topic": "firstTest",
"partition": 2,
"replicas": [2, 0, 1],
"log_dirs": ["any", "any", "any"]
},
{
"topic": "secondTest",
"partition": 0,
"replicas": [1, 2, 3],
"log_dirs": ["any", "any", "any"]
},
{
"topic": "secondTest",
"partition": 1,
"replicas": [2, 3, 0],
"log_dirs": ["any", "any", "any"]
},
{
"topic": "secondTest",
"partition": 2,
"replicas": [3, 0, 1],
"log_dirs": ["any", "any", "any"]
}
]
}

执行命令

1
kafka-reassign-partitions.bat --bootstrap-server localhost:9092 --reassignment-json-file execute.json --execute

结果

4、验证副本存储计划

执行命令

1
kafka-reassign-partitions.bat --bootstrap-server localhost:9092 --reassignment-json-file execute.json --verify

同时通过查看主题详情加以验证

退役旧节点

与新节点服役的操作相同,首先创建需要均衡的主题,可以参考上面的文件

然后执行负载均衡计划,少了 broker3 这个节点

1
kafka-reassign-partitions.bat --bootstrap-server localhost:9092 --topics-to-move-json-file balance.json --broker-list "0,1,2" --generate

复制生成的计划,我是直接替代了原来的execute.json 文件,执行计划

1
kafka-reassign-partitions.bat --bootstrap-server localhost:9092 --reassignment-json-file execute.json --execute

验证计划

1
kafka-reassign-partitions.bat --bootstrap-server localhost:9092 --reassignment-json-file execute.json --verify

同时查看主题详情加以验证

1
kafka-topics.bat --bootstrap-server localhost:9092 --describe --topic firstTest,secondTest

最后在相应服务器上执行 Kafka 停止命令即可(这里用的 Windows 集群,就不演示了)