0


Kafka topic分区增加副本

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

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

准备工作

创建副本为1的topic

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

查看topic信息

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

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

增加为3副本

使用官方自带的

  1. 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 rollbackSuccessfully 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 successfullyReassignment of partition test_topic-1 completed successfullyReassignment 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个。

附录

  1. # kafka-reassign-partitions
  2. This tool helps to moves topic partitions between replicas.
  3. Option Description
  4. ------ -----------
  5. --bootstrap-server <String: Server(s) the server(s) to use for
  6. to use for bootstrapping> bootstrapping. REQUIRED if an
  7. absolute path of the log directory
  8. is specified for any replica in the
  9. reassignment json file
  10. --broker-list <String: brokerlist> The list of brokers to which the
  11. partitions need to be reassigned in
  12. the form "0,1,2". This is required
  13. if --topics-to-move-json-file is
  14. used to generate reassignment
  15. configuration
  16. --command-config <String: Admin client Property file containing configs to be
  17. property file> passed to Admin Client.
  18. --disable-rack-aware Disable rack aware replica assignment
  19. --execute Kick off the reassignment as specified
  20. by the --reassignment-json-file
  21. option.
  22. --generate Generate a candidate partition
  23. reassignment configuration. Note
  24. that this only generates a candidate
  25. assignment, it does not execute it.
  26. --help Print usage information.
  27. --reassignment-json-file <String: The JSON file with the partition
  28. manual assignment json file path> reassignment configurationThe format
  29. to use is -
  30. {"partitions":
  31. [{"topic": "foo",
  32. "partition": 1,
  33. "replicas": [1,2,3],
  34. "log_dirs": ["dir1","dir2","dir3"]
  35. }],
  36. "version":1
  37. }
  38. Note that "log_dirs" is optional. When
  39. it is specified, its length must
  40. equal the length of the replicas
  41. list. The value in this list can be
  42. either "any" or the absolution path
  43. of the log directory on the broker.
  44. If absolute log directory path is
  45. specified, the replica will be moved
  46. to the specified log directory on
  47. the broker.
  48. --replica-alter-log-dirs-throttle The movement of replicas between log
  49. <Long: replicaAlterLogDirsThrottle> directories on the same broker will
  50. be throttled to this value
  51. (bytes/sec). Rerunning with this
  52. option, whilst a rebalance is in
  53. progress, will alter the throttle
  54. value. The throttle rate should be
  55. at least 1 KB/s. (default: -1)
  56. --throttle <Long: throttle> The movement of partitions between
  57. brokers will be throttled to this
  58. value (bytes/sec). Rerunning with
  59. this option, whilst a rebalance is
  60. in progress, will alter the throttle
  61. value. The throttle rate should be
  62. at least 1 KB/s. (default: -1)
  63. --timeout <Long: timeout> The maximum time in ms allowed to wait
  64. for partition reassignment execution
  65. to be successfully initiated
  66. (default: 10000)
  67. --topics-to-move-json-file <String: Generate a reassignment configuration
  68. topics to reassign json file path> to move the partitions of the
  69. specified topics to the list of
  70. brokers specified by the --broker-
  71. list option. The format to use is -
  72. {"topics":
  73. [{"topic": "foo"},{"topic": "foo1"}],
  74. "version":1
  75. }
  76. --verify Verify if the reassignment completed
  77. as specified by the --reassignment-
  78. json-file option. If there is a
  79. throttle engaged for the replicas
  80. specified, and the rebalance has
  81. completed, the throttle will be
  82. removed
  83. --zookeeper <String: urls> REQUIRED: The connection string for
  84. the zookeeper connection in the form
  85. host:port. Multiple URLS can be
  86. given to allow fail-over.

本文转载自: https://blog.csdn.net/ifenggege/article/details/127100902
版权归原作者 upupfeng 所有, 如有侵权,请联系我们删除。

“Kafka topic分区增加副本”的评论:

还没有评论