Kraft模式下Kafka脚本的使用

发布时间 2023-10-19 08:50:18作者: 付同學

Kafka集群 版本:V3.5.1

名称 Node1 Node2 Node3
IP 172.29.145.157 172.29.145.182 172.29.145.183

(1)查看Kraft集群中的状态以及Leader节点,投票节点
使用--status可以查看集群选举次数/水位线以及投票节点等
使用--replication可以查看Ledaer和Follower分布

使用kafka-metadata-quorum.sh

./kafka-metadata-quorum.sh --bootstrap-server 172.29.145.157:9092,172.29.145.182:9092,172.29.145.183:9092 describe --replication
image

./kafka-metadata-quorum.sh --bootstrap-server 172.29.145.157:9092,172.29.145.182:9092,172.29.145.183:9092 describe --status
image

(2)查看Kraft集群中的Topic
使用--list可以查看所有的topic
使用--describe --topic topicname可以查看指定topic的分区状态和副本同步状态

使用kafka-topics.sh

./kafka-topics.sh --list --bootstrap-server 172.29.145.157:9092,172.29.145.182:9092,172.29.145.183:9092
image

./kafka-topics.sh --describe --bootstrap-server 172.29.145.157:9092,172.29.145.182:9092,172.29.145.183:9092 --topic test-topic1
image

(3)启动与关闭Kraft集群
启动集群时一定需要添加--daemon参数以后台守护进程运行

使用kafka-server-start.sh
使用kafka-server-stop.sh

./kafka-server-start.sh -daemon ../config/kraft/server.properties
启动后可以使用jps来查看kafka集群是否启动成功
image

关闭集群也需要加上参数server.properties
./kafka-server-stop.sh ../config/kraft/server.properties

(4)测试集群性能

使用kafka-producer-perf-test.sh生产数据
使用kafka-consumer-perf-test.sh消费数据

./kafka-producer-perf-test.sh --producer-props bootstrap.servers=172.29.145.157:9092,172.29.145.182:9092,172.29.145.183:9092 --topic test-topic1 --num-records 1000000 --record-size 4096 --throughput 50000

--topic指定topic
--num-records指定生产者产生的消息数量
--record-size指定一条消息的大小KB为单位
--throughput指定生产者每秒写入的消息数量限制(吞吐量),-1则为不限制

image
反馈的指标是生产者发送了一百万条消息,每秒生产消息15377条(生产速率60MB/s),平均时延394毫秒,后续就是各种时延的分布范围

./kafka-consumer-perf-test.sh --topic test-topic1 --messages 1000000 --fetch-size 40000 --broker-list 172.29.145.157:9092,172.29.145.182:9092,172.29.145.183:9092

使用--topic指定topic
使用--messages指定需要消费的消息数量
使用--fetch-size指定一次获取的消息总大小
使用broker-list来指定消费的broker

image
反馈的指标是 Kafka集群消费100万条消息用时23秒,消息总大小3906MB,平均每秒消费速率168MB,消息总数量1000015条,每秒消费消息数量43185条

(5)命令行验证生产实时消费

使用kafka-console-producer.sh实时生产消息
使用kafka-console-consumer.sh实时消费消息

./kafka-console-producer.sh --bootstrap-server 172.29.145.157:9092,172.29.145.182:9092,172.29.145.183:9092 --topic test-topic2

image

./kafka-console-consumer.sh --bootstrap-server 172.29.145.157:9092,172.29.145.182:9092,172.29.145.183:9092 --topic test-topic2

image

同时在Kafka管控平台上也能够实时查询到test-topic2上的消息
image

同时我们也可以指定offset来使消费者从指定offset开始消费,对于生产环境有利于故障恢复
./kafka-console-consumer.sh --bootstrap-server 172.29.145.157:9092,172.29.145.182:9092,172.29.145.183:9092 --topic test-topic2 --partition 1 --offset 2
可以看到由于输入的消息key相同所以消息都分到了partition1上,所以在消费消息时需要指定分区partition1,然后指定offset消息位移量2,就可以读取到offset=2对应的消息3以及之后的所有消息了

image

(6)查询消费者组信息

使用kafka-consumer-groups.sh

./kafka-consumer-groups.sh --bootstrap-server 172.29.145.157:9092,172.29.145.182:9092,172.29.145.183:9092 --list

image

./kafka-consumer-groups.sh --bootstrap-server 172.29.145.157:9092,172.29.145.182:9092,172.29.145.183:9092 --group perf-consumer-91301 --describe
通过--list查询到消费者组列表再通过--describe查询具体信息

image

可以看到在test-topic1主题上的partition5和partition0的Lag为18,代表着两个分区还有18条消息没有消费,通过kafka-console-consumer.sh尝试消费partition5的剩余18条消息
./kafka-console-consumer.sh --bootstrap-server 172.29.145.157:9092,172.29.145.182:9092,172.29.145.183:9092 --topic test-topic1 --group perf-consumer-91301

ps:很多很多消息,因为输入的时候一条消息的大小由--record-size决定,设置为4096KB...
此时再通过describe查看消费者组情况看在partition5和partition0上是否还有消息Lag

image
可以看到消息都消费完了,Lag也已为0,再执行相同的命令只会等待而不会继续输出消息