kafka 动态扩容现有 topic 的分区数和副本数

这篇具有很好参考价值的文章主要介绍了kafka 动态扩容现有 topic 的分区数和副本数。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

  • 文档内出现的 ${KAFKA_BROKERS} 表示 kafka 的连接地址,${ZOOKEEPER_CONNECT} 表示 zk 的连接地址,需要替换成自己的实际 ip 地址

创建一个演示 topic

kafka-topics.sh --create --zookeeper ${ZOOKEEPER_CONNECT} --replication-factor 1 --partitions 3 --topic test-topic-update

查看 topic 详情

kafka-topics.sh --bootstrap-server ${KAFKA_BROKERS} --describe --topic test-topic-update

总共是六个 kafka 节点,三分区一副本,分散在三个不同的 kafka 节点

Topic:test-topic-update PartitionCount:3        ReplicationFactor:1     Configs:segment.bytes=1073741824
        Topic: test-topic-update        Partition: 0    Leader: 5       Replicas: 5     Isr: 5
        Topic: test-topic-update        Partition: 1    Leader: 1       Replicas: 1     Isr: 1
        Topic: test-topic-update        Partition: 2    Leader: 0       Replicas: 0     Isr: 0
  • 关于输出内容的概念
    • 分区(Partition)
      • 主题(Topic)在 Kafka 中的数据被分成一个或多个分区。每个分区是一个有序且持久化的消息日志。
      • 分区允许 Kafka 集群进行水平扩展,使多个消费者能够并行地处理主题的消息。
      • 消费者组中的每个消费者负责处理一个或多个分区的消息。
    • 领导者(Leader)
      • 每个分区都有一个领导者,领导者负责处理该分区的所有读写请求。
      • 生产者向领导者发送消息,消费者从领导者读取消息。
      • 领导者也负责维护分区的复制和同步。
    • 副本(Replicas)
      • 为了提高数据的冗余和可用性,每个分区可以有多个副本,包括一个领导者副本和零个或多个追随者副本。
      • 领导者副本处理写请求,追随者副本用于数据冗余和读请求。
    • 同步副本集(In-Sync Replicas,ISR)
      • 同步副本集是指在分区的所有副本中,与领导者副本保持同步的副本。
      • 领导者和同步副本集中的副本是可用于读取的,其他追随者副本可能会有一些延迟。

生产一些数据

  • 手动生产 300 条数据
kafka-verifiable-producer.sh --broker-list ${KAFKA_BROKERS} --topic test-topic-update --max-messages 300

使用消费者组消费数据

  • 消费者组不存在的情况下,没有返回被消费的数据,过两三秒之后,可以中断这个命令,然后使用下面的 --describe 来验证
kafka-console-consumer.sh --bootstrap-server ${KAFKA_BROKERS} --topic test-topic-update --group test-topic-update-group

查看消费组内的 topic 消费情况

kafka-consumer-groups.sh --bootstrap-server ${KAFKA_BROKERS} --describe --group test-topic-update-group

目前三百条都被消费了,使用上面的生产数据的命令,再生产300条,模拟 topic 有数据的场景

GROUP                   TOPIC             PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG             CONSUMER-ID     HOST            CLIENT-ID
test-topic-update-group test-topic-update 2          100             100             0               -               -               -
test-topic-update-group test-topic-update 0          100             100             0               -               -               -
test-topic-update-group test-topic-update 1          100             100             0               -               -               -

生产完数据后,再次查看,返回结果如下

GROUP                   TOPIC             PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG             CONSUMER-ID     HOST            CLIENT-ID
test-topic-update-group test-topic-update 2          100             200             100             -               -               -
test-topic-update-group test-topic-update 0          100             200             100             -               -               -
test-topic-update-group test-topic-update 1          100             200             100             -               -               -

增加分区

  • 在增加分区的场景下比较方便,直接使用 --alter 就能实现,这里将原来的 3 分区改成 12 分区
