設定 Kafka 配額
Kafka 配額會對產生及耗用要求施行限制,以控制用戶端所使用的分配管理系統資源。 Kafka 配額可讓管理者對個別生產者及消費者應用程式可以使用的網路傳輸量施行限制。
關於 Kafka 配額
如果保持不受限制,則少數消費者或生產者可能會獨佔服務實例的可用網路傳輸量。
Kafka 分配管理系統支援施行速率限制的配額,以防止用戶端使網路飽和或獨佔分配管理系統資源。 如需相關資訊,請參閱
Apache Kafka 說明文件。
Kafka 配額可以配置為限制網路頻寬使用情形,Kafka 會以每秒位元組數來測量此傳輸量。 如果發現 30 秒時間範圍內的傳輸量超出已配置的配額,Kafka 會計算足夠的延遲,以將傳輸量帶入配額限制內。
然後,Kafka 分配管理系統會將延遲資訊作為標準 Kafka 通訊協定回應的一部分傳送至用戶端。 遵循通訊協定合約的合作用戶端在提出新要求之前會等待此延遲; 不合作用戶端可能不會遵循節流控制要求,但在這種情況下,分配管理系統不會讀取該用戶端的要求,直到經歷節流控制延遲為止 (這可能會導致不合作用戶端逾時)。
下列資訊適用於配額:
- 可以為生產者和消費者定義單獨的配額。
- 依預設,用戶端配額無限制。
- 配額是根據分配管理系統而非叢集來套用。
- 配額會套用至共用單一使用者身分的所有用戶端。
- 配額可以套用至「預設」使用者,因此它會套用至任何使用者,即使未設定使用者特定配額也一樣。
用戶端度量
Java 用戶端也會使用下列個別分配管理系統度量來公開節流控制資訊:
- 產生節流控制時間上限
- 產生節流控制時間平均值
- 提取節流控制時間上限
- fetch-throttle-time-avg
設定用戶端配額
IBM® Event Streams for IBM Cloud® 企業方案容許使用 Kafka API 來設定及說明 Kafka V3.1.x 叢集上的配額。 如需相關資訊,請參閱 IBM® Event Streams for IBM Cloud® Admin REST API 的 配額作業 區段。
參照 關於配額的 Kafka 文件,只支援套用至 "user" 實體 (或 "default user") 的傳輸量配額類型 ("producer_byte_rate" 和 "consumer_byte_rate" 配額類型)。
不支援 "client-id" 實體、"request" 和 "controller-mutations" 配額類型作為使用者可設定的配額。 在 Event Streams中,已鑑別使用者身分由 IBM Cloud® Identity and Access Management ID 代表。 因為每個 Cloud Identity and Access Management ID 套用 Kafka 配額,所以 API
金鑰群組可以共用單一配額 (如果這些 API 金鑰都屬於相同的 Cloud Identity and Access Management 服務 ID)。
若要取得 Cloud Identity and Access Management (IAM) 服務 ID 的 Cloud Identity and Access Management ID,可以使用 IBM Cloud CLI。
ibmcloud iam service-id ServiceId-12345678-aaaa-bbbb-cccc-1234567890ab --output json
請參閱下列輸出範例:
{
"active":true,
"jti":"...",
"iam_id":"iam-ServiceId-12345678-aaaa-bbbb-cccc-1234567890ab",
"realmId":"iam",
....
}
將配額對映至 IBM Event Streams Enterprise 叢集
Kafka API 配額是每個分配管理系統的,不過企業方案容量說明為 每個叢集的傳輸量。 因此,如果您想要將使用者總計限制為 10 MB/s,則會將配額 10/n MB/s 套用至每一個分配管理系統 (其中 n 是叢集中分配管理系統的數目)。
若要尋找叢集中分配管理系統的數目,您可以使用 KafkaAdminClient.describeCluster 呼叫。
如需相關資訊,請參閱 Java 說明文件。
也可以使用 Apache Kafka 發行套件中組合的 kafka-configs.sh Shell Script 來找到分配管理系統數目。
bin/kafka-configs.sh --command-config command-config.properties --bootstrap-server "kafka-0.blah.cloud:9093" --describe --entity-type brokers
Event Streams 授權
若要獲授權設定用戶端配額,使用者必須在 Cloud Identity and Access Management中具有 "cluster" 資源的「管理員」角色。
使用實例上具有「管理員」角色的 Event Streams 使用者介面建立的一組認證也具有叢集上的「管理員」角色。 因此,除了設定配額之外,您還可以建立、刪除及變更主題。 任何已鑑別使用者都具有叢集的隱含「讀者」角色,且能夠說明配額。
若要建立一組認證,可管理主題、群組及參與交易,但未授權以設定配額,必須將具有資源類型 "topic"、"group" 及 "txnid" 及 "cluster" 上「讀者」角色之「管理者」角色的 IAM 存取原則與服務 ID 相關聯。
範例: 使用 the kafka-config.sh Script 管理配額 (Apache 用戶端)
-
下載 Kafka 二進位 發行套件 (至少 V3.1.0)。
-
建立內容檔 (在下列指令行範例中名為 command-config.properties ),其中包含下列項目 (將 "myapikey" 取代為實際 API 金鑰)。
sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="token" password="myapikey";
security.protocol=SASL_SSL
sasl.mechanism=PLAIN
ssl.protocol=TLSv1.2
ssl.enabled.protocols=TLSv1.2
ssl.endpoint.identification.algorithm=HTTPS
如需相關資訊,請參閱配置 Kafka API 用戶端。
-
使用下列用法範例。
bin/kafka-configs.sh --command-config command-config.properties --bootstrap-server "kafka-0.blah.cloud:9093" --alter --add-config 'producer_byte_rate=1024,
consumer_byte_rate=2048' --entity-type users --entity-name iam-ServiceId-12345678-aaaa-bbbb-cccc-1234567890ab
已完成更新使用者 iam-ServiceId-12345678-aaaa-bbbb-cccc-1234567890ab 的配置。
bin/kafka-configs.sh --command-config command-config.properties --bootstrap-server "kafka-0.blah.cloud:9093" --describe --entity-type users --entity-name
iam-ServiceId-12345678-aaaa-bbbb-cccc-1234567890ab
使用者主體 iam-ServiceId-12345678-aaaa-bbbb-cccc-1234567890ab 的配額配置為 consumer_byte_rate=2048.0 和 producer_byte_rate=1024.0。
bin/kafka-configs.sh --command-config command-config.properties --bootstrap-server "kafka-0.blah.cloud:9093" --alter --delete-config 'producer_byte_rate,consumer_byte_rate' --entity-type users --entity-name iam-ServiceId-12345678-aaaa-bbbb-cccc-1234567890ab
已完成更新使用者 iam-ServiceId-12345678-aaaa-bbbb-cccc-1234567890ab 的配置。
- 說明已設定給任何使用者的所有配額,包括預設使用者:
bin/kafka-configs.sh --command-config command-config.properties --bootstrap-server "kafka-0.blah.cloud:9093" --describe --entity-type users
bin/kafka-configs.sh --command-config command-config.properties --bootstrap-server "kafka-0.blah.cloud:9093" --alter --add-config 'producer_byte_rate=1024,
consumer_byte_rate=2048' --entity-type users --entity-default
已完成更新叢集中使用者的預設配置。
bin/kafka-configs.sh --command-config command-config.properties --bootstrap-server "kafka-0.blah.cloud:9093" --alter --delete-config 'producer_byte_rate,
consumer_byte_rate' --entity-type users --entity-default
已完成更新叢集中使用者的預設配置。
範例: 透過 Java API 變更傳輸量配額使用情形
下列範例是簡短範例 Snippet,顯示如何呼叫 KafkaAdminClient.alterclientQuotas 方法。
https://kafka.apache.org/32/javadoc/org/apache/kafka/clients/admin/KafkaAdminClient.html
Options)
import java.util.*;
import org.apache.kafka.clients.CommonClientConfigs;
import org.apache.kafka.clients.admin.*;
import org.apache.kafka.common.config.*;
import org.apache.kafka.common.quota.*;
class Snippet {
public static void main(String[] args) throws Exception {
// Kafka client configuration properties.
String mybootstrap = "..."; // from the bootstrap_endpoints of the service credentials
String myapikey = "..."; // from the apikey of the service credentials
Properties properties = new Properties();
properties.put(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, mybootstrap);
properties.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, "sasl_ssl");
properties.put(SslConfigs.SSL_ENABLED_PROTOCOLS_CONFIG, "TLSv1.2");
properties.put(SslConfigs.SSL_PROTOCOL_CONFIG, "TLSv1.2");
properties.put(SaslConfigs.SASL_MECHANISM, "PLAIN");
properties.put(SaslConfigs.SASL_JAAS_CONFIG,
"org.apache.kafka.common.security.plain.PlainLoginModule " +
"required username=\"token\" password=\"" + myapikey + "\";");
AdminClient admin = AdminClient.create(properties);
String iamID = "iam-ServiceId-12345678-aaaa-bbbb-cccc-1234567890ab"; //set iam id of target user to set quotas to
// if null is used instead of the iamID string, the following quota alteration will be applied to the default user
// add quotas
ClientQuotaEntity entity = new ClientQuotaEntity(
Collections.singletonMap(ClientQuotaEntity.USER, iamID));
ClientQuotaAlteration alteration = new ClientQuotaAlteration(entity,
Arrays.asList(new ClientQuotaAlteration.Op("consumer_byte_rate", 1000.0),
new ClientQuotaAlteration.Op("producer_byte_rate", 1000.0)));
admin.alterClientQuotas(Arrays.asList(alteration)).all().get();
// describe quotas
DescribeClientQuotasResult describeClientQuotasFuture = admin.describeClientQuotas(ClientQuotaFilter.all());
System.out.println(describeClientQuotasFuture.entities().get());
//remove quotas (set them to null)
entity = new ClientQuotaEntity(Collections.singletonMap(ClientQuotaEntity.USER, iamID));
alteration = new ClientQuotaAlteration(entity,
Arrays.asList(new ClientQuotaAlteration.Op("consumer_byte_rate", null),
new ClientQuotaAlteration.Op("producer_byte_rate", null)));
admin.alterClientQuotas(Arrays.asList(alteration)).all().get();
}
}
IBM Cloud Activity Tracker 事件
每當更新傳輸量配額時,即會產生 Event Streams 配置事件,可在 IBM Cloud® Activity Tracker中監視。
如需配置 Event Streams的 Activity Tracker 事件的相關資訊,請參閱 Activity Tracker 文件。