设为首页 收藏本站
查看: 1408|回复: 0

[经验分享] kafka迁移与扩容

[复制链接]

尚未签到

发表于 2019-1-31 10:59:48 | 显示全部楼层 |阅读模式
  参考官网site:
  http://kafka.apache.org/documentation.html#basic_ops_cluster_expansion
  https://cwiki.apache.org/confluence/display/KAFKA/Replication+tools#Replicationtools-6.ReassignPartitionsTool
  说明:
  当我们对kafka集群扩容时,需要满足2点要求:

  •   将指定topic迁移到集群内新增的node上。
  •   将topic的指定partition迁移到新增的node上。
  1. 迁移topic到新增的node上
  假如现在一个kafka集群运行三个broker,broker.id依次为101,102,103,后来由于业务数据突然暴增,需要新增三个broker,broker.id依次为104,105,106.目的是要把push-token-topic迁移到新增node上。
  1、脚本migration-push-token-topic.json文件内容如下:
  Java代码  

  •   {

  •   "topics":

  •   [

  •   {

  •   "topic": "push-token-topic"

  •   }

  •   ],

  •   "version":1

  •   }
  2、执行脚本如下所示:
  Java代码  

  •   root@localhost:$  ./bin/kafka-reassign-partitions.sh --zookeeper 192.168.2.225:2183 --topics-to-move-json-file  migration-push-token-topic.json  --broker-list  "104,105,106"  --generate
  生成分配partitions的json脚本 备份恢复使用:
  Current partition replica assignment
  {"version":1,"partitions":[{"topic":"cluster-switch-topic","partition":10,"replicas":[8]},{"topic":"cluster-switch-topic","partition":5,"replicas":[4]},{"topic":"cluster-switch-topic","partition":3,"replicas":[5]},{"topic":"cluster-switch-topic","partition":4,"replicas":[5]},{"topic":"cluster-switch-topic","partition":9,"replicas":[5]},{"topic":"cluster-switch-topic","partition":1,"replicas":[5]},{"topic":"cluster-switch-topic","partition":11,"replicas":[4]},{"topic":"cluster-switch-topic","partition":7,"replicas":[5]},{"topic":"cluster-switch-topic","partition":2,"replicas":[4]},{"topic":"cluster-switch-topic","partition":0,"replicas":[4]},{"topic":"cluster-switch-topic","partition":6,"replicas":[4]},{"topic":"cluster-switch-topic","partition":8,"replicas":[4]}]}
  重新分配parttions的json脚本如下:
  migration-topic-cluster-switch-topic.json
  {"version":1,"partitions":[{"topic":"cluster-switch-topic","partition":10,"replicas":[5]},{"topic":"cluster-switch-topic","partition":5,"replicas":[4]},{"topic":"cluster-switch-topic","partition":4,"replicas":[5]},{"topic":"cluster-switch-topic","partition":3,"replicas":[4]},{"topic":"cluster-switch-topic","partition":9,"replicas":[4]},{"topic":"cluster-switch-topic","partition":1,"replicas":[4]},{"topic":"cluster-switch-topic","partition":11,"replicas":[4]},{"topic":"cluster-switch-topic","partition":7,"replicas":[4]},{"topic":"cluster-switch-topic","partition":2,"replicas":[5]},{"topic":"cluster-switch-topic","partition":0,"replicas":[5]},{"topic":"cluster-switch-topic","partition":6,"replicas":[5]},{"topic":"cluster-switch-topic","partition":8,"replicas":[5]}]}
  3、执行:
  Java代码  

  •   root@localhost:$   bin/kafka-reassign-partitions.sh --zookeeper 192.168.2.225:2183 --reassignment-json-file migration-topic-cluster-switch-topic.json --execute
  执行后会生成一个json格式文件expand-cluster-reassignment.json
  4、查询执行状态:
  Java代码  

  •   bin/kafka-reassign-partitions.sh --zookeeper 192.168.2.225:2183 --reassignment-json-file expand-cluster-reassignment.json --verify
  正常执行后会返回当前数据迁移的不用partion的,信息状态类似下面
  Java代码  

  •   Reassignment of partition [push-token-topic,0] completed successfully   //移动成功
  •   Reassignment of partition [push-token-topic,1] is in progress          //这行代表数据在移动中
  •   Reassignment of partition [push-token-topic,2] is in progress
  •   Reassignment of partition [push-token-topic,1] completed successfully
  •   Reassignment of partition [push-token-topic,2] completed successfully
  这样做不会影响原来集群上的topic业务
  2.topic修改(replicats-factor)副本个数
  假如初始时push-token-topic为一个副本,为了提高可用性,需要改为2副本模式。
  脚本replicas-update-push-token-topic.json文件内容如下:
  {
  "partitions":
  [
  {
  "topic": "log.mobile_nginx",
  "partition": 0,
  "replicas": [101,102,104]
  },
  {
  "topic": "log.mobile_nginx",
  "partition": 1,
  "replicas": [102,103,106]
  }
  ],
  "version":1
  }
  2、执行:
  Java代码  

  •   root@localhost:$ ./bin/kafka-reassign-partitions.sh --zookeeper   192.168.2.225:2183 --reassignment-json-file  replicas-update-push-token-topic.json  --execute
  执行后会列出当前的partition和修改后的patition
  3、verify
  Java代码  

  •   bin/kafka-reassign-partitions.sh --zookeeper 192.168.2.225:2181 --reassignment-json-file replicas-update-push-token-topic.json --verify
  如下:
Status of partition reassignment:  
Reassignment of partition [log.mobile_nginx,0] completed successfully
  
Reassignment of partition [log.mobile_nginx,1] completed successfully
  3.自定义分区和迁移
  1、The first step is to hand craft the custom reassignment plan in a json file-
> cat custom-reassignment.json  
{"version":1,"partitions":[{"topic":"foo1","partition":0,"replicas":[5,6]},{"topic":"foo2","partition":1,"replicas":[2,3]}]}
  2、Then, use the json file with the --execute option to start the reassignment process-
> bin/kafka-reassign-partitions.sh --zookeeper localhost:2181 --reassignment-json-file custom-reassignment.json --execute  

  
Current partition replica assignment
  

  
{"version":1,
  
"partitions":[{"topic":"foo1","partition":0,"replicas":[1,2]},
  
               {"topic":"foo2","partition":1,"replicas":[3,4]}]
  
}
  

  
Save this to use as the --reassignment-json-file option during rollback
  
Successfully started reassignment of partitions
  
{"version":1,
  
"partitions":[{"topic":"foo1","partition":0,"replicas":[5,6]},
  
               {"topic":"foo2","partition":1,"replicas":[2,3]}]
  
}
  3、The --verify option can be used with the tool to check the status of the partition reassignment. Note that the same expand-cluster-reassignment.json (used with the --execute option) should be used with the --verify option
bin/kafka-reassign-partitions.sh --zookeeper localhost:2181 --reassignment-json-file custom-reassignment.json --verify  

  
Status of partition reassignment:
  
Reassignment of partition [foo1,0] completed successfully
  
Reassignment of partition [foo2,1] completed successfully
  4.topic的分区扩容用法
  a.先扩容分区数量,脚本如下:
  例如:push-token-topic初始分区数量为12,目前到增加到15个
  root@localhost:$ ./bin/kafka-topics.sh --zookeeper 192.168.2.225:2183 --alter --partitions 15 --topic   push-token-topic
  b.设置topic分区副本
  root@localhost:$ ./bin/kafka-reassign-partitions.sh --zookeeper  192.168.2.225:2183
  --reassignment-json-file partitions-extension-push-token-topic.json  --execute
  脚本partitions-extension-push-token-topic.json文件内容如下:
  {
  "partitions":
  [
  {
  "topic": "push-token-topic",
  "partition": 12,
  "replicas": [101,102]
  },
  {
  "topic": "push-token-topic",
  "partition": 13,
  "replicas": [103,104]
  },
  {
  "topic": "push-token-topic",
  "partition": 14,
  "replicas": [105,106]
  }
  ],
  "version":1
  }



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-669970-1-1.html 上篇帖子: zookeepr+kafka集群--代码实现(三) 下篇帖子: 爬虫架构|利用Kafka处理数据推送问题(1)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表