腳本kafka-configs.sh用法解析


引用博客來自李志濤:https://www.cnblogs.com/lizherui/p/12275193.html

前言介紹

網絡上針對腳本kafka-configs.sh用法,也有一些各種文章,但都不系統不全面,介紹的內容是有缺失的,總讓人看起來很懂,用起來難,例如:動態配置內部關系不清晰、有些重點配置參數主從同步配額限流也沒有解釋清楚,除非去看代碼。所以我希望讀者通過深入閱讀此文,更便捷利用此腳本解決實際運維和開發中遇到的問題,同時也為大家節省學習時間。

腳本語法解析

kafka-configs.sh參數解析

語法格式

增加配置項

某個topic配置對象

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' 

例子

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'

刪除配置項

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及遷移目錄

 引用博客來自李志濤:https://www.cnblogs.com/lizherui/p/12275193.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM