Kafka topic分区增加副本

Kafka中topic的每个分区可以设置多个副本。如果副本数为1,当该分区副本的leader节点宕机后,会导致该分区不可用。故需要设置多副本来保证可用性。

实际项目中,存在项目初期创建了副本数为1的topic,但是后期又需要扩大副本数的场景。通常不能直接删除topic重建,可以通过如下操作实现。

准备工作

创建副本为1的topic

kafka-topics --zookeeper mdw:2181/kafka --create --replication-factor 1 --partitions 3 --topic test_topic

查看topic信息

kafka-topics --describe --zookeeper mdw:2181/kafka --topic test_topic

输出:
Topic:test_topic        PartitionCount:3        ReplicationFactor:1     Configs:
        Topic: test_topic       Partition: 0    Leader: 364     Replicas: 364   Isr: 364
        Topic: test_topic       Partition: 1    Leader: 365     Replicas: 365   Isr: 365
        Topic: test_topic       Partition: 2    Leader: 366     Replicas: 366   Isr: 366

可以看出3个分区,各自都是只有一个副本。当Leader对应节点挂掉后,分区就不可用了。

增加为3副本

使用官方自带的kafka-reassign-partitions.sh脚本实现。该脚本用来移动分区的副本位置。除了可以实现增加副本,还可以实现将分区的副本移动到指定机器上。该脚本的help见附录。


以下步骤实现将test_topic的各个分区增加为三副本。

  1. 制定分区及副本分配策略

    创建json文件,写入如下内容:

    填写说明:topic为topic名称,partition为分区号,replicas为broker id的数组

    {
        "version": 1,
        "partitions": [
            {
                "topic": "test_topic",
                "partition": 0,
                "replicas": [
                    364,
                    365,
                    366
                ]
            },
            {
                "topic": "test_topic",
                "partition": 1,
                "replicas": [
                    364,
                    365,
                    366
                ]
            },
            {
                "topic": "test_topic",
                "partition": 2,
                "replicas": [
                    364,
                    365,
                    366
                ]
            }
        ]
    }
    
  2. 执行扩副本操作

    本例中创建的json文件为test_topic.json

    执行如下命令:

    kafka-reassign-partitions --zookeeper mdw:2181/kafka --reassignment-json-file test_topic.json --execute
    

    输出内容:

    Current partition replica assignment
    
    {"version":1,"partitions":[{"topic":"test_topic","partition":2,"replicas":[366],"log_dirs":["any"]},{"topic":"test_topic","partition":1,"replicas":[365],"log_dirs":["any"]},{"topic":"test_topic","partition":0,"replicas":[364],"log_dirs":["any"]}]}
    
    Save this to use as the --reassignment-json-file option during rollback
    Successfully started reassignment of partitions.
    

    这是个异步操作,执行命令后开始执行扩副本。

    输出内容中有之前的副本策略,可以保存下来,用于回滚。

  3. 查看执行进度

    kafka-reassign-partitions --zookeeper mdw:2181/kafka --reassignment-json-file test_topic.json --verify
    
    

    出现如下内容说明执行完成

    Status of partition reassignment: 
    Reassignment of partition test_topic-0 completed successfully
    Reassignment of partition test_topic-1 completed successfully
    Reassignment of partition test_topic-2 completed successfully
    
  4. 验证是否成功

    kafka-topics --describe --zookeeper mdw:2181/kafka --topic test_topic
    
    输出:
    Topic:test_topic        PartitionCount:3        ReplicationFactor:3     Configs:
            Topic: test_topic       Partition: 0    Leader: 364     Replicas: 364,365,366   Isr: 364,366,365
            Topic: test_topic       Partition: 1    Leader: 365     Replicas: 364,365,366   Isr: 365,366,364
            Topic: test_topic       Partition: 2    Leader: 366     Replicas: 364,365,366   Isr: 366,365,364
    

    从输出中可以看出每个分区的副本都变成了3个。

附录

# kafka-reassign-partitions
This tool helps to moves topic partitions between replicas.
Option                                  Description                           
------                                  -----------                           
--bootstrap-server <String: Server(s)   the server(s) to use for              
  to use for bootstrapping>               bootstrapping. REQUIRED if an       
                                          absolute path of the log directory  
                                          is specified for any replica in the 
                                          reassignment json file              
