脚本kafka-configs.sh用法解析

前言介绍

网络上针对脚本kafka-configs.sh用法,也有一些各类文章,但都不系统不全面,介绍的内容是有缺失的,总让人看起来很懂,用起来难,例如:动态配置内部关系不清晰、有些重点配置参数主从同步配额限流也没有解释清楚,除非去看代码。因此我但愿读者经过深刻阅读此文,更便捷利用此脚本解决实际运维和开发中遇到的问题,同时也为你们节省学习时间。html

脚本语法解析

kafka-configs.sh参数解析

语法格式

增长配置项json

某个topic配置对象bootstrap

bin/kafka-configs.sh --zookeeper localhost:2181/kafkacluster --alter --entity-type topics --entity-name topicName  --add-config 'k1=v1, k2=v2, k3=v3' 后端

全部clientId的配置对象服务器

bin/kafka-configs.sh --zookeeper localhost:2181/kafkacluster --alter --entity-type clients --entity-default --add-config 'k1=v1, k2=v2, k3=v3' 
网络

例子app

bin/kafka-configs.sh --zookeeper localhost:2181/kafkacluster --alter --entity-type topics --entity-name topicName  --add-config 'max.message.bytes=50000000, flush.messages=50000, flush.ms=5000'运维

bin/kafka-configs.sh --zookeeper localhost:2181/kafkacluster --alter --entity-type topics --entity-name topicName  --add-config 'max.message.bytes=50000000' --add-config 'flush.messages=50000'ide

删除配置项性能

bin/kafka-configs.sh --zookeeper localhost:2181/kafkacluster --alter --entity-type topics --entity-name topicName --delete-config ‘k1,k2,k3’

bin/kafka-configs.sh --zookeeper localhost:2181/kafkacluster --alter --entity-type clients --entity-name clientId --delete-config ‘k1,k2,k3’

bin/kafka-configs.sh --bootstrap-server localhost:9092 --alter --entity-type brokers --entity-name $brokerId --delete-config ‘k1,k2,k3’

bin/kafka-configs.sh --bootstrap-server localhost:9092 --alter --entity-type brokers --entity-default --delete-config ‘k1,k2,k3’

例子

bin/kafka-configs.sh --zookeeper localhost:2181/kafkacluster --alter --entity-type topics --entity-name test-cqy --delete-config 'segment.bytes'

修改配置项

修改配置项与增长语法格式相同,相同参数后端直接覆盖

列出entity配置描述

bin/kafka-configs.sh --zookeeper localhost:2181/kafkacluster --entity-type topics --entity-name topicName --describe

bin/kafka-configs.sh--bootstrap-server localhost:9092 --entity-type brokers --entity-name $brokerId --describe

bin/kafka-configs.sh --bootstrap-server localhost:9092 --entity-type brokers --entity-default --describe

bin/kafka-configs.sh  --zookeeper localhost:2181/kafkacluster --entity-type users --entity-name user1 --entity-type clients --entity-name clientA --describe

其余依次类推,不一一列举

配置管理用法

客户端配额限流

kafka支持配额管理,从而能够对Producer和Consumer的produce&fetch操做进行流量限制,防止个别业务压爆服务器。本文主要介绍如何使用kafka的配额管理功能

配额限流简介

Kafka配额限流由3种粒度配置:

  • users + clients
  • users
  • clients

以上3种都是对接入的client的身份进行的认定方式。其中clientid是每一个接入kafka集群的client的一个身份标志,在ProduceRequest和FetchRequest中都须要带上;users只有在开启了身份认证的kafka集群才有。producer和consumer的clientid默认值分别为producer-自增序号、groupid

配置优先级

以上三种粒度配置会组合成8个配置对象,相同配置项做用域范围不一样,高优先级覆盖低优先级,下面为配置优先级

 

 

配置项列表

配置用例

1.配置users + clients

bin/kafka-configs.sh  --zookeeper localhost:2181/kafkacluster --alter  --entity-type users --entity-name user1 --entity-type clients --entity-name clientA --add-config 'producer_byte_rate=20971520,consumer_byte_rate=20971520' 

bin/kafka-configs.sh  --zookeeper localhost:2181/kafkacluster --alter  --entity-type users --entity-name user1 --entity-type clients --entity-default --add-config 'producer_byte_rate=20971520,consumer_byte_rate=20971520' 

bin/kafka-configs.sh  --zookeeper localhost:2181/kafkacluster --alter  --entity-type users --entity-default --entity-type clients --entity-default --add-config 'producer_byte_rate=20971520,consumer_byte_rate=20971520' 

2.配置users

broker内全部的users累加总和最大producer生产&消费速率为20MB/sec

bin/kafka-configs.sh  --zookeeper localhost:2181/kafkacluster --entity-type users --entity-default --alter --add-config 'producer_byte_rate=20971520,consumer_byte_rate=20971520'

broker内userA的最大producer生产&消费速率为20MB/sec

bin/kafka-configs.sh  --zookeeper localhost:2181/kafkacluster --entity-type users --entity-name userA --alter --add-config 'producer_byte_rate=20971520,consumer_byte_rate=20971520'

3.配置clients

broker内全部clientId累加总和最大producer生产速率为20MB/sec

bin/kafka-configs.sh --zookeeper localhost:2181/kafkacluster --alter --entity-type clients --entity-default  --add-config 'producer_byte_rate=20971520'

broker内clientA的最大producer生产速率为20MB/sec

bin/kafka-configs.sh --zookeeper localhost:2181/kafkacluster --alter --entity-type clients  --entity-name clientA  --add-config 'producer_byte_rate=20971520'

超出限流存在的问题

若是producer和consumer超出了流量限制,kafka会怎么处理呢?

  • 对于Producer。若是Producer超出限流,先把数据append到log文件,再计算延时时间,并在等待ThrottleTime时间后响应给Producer。kafka没有客户端反馈机制,因此producer写入超时会重发,写入消息会重复。
  • 对于Consumer。若是Consumer超出限流,先计算延时时间,并在等待ThrottleTime时间后,Kafka从log读取数据并响应Consumer。若是consumer的QequestTimeout < ThrottleTime,则consumer在ThrottleTime时间内会不断重发fetch请求,kafka会堆积大量无效请求,占用资源。

Brokers类型配置

 brokers配置比较复杂,配置项众多,Kafka内部把brokers配置分为7个模块,具体以下表格所示:

 

 

 

 

brokers类型配置并不支持全部的配置项,例如:Broker升级相关协议和group、zookeeper、内置Transaction、Controlled、内置offset相关就不能动态更改。配置brokers只能指定--bootstrap-server,zk不支持

增长配置项

bin/kafka-configs.sh --bootstrap-server localhost:9092 --alter --entity-type brokers --entity-default  --add-config 'max.connections.per.ip=200,max.connections.per.ip.overrides=[ip1:100,ip2:120]' 

bin/kafka-configs.sh --bootstrap-server localhost:9092--alter --entity-type brokers --entity-name  $brokerId --add-config 'max.connections.per.ip=200,max.connections.per.ip.overrides=[ip1:100]' 

删除配置项

bin/kafka-configs.sh --bootstrap-server localhost:9092 --alter --entity-type brokers --entity-default  --delete-config  'max.connections.per.ip,max.connections.per.ip.overrides' 

bin/kafka-configs.sh --bootstrap-server localhost:9092 --alter --entity-type brokers --entity-name  $brokerId --delete-config  'max.connections.per.ip,max.connections.per.ip.overrides' 

列出配置描述

bin/kafka-configs.sh --bootstrap-server localhost:9092 --entity-type brokers --entity-default --describe

bin/kafka-configs.sh --bootstrap-server localhost:9092 --entity-type brokers --entity-name $brokerId --describe

Topics类型配置

Topics类型配置是Brokers类型配置的子集,Brokers类型包含Topics类型全部配置,brokers只是在topics配置项前加了前缀。还有一个特例区别是参数message.format.version在brokers动态配置暂时是不支持的

 

增长配置项

bin/kafka-configs.sh --zookeeper localhost:2181/kafkacluster --alter --entity-type topics --entity-name test-cqy --add-config 'max.message.bytes=50000000,flush.messages=50000,flush.ms=5000'

删除配置项

bin/kafka-configs.sh --zookeeper localhost:2181/kafkacluster --alter --entity-type topics --entity-name test-cqy --delete-config 'max.message.bytes,flush.messages,flush.ms'

列出配置描述

bin/kafka-configs.sh --zookeeper localhost:2181/kafkacluster --entity-type topics --entity-name test-cqy --describe

Brokers配额限流

broker之间复制数据配额限流

 

