腳本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及遷移目錄
相關文章
相關標籤/搜索