--broker-list <String: brokerlist>      The list of brokers to which the      
                                          partitions need to be reassigned in 
                                          the form "0,1,2". This is required  
                                          if --topics-to-move-json-file is    
                                          used to generate reassignment       
                                          configuration                       
--command-config <String: Admin client  Property file containing configs to be
  property file>                          passed to Admin Client.             
--disable-rack-aware                    Disable rack aware replica assignment 
--execute                               Kick off the reassignment as specified
                                          by the --reassignment-json-file     
                                          option.                             
--generate                              Generate a candidate partition        
                                          reassignment configuration. Note    
                                          that this only generates a candidate
                                          assignment, it does not execute it. 
--help                                  Print usage information.              
--reassignment-json-file <String:       The JSON file with the partition      
  manual assignment json file path>       reassignment configurationThe format
                                          to use is -                         
                                        {"partitions":                        
                                                [{"topic": "foo",                    
                                                  "partition": 1,                    
                                                  "replicas": [1,2,3],               
                                                  "log_dirs": ["dir1","dir2","dir3"] 
                                          }],                                 
                                        "version":1                           
                                        }                                     
                                        Note that "log_dirs" is optional. When
                                          it is specified, its length must    
                                          equal the length of the replicas    
                                          list. The value in this list can be 
                                          either "any" or the absolution path 
                                          of the log directory on the broker. 
                                          If absolute log directory path is   
                                          specified, the replica will be moved
                                          to the specified log directory on   
                                          the broker.                         
--replica-alter-log-dirs-throttle       The movement of replicas between log  
  <Long: replicaAlterLogDirsThrottle>     directories on the same broker will 
                                          be throttled to this value          
                                          (bytes/sec). Rerunning with this    
                                          option, whilst a rebalance is in    
                                          progress, will alter the throttle   
                                          value. The throttle rate should be  
                                          at least 1 KB/s. (default: -1)      
--throttle <Long: throttle>             The movement of partitions between    
                                          brokers will be throttled to this   
                                          value (bytes/sec). Rerunning with   
                                          this option, whilst a rebalance is  
                                          in progress, will alter the throttle
                                          value. The throttle rate should be  
                                          at least 1 KB/s. (default: -1)      
--timeout <Long: timeout>               The maximum time in ms allowed to wait
                                          for partition reassignment execution
                                          to be successfully initiated        
                                          (default: 10000)                    
--topics-to-move-json-file <String:     Generate a reassignment configuration 
  topics to reassign json file path>      to move the partitions of the       
                                          specified topics to the list of     
                                          brokers specified by the --broker-  
                                          list option. The format to use is - 
                                        {"topics":                            
                                                [{"topic": "foo"},{"topic": "foo1"}],
                                        "version":1                           
                                        }                                     
--verify                                Verify if the reassignment completed  
                                          as specified by the --reassignment- 
                                          json-file option. If there is a     
                                          throttle engaged for the replicas   
                                          specified, and the rebalance has    
                                          completed, the throttle will be     
                                          removed                             
--zookeeper <String: urls>              REQUIRED: The connection string for   
                                          the zookeeper connection in the form
                                          host:port. Multiple URLS can be     
                                          given to allow fail-over.          
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Kafka分区副本是为了提高据的可靠性和可扩展性而引入的概念。每个Kafkatopic可以分为多个分区,这些分区会均匀地分布在集群的各个节点下。每个分区都是单点的,当其中一个分区不可用时,该分区的消息将无法被消费。为了提高分区的可靠性,Kafka引入了副本的概念。副本分区的冗余备份,通过副本机制来实现据的冗余备份。 创建带有副本topic可以使用以下命令: ``` kafka-topics.sh --create --zookeeper 192.168.10.150:2181 --replication-factor 3 --partitions 3 --topic secondTopic ``` 上述命令创建了一个名为"secondTopic"的topic,该topic有3个分区和3个副本。每个分区都有3个副本,分布在集群的不同节点上。 Kafka提供了据复制算法来保证副本的可靠性。如果leader副本所在的broker节点宕机或出现故障,或者分区的leader节点发生故障,Kafka会从follower副本中选择一个新的leader副本来保证据的可用性和一致性。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *2* *3* [kafka 消息分发机制、分区副本机制](https://blog.csdn.net/weixin_43281498/article/details/124233357)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值