kafka关于修改副本数和分区的数的案例实战(也可用作leader节点均衡案例)

              kafka关于修改副本数和分区的数的案例实战(也可用作leader节点均衡案例)

                                               作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

 

 

 

一.关于topic分区数的修改

1>.创建1分区1个的topic,名称为yinzhengjie-channel

[root@node101 ~]# kafka-topics.sh --zookeeper node102.yinzhengjie.org.cn:2181 --create --replication-factor 1 -partitions 1 --topic yinzhengjie-channel
Created topic "yinzhengjie-channel".
[root@node101 ~]# 

2>.查看topic的信息

[root@node101 ~]# kafka-topics.sh --describe --zookeeper node102.yinzhengjie.org.cn:2181 --topic yinzhengjie-channel
Topic:yinzhengjie-channel    PartitionCount:1    ReplicationFactor:1    Configs:
    Topic: yinzhengjie-channel    Partition: 0    Leader: 103    Replicas: 103    Isr: 103                              #可以很明显的看出kafka 的分区数和副本数都是1
[root@node101 ~]# 

3>.将之前创建的topic修改为3个分区

[root@node101 ~]# kafka-topics.sh --alter --zookeeper node102.yinzhengjie.org.cn:2181 --topic yinzhengjie-channel --partitions 3
WARNING: If partitions are increased for a topic that has a key, the partition logic or ordering of the messages will be affected
Adding partitions succeeded!
[root@node101 ~]# 

4>.再次查看topic的分区数

[root@node101 ~]# kafka-topics.sh --describe --zookeeper node102.yinzhengjie.org.cn:2181 --topic yinzhengjie-channel                   
Topic:yinzhengjie-channel    PartitionCount:3    ReplicationFactor:1    Configs:
    Topic: yinzhengjie-channel    Partition: 0    Leader: 103    Replicas: 103    Isr: 103                                    #这是第一个分区,它的副本数依然是1一个,当前0号分区的副本数存放在103这个节点上。说明你的数据修改成功啦!
    Topic: yinzhengjie-channel    Partition: 1    Leader: 101    Replicas: 101    Isr: 101
    Topic: yinzhengjie-channel    Partition: 2    Leader: 102    Replicas: 102    Isr: 102
[root@node101 ~]# 

 

二.关于topic副本数的修改

1>.编写分配脚本

[root@node101 ~]# cat addReplicas.json 
{"topics":
    [{"topic":"yinzhengjie-channel"}],
    "version": 1
}
[root@node101 ~]# 

2>.执行分配计划,用于生成json格式的文件

[root@node101 ~]# kafka-reassign-partitions.sh --zookeeper node102.yinzhengjie.org.cn:2181 --topics-to-move-json-file addReplicas.json --broker-list "101,102,103" --generate
Current partition replica assignment                  #这是当前的分区情况,你可以结合--describe参数查看当前的分区情况
{"version":1,"partitions":[{"topic":"yinzhengjie-channel","partition":0,"replicas":[103]},{"topic":"yinzhengjie-channel","partition":1,"replicas":[101]},{"topic":"yinzhengjie-channel","partition":2,"replicas":[102]}]}

Proposed partition reassignment configuration         #这是推荐分区计划
{"version":1,"partitions":[{"topic":"yinzhengjie-channel","partition":0,"replicas":[102]},{"topic":"yinzhengjie-channel","partition":1,"replicas":[103]},{"topic":"yinzhengjie-channel","partition":2,"replicas":[101]}]}
[root@node101 ~]# 
[root@node101 ~]# kafka-topics.sh --describe --zookeeper node102.yinzhengjie.org.cn:2181 --topic yinzhengjie-channel      #查看当前分区的情况
Topic:yinzhengjie-channel    PartitionCount:3    ReplicationFactor:1    Configs:
    Topic: yinzhengjie-channel    Partition: 0    Leader: 103    Replicas: 103    Isr: 103
    Topic: yinzhengjie-channel    Partition: 1    Leader: 101    Replicas: 101    Isr: 101
    Topic: yinzhengjie-channel    Partition: 2    Leader: 102    Replicas: 102    Isr: 102
[root@node101 ~]# 

3>.Proposed partition reassignment configuration 后是根据命令行的指定的brokerlist生成的分区分配计划json格式。将 Proposed partition reassignment configuration的配置copy保存到一个文件中 topic-reassignment.json并对它进行相应的修改

[root@node101 ~]# cat topic-reassignment.json    #注意,我在复制下来之后,对副本数进行了修改,由之前的1个副本升级为2个副本。
{"version":1,"partitions":[{"topic":"yinzhengjie-channel","partition":0,"replicas":[102,101]},{"topic":"yinzhengjie-channel","partition":1,"replicas":[102,103]},{"topic":"yinzhengjie-channel","partition":2,"replicas":[101,103]}]}
[root@node101 ~]# 

