限制數據遷移期間的帶寬使用
Kafka允許您對複製流量應用限制,設置用於將副本從一臺機器移動到另一臺機器的帶寬的上限。這在重新平衡羣集,引導新代理或添加或刪除代理時非常有用,因爲它限制了這些數據密集型操作對用戶的影響。
有兩個接口可用於接合油門。最簡單,最安全的是在調用kafka-reassign-partitions.sh時應用節流,但kafka-configs.sh也可用於直接查看和更改節流值。
因此,例如,如果您要執行重新平衡,使用以下命令,它將以不超過50MB / s的速度移動分區。
1
|
$ bin /kafka-reassign-partitions .sh --zookeeper myhost:2181--execute --reassignment-json- file bigger-cluster.json —throttle 50000000
|
執行此腳本時,您將看到油門參與:
1
2
|
The throttle limit was set to 50000000 B /s
Successfully started reassignment of partitions.
|
如果您希望更改限制,在重新平衡期間,比如增加吞吐量以便更快地完成,您可以通過重新運行執行命令傳遞相同的重新分配-json文件來執行此操作:
1
2
3
|
$ bin /kafka-reassign-partitions .sh --zookeeper localhost:2181 --execute --reassignment-json- file bigger-cluster.json --throttle 700000000
There is an existing assignment running.
The throttle limit was set to 700000000 B /s
|
重新平衡完成後,管理員可以使用--verify選項檢查重新平衡的狀態。如果重新平衡已完成,則將通過--verify命令刪除限制。通過使用--verify選項運行命令,管理員必須在重新平衡完成後及時刪除限制。如果不這樣做,可能會導致定期複製流量受到限制。
執行--verify選項並重新分配完成後,腳本將確認已刪除限制:
1
2
3
4
五
|
> bin /kafka-reassign-partitions .sh --zookeeper localhost:2181 --verify --reassignment-json- file bigger-cluster.json
Status of partition reassignment:
Reassignment of partition [my-topic,1] completed successfully
Reassignment of partition [mytopic,0] completed successfully
Throttle was removed.
|
管理員還可以使用kafka-configs.sh驗證分配的配置。有兩對油門配置用於管理節流過程。油門值本身。這是在代理級別使用動態屬性配置的:
1
2
|
leader.replication.throttled.rate
follower.replication.throttled.rate
|
還有一組枚舉的限制副本:
1
2
|
leader.replication.throttled.replicas
follower.replication.throttled.replicas
|
每個主題配置哪些。所有四個配置值都由kafka-reassign-partitions.sh自動分配(下面討論)。
要查看油門限制配置:
1
2
3
|
> bin /kafka-configs .sh --describe --zookeeper localhost:2181 --entity- type brokers
Configs for brokers '2' are leader.replication.throttled.rate=700000000,follower.replication.throttled.rate=700000000
Configs for brokers '1' are leader.replication.throttled.rate=700000000,follower.replication.throttled.rate=700000000
|
這顯示了應用於複製協議的前導端和跟隨端的節流。默認情況下,爲雙方分配相同的限制吞吐量值。
要查看受限制的副本列表:
1
2
3
|
> bin /kafka-configs .sh --describe --zookeeper localhost:2181 --entity- type topics
Configs for topic 'my-topic' are leader.replication.throttled.replicas=1:102,0:101,
follower.replication.throttled.replicas=1:101,0:102
|
這裏我們看到領導者限制被應用於代理102上的分區1和代理101上的分區0。同樣,跟隨者限制被應用於代理101上的分區1和代理102上的分區0。
默認情況下,kafka-reassign-partitions.sh會將領導者限制應用於重新平衡之前存在的所有副本,其中任何一個可能是領導者。它將跟隨者油門應用於所有移動目的地。因此,如果在代理101,102上存在具有副本的分區,被重新分配給102,103,則該分區的領導者節流將應用於101,102並且跟隨者節流將僅應用於103。
如果需要,您還可以使用kafka-configs.sh上的--alter開關手動更改油門配置。
二,安全使用限制複製
使用限制複製時應該小心。特別是:
(1)節流閥拆卸:
一旦重新分配完成(通過運行kafka-reassign-partitions -verify),應及時刪除油門。
(2)確保進展:
如果將節流閥設置得太低,則與傳入的寫入速率相比,複製可能無法進行。這發生在:
max(BytesInPerSec) > throttle
其中BytesInPerSec是監視生成器到每個代理的寫吞吐量的度量標準。
管理員可以使用度量標準監控重新平衡期間複製是否正在進行中:
kafka.server:type=FetcherLagMetrics,name=ConsumerLag,clientId=([-.\w]+),topic=([-.\w]+),partition=([0-9]+)
複製期間滯後應不斷減少。如果度量標準沒有降低,則管理員應該如上所述增加節流量。
三,設定配額
配額覆蓋和默認值可以在(用戶,客戶端ID),用戶或客戶端ID水平中所述被配置在這裏。默認情況下,客戶端會收到無限制的配額 可以爲每個(用戶,客戶端ID),用戶或客戶端ID組設置自定義配額。
爲(user = user1,client-id = clientA)配置自定義配額:
1
2
|
> bin /kafka-configs .sh --zookeeper localhost:2181 --alter --add-config 'producer_byte_rate=1024,consumer_byte_rate=2048,request_percentage=200' --entity- type users --entity-name user1 --entity- type clients --entity-name clientA
Updated config for entity: user-principal 'user1' , client- id 'clientA' .
|
爲user = user1配置自定義配額:
1
2
|
> bin /kafka-configs .sh --zookeeper localhost:2181 --alter --add-config 'producer_byte_rate=1024,consumer_byte_rate=2048,request_percentage=200' --entity- type users --entity-name user1
Updated config for entity: user-principal 'user1' .
|
爲client-id = clientA配置自定義配額:
1
2
|
> bin /kafka-configs .sh --zookeeper localhost:2181 --alter --add-config 'producer_byte_rate=1024,consumer_byte_rate=2048,request_percentage=200' --entity- type clients --entity-name clientA
Updated config for entity: client- id 'clientA' .
|
可以通過指定--entity-default選項而不是--entity-name爲每個(用戶,客戶端ID),用戶或客戶端ID組設置默認配額。
爲user = userA配置默認客戶端ID配額:
1
2
|
> bin /kafka-configs .sh --zookeeper localhost:2181 --alter --add-config 'producer_byte_rate=1024,consumer_byte_rate=2048,request_percentage=200' --entity- type users --entity-name user1 --entity- type clients --entity-default
Updated config for entity: user-principal 'user1' , default client- id .
|
配置用戶的默認配額:
1
2
|
> bin /kafka-configs .sh --zookeeper localhost:2181 --alter --add-config 'producer_byte_rate=1024,consumer_byte_rate=2048,request_percentage=200' --entity- type users --entity-default
Updated config for entity: default user-principal.
|
配置client-id的默認配額:
1
2
|
> bin /kafka-configs .sh --zookeeper localhost:2181 --alter --add-config 'producer_byte_rate=1024,consumer_byte_rate=2048,request_percentage=200' --entity- type clients --entity-default
Updated config for entity: default client- id .
|
以下是如何描述給定(用戶,客戶端ID)的配額:
1
2
|
> bin /kafka-configs .sh --zookeeper localhost:2181 --describe --entity- type users --entity-name user1 --entity- type clients --entity-name clientA
Configs for user-principal 'user1' , client- id 'clientA' are producer_byte_rate=1024,consumer_byte_rate=2048,request_percentage=200
|
描述給定用戶的配額:
1
2
|
> bin /kafka-configs .sh --zookeeper localhost:2181 --describe --entity- type users --entity-name user1
Configs for user-principal 'user1' are producer_byte_rate=1024,consumer_byte_rate=2048,request_percentage=200
|
Describe quota for a given client-id:
1
2
|
> bin /kafka-configs .sh --zookeeper localhost:2181 --describe --entity- type clients --entity-name clientA
Configs for client- id 'clientA' are producer_byte_rate=1024,consumer_byte_rate=2048,request_percentage=200
|
如果未指定實體名稱,則描述指定類型的所有實體。例如,描述所有用戶:
1
2
3
|
> bin /kafka-configs .sh --zookeeper localhost:2181 --describe --entity- type users
Configs for user-principal 'user1' are producer_byte_rate=1024,consumer_byte_rate=2048,request_percentage=200
Configs for default user-principal are producer_byte_rate=1024,consumer_byte_rate=2048,request_percentage=200
|
類似地(用戶,客戶端):
1
2
3
|
> bin /kafka-configs .sh --zookeeper localhost:2181 --describe --entity- type users --entity- type clients
Configs for user-principal 'user1' , default client- id are producer_byte_rate=1024,consumer_byte_rate=2048,request_percentage=200
Configs for user-principal 'user1' , client- id 'clientA' are producer_byte_rate=1024,consumer_byte_rate=2048,request_percentage=200
|
通過在代理上設置這些配置,可以設置適用於所有客戶端ID的默認配額。僅當在Zookeeper中未配置配額覆蓋或默認值時,才應用這些屬性。默認情況下,每個client-id都會收到無限制的配額。以下設置將每個生產者和使用者客戶端ID的默認配額設置爲10MB /秒。
1
2
|
quota.producer.default=10485760
quota.consumer.default=10485760
|
請注意,這些屬性已被棄用,可能會在將來的版本中刪除。使用kafka-configs.sh配置的默認值優先於這些屬性。