在某些代理关闭后如何更改主题领导者或删除分区?
How change topic leader or remove partition after some broker down?
我们有一个带有 4 个代理的 kafka 集群和一些具有副本因子 1 和 10 个分区的主题。
在某一时刻,我们的 4 个带有 kafka 集群的服务器中有 2 个出现故障。
所以现在我们有 2 个具有相同主题的经纪人。
当我m run command
./kafka_topics.sh --zookeeper localhost:2181 --describe
i
m 得到这个:
Topic:outcoming-notification-error-topic PartitionCount:10 ReplicationFactor:1 Configs:
Topic: outcoming-error-topic Partition: 0 Leader: 2 Replicas: 2 Isr: 2
Topic: outcoming-error-topic Partition: 1 Leader: 3 Replicas: 3 Isr: 3
Topic: outcoming-error-topic Partition: 2 Leader: 4 Replicas: 4 Isr: 4
Topic: outcoming-error-topic Partition: 3 Leader: 1 Replicas: 1 Isr: 1
Topic: outcoming-error-topic Partition: 4 Leader: 2 Replicas: 2 Isr: 2
Topic: outcoming-error-topic Partition: 5 Leader: 3 Replicas: 3 Isr: 3
Topic: outcoming-error-topic Partition: 6 Leader: 4 Replicas: 4 Isr: 4
Topic: outcoming-error-topic Partition: 7 Leader: 1 Replicas: 1 Isr: 1
Topic: outcoming-error-topic Partition: 8 Leader: 2 Replicas: 2 Isr: 2
Topic: outcoming-error-topic Partition: 9 Leader: 3 Replicas: 3 Isr: 3
如何删除 Leader 2...4?或者我可能需要为这个 Leader 删除分区,但是如何?
UPD..
我们还使用 kafka_exporter 通过 prometheus 监控 kafka。在 kafka_exporter 日志中有 2 个代理关闭后,我们收到此错误:
level=error msg="Cannot get oldest offset of topic outcoming-error-topic partition 10: kafka server: In the middle of a leadership election, there is currently no leader for this partition and hence it is unavailable for writes." source="kafka_exporter.go:296"
您可以使用 Kafka 的 kafka-reassign-partitions.sh
来做到这一点。您有两种方法,一种是生成新分配的提案,另一种是manually specifying特定分区的领导者。
1。生成提案
kafka docs 中指定的第一种方法遵循以下逻辑:
1.1 生成建议的分区重新分配配置
首先,您应该创建一个 json 文件,例如 link 中提供的文件。我们将其命名为 topics.json
.
{
"topics": [{"topic": "foo1"},
{"topic": "foo2"}],
"version":1
}
这将告诉 kafka 您愿意从哪些主题重新分配它们的分区。在这个例子中,他想让Kafka对主题foo1
和foo2
.
提出建议
有了那个json,调用该工具并在命令中设置活动经纪人列表:
kafka-reassign-partitions.sh --zookeeper $ZK_HOSTS
--topics-to-move-json-file topics.json --broker-list "1,2,3,4,5" --generate
这将输出 Kafka 的建议,您可以将其保存到另一个 .json 文件中。例如:
{
"version":1,
"partitions":[{"topic":"foo1","partition":2,"replicas":[5,6]},
{"topic":"foo1","partition":0,"replicas":[5,6]},
{"topic":"foo2","partition":2,"replicas":[5,6]},
{"topic":"foo2","partition":0,"replicas":[5,6]},
{"topic":"foo1","partition":1,"replicas":[5,6]},
{"topic":"foo2","partition":1,"replicas":[5,6]}]
}
如果您愿意,您可以手动修改一些分配(或者认为这是正确的想法,因为该工具并不完美)。将json保存到文件中,例如reassign-example.json
,下一步会用到
1.2。 执行提议的分区重新分配
让我们让 Kafka 执行提案并移动分区。为此,执行:
bin/kafka-reassign-partitions.sh --zookeeper $ZK_HOSTS
--reassignment-json-file reassign-example.json --execute
这将执行在 reassign-example.json
文件上定义的分区移动。
2。手动规格
第二种方法比较简单,但您必须手动确定要重新分配的分区。例如,如果您希望主题 XXX 的分区 1 移动到代理 5 和 6,您可以创建一个 json 文件 (manual-reassign.json
),例如:
{"version":1,"partitions":[{"topic":"XXX","partition":1,"replicas":[5,6]}]}
启动方式与上一种方式相同:
bin/kafka-reassign-partitions.sh --zookeeper $ZK_HOSTS
--reassignment-json-file manual-reassign.json --execute
我们有一个带有 4 个代理的 kafka 集群和一些具有副本因子 1 和 10 个分区的主题。
在某一时刻,我们的 4 个带有 kafka 集群的服务器中有 2 个出现故障。
所以现在我们有 2 个具有相同主题的经纪人。
当我m run command
./kafka_topics.sh --zookeeper localhost:2181 --describe
i
m 得到这个:
Topic:outcoming-notification-error-topic PartitionCount:10 ReplicationFactor:1 Configs:
Topic: outcoming-error-topic Partition: 0 Leader: 2 Replicas: 2 Isr: 2
Topic: outcoming-error-topic Partition: 1 Leader: 3 Replicas: 3 Isr: 3
Topic: outcoming-error-topic Partition: 2 Leader: 4 Replicas: 4 Isr: 4
Topic: outcoming-error-topic Partition: 3 Leader: 1 Replicas: 1 Isr: 1
Topic: outcoming-error-topic Partition: 4 Leader: 2 Replicas: 2 Isr: 2
Topic: outcoming-error-topic Partition: 5 Leader: 3 Replicas: 3 Isr: 3
Topic: outcoming-error-topic Partition: 6 Leader: 4 Replicas: 4 Isr: 4
Topic: outcoming-error-topic Partition: 7 Leader: 1 Replicas: 1 Isr: 1
Topic: outcoming-error-topic Partition: 8 Leader: 2 Replicas: 2 Isr: 2
Topic: outcoming-error-topic Partition: 9 Leader: 3 Replicas: 3 Isr: 3
如何删除 Leader 2...4?或者我可能需要为这个 Leader 删除分区,但是如何?
UPD..
我们还使用 kafka_exporter 通过 prometheus 监控 kafka。在 kafka_exporter 日志中有 2 个代理关闭后,我们收到此错误:
level=error msg="Cannot get oldest offset of topic outcoming-error-topic partition 10: kafka server: In the middle of a leadership election, there is currently no leader for this partition and hence it is unavailable for writes." source="kafka_exporter.go:296"
您可以使用 Kafka 的 kafka-reassign-partitions.sh
来做到这一点。您有两种方法,一种是生成新分配的提案,另一种是manually specifying特定分区的领导者。
1。生成提案
kafka docs 中指定的第一种方法遵循以下逻辑:
1.1 生成建议的分区重新分配配置
首先,您应该创建一个 json 文件,例如 link 中提供的文件。我们将其命名为 topics.json
.
{
"topics": [{"topic": "foo1"},
{"topic": "foo2"}],
"version":1
}
这将告诉 kafka 您愿意从哪些主题重新分配它们的分区。在这个例子中,他想让Kafka对主题foo1
和foo2
.
有了那个json,调用该工具并在命令中设置活动经纪人列表:
kafka-reassign-partitions.sh --zookeeper $ZK_HOSTS
--topics-to-move-json-file topics.json --broker-list "1,2,3,4,5" --generate
这将输出 Kafka 的建议,您可以将其保存到另一个 .json 文件中。例如:
{
"version":1,
"partitions":[{"topic":"foo1","partition":2,"replicas":[5,6]},
{"topic":"foo1","partition":0,"replicas":[5,6]},
{"topic":"foo2","partition":2,"replicas":[5,6]},
{"topic":"foo2","partition":0,"replicas":[5,6]},
{"topic":"foo1","partition":1,"replicas":[5,6]},
{"topic":"foo2","partition":1,"replicas":[5,6]}]
}
如果您愿意,您可以手动修改一些分配(或者认为这是正确的想法,因为该工具并不完美)。将json保存到文件中,例如reassign-example.json
,下一步会用到
1.2。 执行提议的分区重新分配
让我们让 Kafka 执行提案并移动分区。为此,执行:
bin/kafka-reassign-partitions.sh --zookeeper $ZK_HOSTS
--reassignment-json-file reassign-example.json --execute
这将执行在 reassign-example.json
文件上定义的分区移动。
2。手动规格
第二种方法比较简单,但您必须手动确定要重新分配的分区。例如,如果您希望主题 XXX 的分区 1 移动到代理 5 和 6,您可以创建一个 json 文件 (manual-reassign.json
),例如:
{"version":1,"partitions":[{"topic":"XXX","partition":1,"replicas":[5,6]}]}
启动方式与上一种方式相同:
bin/kafka-reassign-partitions.sh --zookeeper $ZK_HOSTS
--reassignment-json-file manual-reassign.json --execute