分享

kafka实战:如何重分配分区




1、确定需要执行重分区的topic,可以指定多个,建议每次重分区一个;(重分区之前检查下topic大小)
  1. # vi topics-to-move.json
  2. {
  3.   "topics": [{
  4.      "topic": "test-topic"
  5.    }],
  6.    "version": 1
  7. }
复制代码
2、生成分区方案
  1. Current partition replica assignment 当前分区分布(用于回滚)
复制代码
  1. Proposed partition reassignment configuration  重分区之后的分区方案(分区后的效果)
复制代码
注意broker-list指定重分配的节点,执行之前先确认每个节点的broker id

不一定需要执行改步骤,也可以手动分配



  1. #bin/kafka-reassign-partitions.sh --zookeeper localhost:2181 --topics-to-move-json-file topics-to-move.json --broker-list "1,2,3,4,5" --generate
  2. Current partition replica assignment
  3. {"version":1,"partitions":[{"topic":"test-topic","partition":2,"replicas":[2,1],"log_dirs":["any","any"]},{"topic":"test-topic","partition":3,"replicas":[3,2],"log_dirs":["any","any"]},{"topic":"test-topic","partition":6,"replicas":[2,3],"log_dirs":["any","any"]},{"topic":"test-topic","partition":4,"replicas":[0,1],"log_dirs":["any","any"]},{"topic":"test-topic","partition":1,"replicas":[1,0],"log_dirs":["any","any"]},{"topic":"test-topic","partition":9,"replicas":[1,3],"log_dirs":["any","any"]},{"topic":"test-topic","partition":5,"replicas":[1,2],"log_dirs":["any","any"]},{"topic":"test-topic","partition":7,"replicas":[3,0],"log_dirs":["any","any"]},{"topic":"test-topic","partition":0,"replicas":[0,3],"log_dirs":["any","any"]},{"topic":"test-topic","partition":8,"replicas":[0,2],"log_dirs":["any","any"]}]}
  4. Proposed partition reassignment configuration
  5. {"version":1,"partitions":[{"topic":"test-topic","partition":4,"replicas":[3,4],"log_dirs":["any","any"]},{"topic":"test-topic","partition":7,"replicas":[1,3],"log_dirs":["any","any"]},{"topic":"test-topic","partition":1,"replicas":[5,1],"log_dirs":["any","any"]},{"topic":"test-topic","partition":9,"replicas":[3,5],"log_dirs":["any","any"]},{"topic":"test-topic","partition":6,"replicas":[5,2],"log_dirs":["any","any"]},{"topic":"test-topic","partition":3,"replicas":[2,3],"log_dirs":["any","any"]},{"topic":"test-topic","partition":8,"replicas":[2,4],"log_dirs":["any","any"]},{"topic":"test-topic","partition":0,"replicas":[4,5],"log_dirs":["any","any"]},{"topic":"test-topic","partition":5,"replicas":[4,1],"log_dirs":["any","any"]},{"topic":"test-topic","partition":2,"replicas":[1,2],"log_dirs":["any","any"]}]}
复制代码
手动分配分区文件如下所示:
  1. {
  2.         "version": 1,
  3.         "partitions": [{
  4.                 "topic": "testTopic",
  5.                 "partition": 0,
  6.                 "replicas": [3, 0]
  7.         }, {
  8.                 "topic": "testTopic",
  9.                 "partition": 1,
  10.                 "replicas": [0, 1]
  11.         }, {
  12.                 "topic": "testTopic",
  13.                 "partition": 2,
  14.                 "replicas": [1, 2]
  15.         }, {
  16.                 "topic": "testTopic",
  17.                 "partition": 3,
  18.                 "replicas": [2, 3]
  19.         }, {
  20.                 "topic": "testTopic",
  21.                 "partition": 4,
  22.                 "replicas": [3, 1]
  23.         }, {
  24.                 "topic": "testTopic",
  25.                 "partition": 5,
  26.                 "replicas": [0, 2]
  27.         }, {
  28.                 "topic": "testTopic",
  29.                 "partition": 6,
  30.                 "replicas": [1, 3]
  31.         }, {
  32.                 "topic": "testTopic",
  33.                 "partition": 7,
  34.                 "replicas": [2, 0]
  35.         }, {
  36.                 "topic": "testTopic",
  37.                 "partition": 8,
  38.                 "replicas": [3, 2]
  39.         }, {
  40.                 "topic": "testTopic",
  41.                 "partition": 9,
  42.                 "replicas": [0, 3]
  43.         }]
  44. }
复制代码
3、将生成的分区方案保存至json文件中
  1. #vi reassignment.json
  2. {"version":1,"partitions":[{"topic":"test-topic","partition":4,"replicas":[3,4],"log_dirs":["any","any"]},{"topic":"test-topic","partition":7,"replicas":[1,3],"log_dirs":["any","any"]},{"topic":"test-topic","partition":1,"replicas":[5,1],"log_dirs":["any","any"]},{"topic":"test-topic","partition":9,"replicas":[3,5],"log_dirs":["any","any"]},{"topic":"test-topic","partition":6,"replicas":[5,2],"log_dirs":["any","any"]},{"topic":"test-topic","partition":3,"replicas":[2,3],"log_dirs":["any","any"]},{"topic":"test-topic","partition":8,"replicas":[2,4],"log_dirs":["any","any"]},{"topic":"test-topic","partition":0,"replicas":[4,5],"log_dirs":["any","any"]},{"topic":"test-topic","partition":5,"replicas":[4,1],"log_dirs":["any","any"]},{"topic":"test-topic","partition":2,"replicas":[1,2],"log_dirs":["any","any"]}]}
复制代码
4、执行分区方案
  1. # bin/kafka-reassign-partitions.sh --zookeeper localhost:2181 --reassignment-json-file reassignment.json --execute
  2. Current partition replica assignment
  3. {"version":1,"partitions":[{"topic":"test-topic","partition":4,"replicas":[3,4],"log_dirs":["any","any"]},{"topic":"test-topic","partition":7,"replicas":[1,3],"log_dirs":["any","any"]},{"topic":"test-topic","partition":1,"replicas":[5,1],"log_dirs":["any","any"]},{"topic":"test-topic","partition":9,"replicas":[3,5],"log_dirs":["any","any"]},{"topic":"test-topic","partition":6,"replicas":[5,2],"log_dirs":["any","any"]},{"topic":"test-topic","partition":3,"replicas":[2,3],"log_dirs":["any","any"]},{"topic":"test-topic","partition":8,"replicas":[2,4],"log_dirs":["any","any"]},{"topic":"test-topic","partition":0,"replicas":[4,5],"log_dirs":["any","any"]},{"topic":"test-topic","partition":5,"replicas":[4,1],"log_dirs":["any","any"]},{"topic":"test-topic","partition":2,"replicas":[1,2],"log_dirs":["any","any"]}]}
  4. Save this to use as the --reassignment-json-file option during rollback
  5. Successfully started reassignment of partitions.
复制代码
5、执行后登录Kafdrop检查分区状态

如果分区完成之后Preferred Replicas不是100%,需要手动重新为分区选举Leader

情况如下:





1.png

  1. # cat leader.json
  2. {
  3.     "version": 1,
  4.     "partitions": [{
  5.         "topic": "showItems",
  6.         "partition": 0
  7.     },{
  8.         "topic": "showItems",
  9.         "partition": 1
  10.     },{
  11.         "topic": "showItems",
  12.         "partition": 2
  13.     },{
  14.         "topic": "showItems",
  15.         "partition": 8
  16.     },{
  17.         "topic": "showItems",
  18.         "partition": 9
  19.     }]
  20. }
复制代码
重新选举
  1. # bin/kafka-preferred-replica-election.sh --zookeeper localhost:2181 --path-to-json-file leader.json
复制代码
执行后检查分区状态是否正常



加微信w3aboutyun,可拉入技术爱好者群

已有(1)人评论

跳转到指定楼层
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条