Kafka提供一个broker之间复制传输的流量限制功能,限制了partitions数据复制从一个broker节点到另外一个broker节点的带宽上限。当从新平衡集群,引导新broker添加或移除老的broker方便颇有用。配置注意事项以下:

  • topic是DynamicReplicationQuota限流的载体,只有做用于具体topic,配额限流才有效
  • leader|follower.xxx.throttled.replicas与leader|follower.xxx.throttled.rate同时都配置才有效
  • 配置只对xxx.throttled.replicas范围限流,其余topics不作限流处理

Kafka复制配额限流仍是挺灵活的,用两个参数做rate和replicas前置限制,保证只对配置topics才有效。例如某个场景一个集群扩容增长broker,须要对制定topics进行迁移分摊IO压力,若是全部topics都限流了,就会对正常运行业务形成影响。

设置xxx.throttled.rate的语法(只能设置brokerId,设置--entity-default无效)

bin/kafka-configs.sh --zookeeper localhost:2181/kafkacluster --alter --entity-type brokers --entity-name $brokerId  --add-config  'leader.replication.throttled.rate=10485760' 

配额流量2两种方式

方式1

bin/kafka-configs.sh --bootstrap-server localhost:9092 --entity-type brokers --entity-name 105 --alter --add-config 'leader.replication.throttled.rate=10485760,follower.replication.throttled.rate=10485760'

bin/kafka-configs.sh --zookeeper localhost:2181/kafkacluster  --entity-type topics --entity-name topicA --alter --add-config 'leader.replication.throttled.replicas=*,follower.replication.throttled.replicas=*'

方式2

用reassign脚本设置leader&follower.xxx.throttled.rate限流,在操做partitions迁移时同时设置限流,避免IO过大网卡打爆,下面throttle实际最终生成leader&follower.xxx.throttled.rate=31457280。reassign在底层仍是调用kafka-configs.sh的API实现,逐个设置move.json覆盖到的brokers进行配置

bin/kafka-reassign-partitions.sh --zookeeper localhost:2181/kafkacluster --reassignment-json-file move.json --throttle 31457280 --execute

当上面partitions数据迁移完成时,执行如下脚本,删除--throttle参数配置

bin/kafka-reassign-partitions.sh --zookeeper localhost:2181/kafkacluster  --reassignment-json-file reassign.json --verify

fetchRequest和fetchResponse两者做用:

  • fetchRequest向leader发起复制请求,用于follower.replication.throttled.rate限流,当follower请求流量大于阀值时,不容许限制topics发送本次fetchRequest请求,下次请求未达到阀值能够成功发送
  • leader向follower响应内容fetchResponse,用于leader.replication.throttled.rate限流,当leader响应流量大于阀值时,不容许限制topics应答fetchResponse响应,下次应答未达到阀值能够成功响应

broker内partitions目录数据迁移配额限流

为何有目录迁移呢?主要缘由是随着硬件高速发展,CPU性能大幅提高,一台物理机会挂载多块磁盘,并且集群扩容可能也会加入不一样型号机型,挂载数量和性能也有差别,因此kafka提供了broker内部目录之间迁移数据流量限制功能,限制数据拷贝从一个目录到另一个目录带宽上限。经常使用于broker内挂载点间partitions数量数据均衡和下降IO压力

 

当在目前间迁移数据时,会设置具体的partitions,这些partitions就是限流的载体。具体操做以下:

具体broker内partitions迁移脚本用法,请查看partitions目录数据迁移

bin/kafka-configs.sh --bootstrap-server localhost:9092 --entity-type brokers --entity-name 105 --alter --add-config 'replica.alter.log.dirs.io.max.bytes.per.second=104857600'

目录数据迁移被设置为独立的FutureLocalReplica角色,不受broker间复制配额限流功能影响

总结

  • 文章开头列出了kafka-configs.sh详尽的语法格式,便于读者阅读使用
  • 客户端配额限流3种粒度配置生成8种优先级组合
  • Brokers类型7个配置模块有2种优先级,除了DynamicReplicationQuota仅只是brokerId局部做用域,其余模块均可以用于全局做用域
  • broker之间复制限流须要2种类型组合配置实现,分别为brokers和topics
  • broker内partitions目录迁移限流,须要kafka-reassign-partitions.sh脚本结合,配置具体partitions的replicas及迁移目录
相关文章
相关标签/搜索