kafka-topics.sh --bootstrap-server ${KAFKA_BROKERS} --alter --topic test-topic-update --partitions 12
无新数据产生,有旧数据未消费

查看 topic 情况

kafka-topics.sh --bootstrap-server ${KAFKA_BROKERS} --describe --topic test-topic-update

可以看到,分区已经更新成 12 个了,也可以看出,kafka 在动态增加分区的时候,是均分的,都会按照类似下面的 5-1-0-3-2-4 这样的顺序去均分(当然,前提是分区数和节点数是倍数关系)

Topic:test-topic-update PartitionCount:12       ReplicationFactor:1     Configs:segment.bytes=1073741824
        Topic: test-topic-update        Partition: 0    Leader: 5       Replicas: 5     Isr: 5
        Topic: test-topic-update        Partition: 1    Leader: 1       Replicas: 1     Isr: 1
        Topic: test-topic-update        Partition: 2    Leader: 0       Replicas: 0     Isr: 0
        Topic: test-topic-update        Partition: 3    Leader: 3       Replicas: 3     Isr: 3
        Topic: test-topic-update        Partition: 4    Leader: 2       Replicas: 2     Isr: 2
        Topic: test-topic-update        Partition: 5    Leader: 4       Replicas: 4     Isr: 4
        Topic: test-topic-update        Partition: 6    Leader: 5       Replicas: 5     Isr: 5
        Topic: test-topic-update        Partition: 7    Leader: 1       Replicas: 1     Isr: 1
        Topic: test-topic-update        Partition: 8    Leader: 0       Replicas: 0     Isr: 0
        Topic: test-topic-update        Partition: 9    Leader: 3       Replicas: 3     Isr: 3
        Topic: test-topic-update        Partition: 10   Leader: 2       Replicas: 2     Isr: 2
        Topic: test-topic-update        Partition: 11   Leader: 4       Replicas: 4     Isr: 4

查看消费组内的分区情况

kafka-consumer-groups.sh --bootstrap-server ${KAFKA_BROKERS} --describe --group test-topic-update-group

因为没有新数据进入,也没有消费旧数据,此时还是显示的原先的信息

GROUP                   TOPIC             PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG             CONSUMER-ID     HOST            CLIENT-ID
test-topic-update-group test-topic-update 2          100             200             100             -               -               -
test-topic-update-group test-topic-update 0          100             200             100             -               -               -
test-topic-update-group test-topic-update 1          100             200             100             -               -               -

将未消费的 300 条数据进行消费

kafka-console-consumer.sh --bootstrap-server ${KAFKA_BROKERS} --topic test-topic-update --group test-topic-update-group --max-messages 300

消费完成后,再次查看消费组的情况

kafka-consumer-groups.sh --bootstrap-server ${KAFKA_BROKERS} --describe --group test-topic-update-group

此时就变成正常的 12 分区了

GROUP                   TOPIC             PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG             CONSUMER-ID     HOST            CLIENT-ID
test-topic-update-group test-topic-update 0          100             100             0               -               -               -
test-topic-update-group test-topic-update 7          0               0               0               -               -               -
test-topic-update-group test-topic-update 5          0               0               0               -               -               -
test-topic-update-group test-topic-update 1          100             100             0               -               -               -
test-topic-update-group test-topic-update 6          0               0               0               -               -               -
test-topic-update-group test-topic-update 2          100             100             0               -               -               -
test-topic-update-group test-topic-update 3          0               0               0               -               -               -
test-topic-update-group test-topic-update 10         0               0               0               -               -               -
test-topic-update-group test-topic-update 9          0               0               0               -               -               -
test-topic-update-group test-topic-update 8          0               0               0               -               -               -
test-topic-update-group test-topic-update 11         0               0               0               -               -               -
test-topic-update-group test-topic-update 4          0               0               0               -               -               -

这里为了方便验证,我把 topic 删了后重建了,下面这个删除 topic 的命令,大家别随意执行,会删除数据的

kafka-topics.sh --bootstrap-server ${KAFKA_BROKERS} --delete --topic test-topic-update
有新数据产生,有旧数据未消费
  • 同样,先扩容分区
kafka-topics.sh --bootstrap-server ${KAFKA_BROKERS} --alter --topic test-topic-update --partitions 12

未生产新数据的时候,查看消费者组的信息同样是没有更新分区信息

kafka-consumer-groups.sh --bootstrap-server ${KAFKA_BROKERS} --describe --group test-topic-update-group

此时,手动使用命令模拟新数据进来

kafka-verifiable-producer.sh --broker-list ${KAFKA_BROKERS} --topic test-topic-update --max-messages 100

通过命令查看消费者组的情况

kafka-consumer-groups.sh --bootstrap-server ${KAFKA_BROKERS} --describe --group test-topic-update-group

此时显示的是老分区,而且只显示了 8+8+9=25 条数据

GROUP                   TOPIC             PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG             CONSUMER-ID     HOST            CLIENT-ID
test-topic-update-group test-topic-update 2          100             108             8               -               -               -
test-topic-update-group test-topic-update 0          100             108             8               -               -               -
test-topic-update-group test-topic-update 1          100             109             9               -               -               -

手动消费一下数据试试

kafka-console-consumer.sh --bootstrap-server ${KAFKA_BROKERS} --topic test-topic-update --group test-topic-update-group --max-messages 100

发现返回的信息里面,只显示25条数据

kafka-consumer-groups.sh --bootstrap-server ${KAFKA_BROKERS} --describe --group test-topic-update-group

但是观察消费者组的情况,显示的是都消费了,看起来,应该是和 topic 加入新消费者组的情况一样,不展示,但实际消费数据了(这块是个人的理解,具体的原理需要有兴趣的大佬深究一下,希望能赐教带我飞)

GROUP                   TOPIC             PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG             CONSUMER-ID     HOST            CLIENT-ID
test-topic-update-group test-topic-update 0          108             108             0               -               -               -
test-topic-update-group test-topic-update 7          9               9               0               -               -               -
test-topic-update-group test-topic-update 5          8               8               0               -               -               -
test-topic-update-group test-topic-update 1          109             109             0               -               -               -
test-topic-update-group test-topic-update 6          9               9               0               -               -               -
test-topic-update-group test-topic-update 2          108             108             0               -               -               -
test-topic-update-group test-topic-update 3          8               8               0               -               -               -
test-topic-update-group test-topic-update 10         8               8               0               -               -               -
test-topic-update-group test-topic-update 9          8               8               0               -               -               -
test-topic-update-group test-topic-update 8          8               8               0               -               -               -
test-topic-update-group test-topic-update 11         8               8               0               -               -               -
test-topic-update-group test-topic-update 4          9               9               0               -               -               -

增加副本

创建 json 文件
  • kafka-reassign-partitions.sh 是 Kafka 提供的命令行工具,用于重新分配主题分区的副本。这个工具允许你重新定义主题分区副本的分布,以实现负载均衡、故障恢复或集群扩展等目的

之前的 topic 是 1 副本,12 分区,按照之前的 5-1-0-3-2-4 的顺序来分配第一个副本,然后按照 4-3-2-0-1-5 的顺序来分配第二个副本,我这里的 json 文件就命名为:add_rep_test_topic_update.json,大家可以以自己实际来命名

{"version":1, "partitions":[
{"topic":"test-topic-update","partition":0,"replicas":[5,4]},
{"topic":"test-topic-update","partition":1,"replicas":[1,3]},
{"topic":"test-topic-update","partition":2,"replicas":[0,2]},
{"topic":"test-topic-update","partition":3,"replicas":[3,0]},
{"topic":"test-topic-update","partition":4,"replicas":[2,1]},
{"topic":"test-topic-update","partition":5,"replicas":[4,5]},
{"topic":"test-topic-update","partition":6,"replicas":[5,4]},
{"topic":"test-topic-update","partition":7,"replicas":[1,3]},
{"topic":"test-topic-update","partition":8,"replicas":[0,2]},
{"topic":"test-topic-update","partition":9,"replicas":[3,0]},
{"topic":"test-topic-update","partition":10,"replicas":[2,1]},
{"topic":"test-topic-update","partition":11,"replicas":[4,5]}]
}
使用指定的 json 文件增加 topic 的副本数
kafka-reassign-partitions.sh --zookeeper ${ZOOKEEPER_CONNECT} --execute --reassignment-json-file add_rep_test_topic_update.json
使用指定的 json 文件查看 topic 的副本数增加的进度
kafka-reassign-partitions.sh --zookeeper ${ZOOKEEPER_CONNECT} --verify --reassignment-json-file add_rep_test_topic_update.json

通过命令返回的内容,可以看出都成功了

Reassignment of partition test-topic-update-0 completed successfully
Reassignment of partition test-topic-update-7 completed successfully
Reassignment of partition test-topic-update-5 completed successfully
Reassignment of partition test-topic-update-1 completed successfully
Reassignment of partition test-topic-update-6 completed successfully
Reassignment of partition test-topic-update-2 completed successfully
Reassignment of partition test-topic-update-3 completed successfully
Reassignment of partition test-topic-update-10 completed successfully
Reassignment of partition test-topic-update-9 completed successfully
Reassignment of partition test-topic-update-8 completed successfully
Reassignment of partition test-topic-update-11 completed successfully
Reassignment of partition test-topic-update-4 completed successfully
查看 topic 情况
kafka-topics.sh --bootstrap-server ${KAFKA_BROKERS} --describe --topic test-topic-update

现在的 topic 变成了 12 分区,2 副本的状态了文章来源地址https://www.toymoban.com/news/detail-688012.html

Topic:test-topic-update PartitionCount:12       ReplicationFactor:2     Configs:segment.bytes=1073741824
        Topic: test-topic-update        Partition: 0    Leader: 5       Replicas: 5,4   Isr: 5,4
        Topic: test-topic-update        Partition: 1    Leader: 1       Replicas: 1,3   Isr: 3,1
        Topic: test-topic-update        Partition: 2    Leader: 0       Replicas: 0,2   Isr: 0,2
        Topic: test-topic-update        Partition: 3    Leader: 3       Replicas: 3,0   Isr: 3,0
        Topic: test-topic-update        Partition: 4    Leader: 1       Replicas: 2,1   Isr: 1,2
        Topic: test-topic-update        Partition: 5    Leader: 4       Replicas: 4,5   Isr: 5,4
        Topic: test-topic-update        Partition: 6    Leader: 5       Replicas: 5,4   Isr: 4,5
        Topic: test-topic-update        Partition: 7    Leader: 1       Replicas: 1,3   Isr: 1,3
        Topic: test-topic-update        Partition: 8    Leader: 0       Replicas: 0,2   Isr: 0,2
        Topic: test-topic-update        Partition: 9    Leader: 3       Replicas: 3,0   Isr: 0,3
        Topic: test-topic-update        Partition: 10   Leader: 1       Replicas: 2,1   Isr: 1,2
        Topic: test-topic-update        Partition: 11   Leader: 4       Replicas: 4,5   Isr: 4,5

