使用
权限管理
/opt/bigdata/app/kafka/bin/kafka-configs.sh --zookeeper zkurl --entity-type topics --entity-name yyj1 --alter --add-config join.isr.wait.seconds=600
# 关于broker间的通信,需要一个admin用户
bin/kafka-configs.sh --zookeeper zkurl --alter --add-config 'SCRAM-SHA-256=[password=xxx],SCRAM-SHA-512=[password=xxx]' --entity-type users --entity-name admin
# kafka-server-start.sh中指定了kafka-server-jaas.conf配置,配置中包含了如下内容:
KafkaServer {
org.apache.kafka.common.security.scram.ScramLoginModule required
username="admin"
password="xxx"
user_admin="xxx"
user_ys="ys-secret";
};
# 查看admin用户信息
bin/kafka-configs.sh --zookeeper zkurl --describe --entity-type users --entity-name admin
# 添加用户ys
kafka-configs.sh --zookeeper zkurl --alter --add-config 'SCRAM-SHA-256=[iterations=8192,password=ys-secret],SCRAM-SHA-512=[password=ys-secret]' --entity-type users --entity-name ys
# 创建topic
bin/kafka-topics.sh --create --zookeeper zkurl --topic test-ys-2 --partitions 1 --replication-factor 3
# topic列表查看
bin/kafka-topics.sh --list --zookeeper zkurl
# 查看topic信息
bin/kafka-topics.sh --describe --zookeeper zkurl --topic test-ys-2
# 为topic添加生产者
bin/kafka-acls.sh --authorizer-properties zookeeper.connect=zkurl --add --allow-principal User:ys --producer --topic test-ys-2
# 创建topic的消费者组、消费者
bin/kafka-acls.sh --authorizer-properties zookeeper.connect=zkurl --add --allow-principal User:ys --consumer --topic test-ys-2 --group test-consumer-group
# 生产者生产数据
bin/kafka-console-producer-ys.sh --broker-list SASL_PLAINTEXT://xx.xx.xx.xx:9092,SASL_PLAINTEXT://xx.xx.xx.xx:9092,SASL_PLAINTEXT://xx.xx.xx.xx:9092 --topic test-ys-2 --producer.config config/producer.properties
# kafka-console-producer-ys.sh中指定了kafka-client-jaas-ys.conf配置文件,配置中包含了如下内容:
KafkaClient {
org.apache.kafka.common.security.scram.ScramLoginModule required
username="ys"
password="ys-secret";
};
# 消费者消费数据
bin/kafka-console-consumer-ys.sh --bootstrap-server SASL_PLAINTEXT://xx.xx.xx.xx:9092,SASL_PLAINTEXT://xx.xx.xx.xx:9092,SASL_PLAINTEXT://xx.xx.xx.xx:9092 --topic test-ys-2 --consumer.config config/consumer.properties
# kafka-console-consumer-ys.sh中指定了kafka-client-jaas-ys.conf配置文件,配置中包含了如下内容:
KafkaClient {
org.apache.kafka.common.security.scram.ScramLoginModule required
username="ys"
password="ys-secret";
};
Kafka监控问题诊断
非同步分区
列出集群的非同步分区
bin/kafka-topics.sh --describe --zookeeper zkurl --under-replicated-partitions
列出没有首领的分区
bin/kafka-topics.sh --describe --zookeeper zkurl --unavailable-partitions
运维Kafka
删除topic
$ bin/kafka-topics.sh --zookeeper zkurl --delete --topic test-replicated-topic
Topic test-replicated-topic is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true.
$ bin/kafka-topics.sh --list --zookeeper zkurl
__consumer_offsets
test
test-replicated-topic - marked for deletion
test-topic-1
test-ys-1
test-ys-2
test2
增加分区
调整基于键的主题:从消费者角度来看,为基于键的主题添加分区是很困难的。因为若改变了分区的数量,键到分区之间的映射也会发生变化。所以,对于基于键的主题来说,建议在一开始就设置好分区数量,避免以后对其进行调整。
$ bin/kafka-topics.sh --zookeeper zkurl --alter --topic my-topic --partitions 16
列出并描述群组
输出结果中的字段说明:
GROUP: 消费者群组的名字;
TOPIC: 正在被读取的主题名字;
PARTITION: 正在被读取的分区ID;
CURRENT-OFFSET: 消费者群组最近提交的偏移量,即消费者在分区里读取的当前位置;
LOG-END-OFFSET: 当前高水位偏移量,即最近一个被读取消息的偏移量,同时也是最近一个被提交到集群的偏移量
LAG: 消费者的CURRENT-OFFSET和broker的LOG-END-OFFSET之间的差距;
OWNER: 消费者群组里正在读取该分区的消费者。这是一个消费者的ID,不一定包含消费者的主机名。
# 列出旧版本的消费者群组
$ bin/kafka-consumer-groups.sh --zookeeper zkurl --list console-consumer-79697 myconsumer
# 列出新版的消费者群组
$ bin/kafka-consumer-groups.sh --bootstrap-server SASL_PLAINTEXT://xx.xx.xx.xx:9092,SASL_PLAINTEXT://xx.xx.xx.xx:9092,SASL_PLAINTEXT://xx.xx.xx.xx:9092 --list --command-config ./config/producer.properties
Consumer group 'test-consumer-group' has no active members.
TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
test-ys-2 0 13 13 0 - - -
# 描述消费者群组
$ bin/kafka-consumer-groups.sh --bootstrap-server SASL_PLAINTEXT://xx.xx.xx.xx:9092,SASL_PLAINTEXT://xx.xx.xx.xx:9092,SASL_PLAINTEXT://xx.xx.xx.xx:9092 --describe --group test-consumer-group --command-config ./config/producer.properties
删除群组
只有旧版本的消费者客户端才支持删除群组的操作。删除群组操作将从Zookeeper上移除整个群组,包括所有已保存的偏移量。在执行该操作前,必须关闭所有的消费者。否则,可能会导致消费者出现不可预测的行为。
# 删除消费者群组
$ bin/kafka-consumer-groups.sh --zookeeper zkurl --delete --group test-consumer-group
# 从消费者群组里删除主题的偏移量
$ bin/kafka-consumer-groups.sh --zookeeper zkurl --delete --group test-consumer-group --topic test-ys-2
偏移量管理
Kafka没有为导出偏移量提供现成的脚本,不过可以使用kafka-run-class.sh脚本调用底层的Java类来实现导出。每个分区在文件里占用一行,格式为:
常用工具
topic分区Leader选主:kafka-preferred-replica-election.sh
介绍
创建topic时,Kafka尽量将partition均分在所有的brokers上,并且将replicas也均分在不同的broker上;
每个partition的所有replicas叫做"assigned replicas",“assigned replicas"中的第一个replicas叫"preferred replica”,刚创建的topic一般"preferred replcia"是leader。leader replica负责所有的读写;
随着时间推移,broker可能会停机,会导致leader迁移…
用法
- 场景1
触发对所有的topic leader进行负载均衡:
kafka-preferred-replica-election.sh --zookeeper zkurl
- 场景2
对某个topic leader触发负载均衡:
kafka-preferred-replica-election.sh --zookeeper zkurl --path-to-json-file xx.json
# 其中xx.json的格式如下
This tool causes leadership for each partition to be transferred back to the 'preferred replica', it can be used to balance leadership among the servers
Option Description
------ -----------
--path-to-json-file <String: list of The JSON file with the list of
partitions for which preferred partitions for which preferred
replica leader election needs to be replica leader election should be
triggered> done, in the following format -
{"partitions":
[{"topic": "foo", "partition": 1},
{"topic": "foobar", "partition": 2}]
}
副本迁移: kafka-reassign-partitions.sh
介绍
用法
- 配置迁移文件
$ vi /root/yyj/topic-reassignment.json
{"version":1,"partitions":[
{"topic":"test","partition":0,"replicas":[1000131,1000124]},
{"topic":"test","partition":1,"replicas":[1000124,1000106]}
]}
- 执行迁移命令
bin/kafka-reassign-partitions.sh --throttle 73400320 --zookeeper zkurl --execute --reassignment-json-file /root/yyj/topic-reassignment.json
- 查看迁移状态/清除限速配置
bin/kafka-reassign-partitions.sh --zookeeper zkurl --verify --reassignment-json-file /root/yyj/topic-reassignment.json
附录
- 参考文章