4>.根据上一步生成的分配计划配置json文件topic-reassignment.json,进行topic的重新分配。

[root@node101 ~]# kafka-reassign-partitions.sh --zookeeper node102.yinzhengjie.org.cn:2181 --reassignment-json-file topic-reassignment.json --execute
Current partition replica assignment

{"version":1,"partitions":[{"topic":"yinzhengjie-channel","partition":0,"replicas":[103]},{"topic":"yinzhengjie-channel","partition":1,"replicas":[101]},{"topic":"yinzhengjie-channel","partition":2,"replicas":[102]}]}

Save this to use as the --reassignment-json-file option during rollback
Successfully started reassignment of partitions.
[root@node101 ~]# 

5>.查看分配的进度

[root@node101 ~]# kafka-reassign-partitions.sh --zookeeper node102.yinzhengjie.org.cn:2181 --reassignment-json-file topic-reassignment.json --verify
Status of partition reassignment: 
Reassignment of partition [yinzhengjie-channel,0] completed successfully      #如果这里的参数是:is still in progress,说明正在进行分配,如果看到当前的提示说明分配完成。
Reassignment of partition [yinzhengjie-channel,1] completed successfully
Reassignment of partition [yinzhengjie-channel,2] completed successfully
[root@node101 ~]# 

     温馨提示,上述的方法不仅仅可以用来修改副本数,还可以用来修改你的leader节点,下图我就是我在生产环境中用来均衡leader节点的实操截图:(是不是上面我提到的2种状态都有呢?)

6>.如果分配完成,我们再次查看

[root@node101 ~]# kafka-topics.sh --describe --zookeeper node102.yinzhengjie.org.cn:2181 --topic yinzhengjie-channel      #查看当前分区的情况,这是还没有重新分配的时候
Topic:yinzhengjie-channel    PartitionCount:3    ReplicationFactor:1    Configs:
    Topic: yinzhengjie-channel    Partition: 0    Leader: 103    Replicas: 103    Isr: 103        #这里的副本数只有一个!
    Topic: yinzhengjie-channel    Partition: 1    Leader: 101    Replicas: 101    Isr: 101
    Topic: yinzhengjie-channel    Partition: 2    Leader: 102    Replicas: 102    Isr: 102
[root@node101 ~]# 
root@node101 ~]# kafka-topics.sh --describe --zookeeper node102.yinzhengjie.org.cn:2181 --topic yinzhengjie-channel
Topic:yinzhengjie-channel    PartitionCount:3    ReplicationFactor:2    Configs:
    Topic: yinzhengjie-channel    Partition: 0    Leader: 102    Replicas: 102,101    Isr: 102,101      #副本数编程了2个!
    Topic: yinzhengjie-channel    Partition: 1    Leader: 102    Replicas: 102,103    Isr: 103,102
    Topic: yinzhengjie-channel    Partition: 2    Leader: 101    Replicas: 101,103    Isr: 103,101
[root@node101 ~]# 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Kafka是一个分布式消息队列系统,它将据分为多个主题(topics),每个主题又被分为多个分区(partitions)。每个分区都有一个leader和若干个副本(replicas)。Kafkaleader分区均衡指的是在分布式环境下,不同分区leader副本量不均衡的情况。 Kafka分区副本是通过集群成员之间的协作来维护的。当一个分区leader副本出现故障或不可用时,会从该分区副本中选举出一个新的leader。然而,在选举过程中可能会出现不均衡的情况。 造成Kafka leader分区均衡的原因可能有以下几个方面: 1. 网络问题:Kafka集群中的不同节点之间的网络延迟或故障可能导致选举过程中的不均衡。在网络不稳定的情况下,分区leader选举可能会出现延迟或失败。 2. 负载不均衡:如果某些分区的负载较重,它们的leader副本可能频繁地发生切换,而其他分区leader副本则相对稳定。这样会导致分区leader分布不均衡。 3. 机器故障:如果Kafka集群中的某些节点频繁发生故障,会导致那些节点上的分区leader副本频繁变更,进而导致整个集群的leader分区均衡。 解决Kafka leader分区均衡问题的方法可以包括以下几个方面: 1. 优化网络:检查和优化Kafka集群节点之间的网络连接和通信,确保网络的稳定性和低延迟。 2. 均衡负载:根据分区的负载情况,合理分配和管理不同分区leader副本,避免某些分区的负载过重。 3. 处理机器故障:及时检测和处理Kafka集群中的机器故障,通过增加或替换故障的节点来保持整个集群的稳定性和均衡性。 4. 监控和调整:通过有效的监控系统来追踪和检测Kafka集群中的leader分区状态,当发现不均衡问题时,及时采取调整措施。 通过以上的措施,可以有效解决Kafka leader分区均衡的问题,提高整个集群的性能和可靠性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值