到了这里,关于kafka 动态扩容现有 topic 的分区数和副本数的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • kafka的副本以及分区与副本的关系

    1.Kafka 副本作用:提高数据可靠性。 2.Kafka 中副本分为:Leader 和 Follower。Kafka 生产者只会把数据发往 Leader, 然后 Follower 找 Leader 进行同步数据。 读写由leader来完成,follower只备份,和leader同步数据,leader发生故障,follower顶上去。 leader副本:可以理解为某个分区中,除了不

    2023年04月09日
    浏览(25)
  • KafKa 分区,副本实战

    5个broker (1主4从) 安装目路/config/server.properties, 额外复制4份为 server-2.properties,server-3.properties,server-4.properties,server-5.properties 主要配置不同 server.properties server-2.properties server-3.properties server-4.properties server-5.properties 运行这5个broker 创建一个主题test,8个分区,3个副本 bootstrap

    2024年02月11日
    浏览(28)
  • Kafka的分区和副本机制

    Kafka的分区和副本机制是分布式消息系统中的重要概念,它们在数据一致性和容错方面起到了关键作用。下面我将详细介绍这两个机制的工作原理和底层实现思路,并通过Java源码示例和分析来加深理解。 Kafka分区机制 Kafka的分区机制是按照一定规则将主题(Topic)中的消息分

    2024年02月01日
    浏览(38)
  • Kafka之分区副本与ISR

    Kafka的Topic分区本质是一个用于存储Topic下的消息的日志,但是只存一份日志会因为机器损坏或其他原因导致消息丢失不可恢复, 因此需要多个相同的日志作为备份,提高系统可用性,这些备份在kafka中被称为副本(replica)。 kafka将分区的所有副本均匀的分配到所有broker上,并从

    2024年02月04日
    浏览(25)
  • kafka topic分区数设定

    然后假设总的目标吞吐量是Tt,那么分区数=Tt / min(Tp,Tc) 例如:producer吞吐量 = 70m/s;consumer吞吐量 =100m/s,期望吞吐量 300m/s; 分区数 = 300 / 70 = 4或者5个分区

    2024年02月02日
    浏览(41)
  • kafka如何在运行中增加分区并重新分配副本

    1.扩容 在新的物理机上安装kafka程序,修改config/server.properties文件里的broker.id必须在集群中唯一,修改其他必要的配置项,其中zookeeper.connect配置项,写上kafka集群现在使用的zookeeper集群的地址。 然后启动kafka就可以加入到集群中了。 但是新加入的机器只能对新产生的topic起作

    2024年02月10日
    浏览(30)
  • Kafka3.0.0版本——手动调整分区副本示例

    四台服务器 原始服务器名称 原始服务器ip 节点 centos7虚拟机1 192.168.136.27 broker0 centos7虚拟机2 192.168.136.28 broker1 centos7虚拟机3 192.168.136.29 broker2 centos7虚拟机4 192.168.136.30 broker3 2.1、先启动zookeeper集群 启动zookeeper集群 2.2、再启动kafka集群 启动kafka集群 3.1、手动调整分区副本的前提

    2024年02月11日
    浏览(44)
  • kafka topic多分区乱序问题

    [root@centos6 bin]# ./kafka-topics.sh --create --bootstrap-server 127.0.0.1:9092 --replication-factor 1 --partitions 6 --topic test1 Created topic test1. [root@centos6 kafka]# cat produce_kafka.py  from kafka import KafkaProducer from kafka.errors import KafkaError import os producer = KafkaProducer(bootstrap_servers=[\\\'127.0.0.1:9092\\\']) # Asynchronous by de

    2024年02月06日
    浏览(34)
  • Docker中Kafka容器创建/更新Topic支持多分区

    自行通过docker部署好kafka,并启动相关容器。 假设Topic为http_capture。 检测Kafka运行正常后,如果Topic为http_capture的主题存在,则更新分区为5个,若不存在Topic,则新建。

    2024年04月26日
    浏览(29)
  • kafka中topic的部分分区leader为none,怎样解决?

      (以Hadoop的topic为例) 进入Zookeeper客户端查看kafka存储的信息,/kafka/brokers/topics/hadoop/partitions/1/state get /kafka/brokers/topics/hadoop/partitions/1/state 查看到 {\\\"controller_epoch\\\":33,\\\"leader\\\":-1,\\\"version\\\":1,\\\"leader_epoch\\\":25,\\\"isr\\\":[3]}  leader为-1,固分区的leader为none 修改/kafka/brokers/topics/hadoop/partitions/

    2024年02月03日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包