kafka KRaft集群安装
包下载
https://downloads.apache.org/kafka/3.6.1/kafka_2.13-3.6.1.tgz
kafka集群构建好后的数据目录结构
[root@localhost data]# tree /data/kafka
/data/kafka
├── kafka-1 # 节点1源码目录
├── kafka-2 # 节点2源码目录
├── kafka-3 # 节点3源码目录
└── kafkadata # kafka数据存放目录
├── kafkadata1 # 节点1数据存放目录
├── kafkadata2 # 节点2数据存放目录
└── kafkadata3 # 节点3数据存放目录
更改kafka配置文件
kafka节点1配置文件
[root@localhost kraft]# cat cat /data/kafka/kafka-1/config/kraft/server.properties |grep -Ev "#|^$"
# 表示kafka的KRaft模式
process.roles=broker,controller
# 集群节点的标记
node.id=1
# 参与集群投票节点
controller.quorum.voters=1@localhost:19093,2@localhost:29093,3@localhost:39093
# 定义监听地址
listeners=PLAINTEXT://:19092,CONTROLLER://:19093
inter.broker.listener.name=PLAINTEXT
# 对外宣告地址
advertised.listeners=PLAINTEXT://localhost:19092
controller.listener.names=CONTROLLER
listener.security.protocol.map=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT,SSL:SSL,SASL_PLAINTEXT:SASL_PLAINTEXT,SASL_SSL:SASL_SSL
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/tmp/kraft-combined-logs
num.partitions=1
num.recovery.threads.per.data.dir=1
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
kafka节点2配置文件
[root@localhost kafka]# cat /data/kafka/kafka-2/config/kraft/server.properties |grep -Ev "#|^$"
process.roles=broker,controller
node.id=2
controller.quorum.voters=1@localhost:19093,2@localhost:29093,3@localhost:39093
listeners=PLAINTEXT://:29092,CONTROLLER://:29093
inter.broker.listener.name=PLAINTEXT
advertised.listeners=PLAINTEXT://localhost:29092
controller.listener.names=CONTROLLER
listener.security.protocol.map=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT,SSL:SSL,SASL_PLAINTEXT:SASL_PLAINTEXT,SASL_SSL:SASL_SSL
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/data/kafka/kafkadata/kafkadata2
num.partitions=1
num.recovery.threads.per.data.dir=1
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
kafka节点3配置文件
[root@localhost kafka]# cat /data/kafka/kafka-3/config/kraft/server.properties |grep -Ev "#|^$"
process.roles=broker,controller
node.id=3
controller.quorum.voters=1@localhost:19093,2@localhost:29093,3@localhost:39093
listeners=PLAINTEXT://:39092,CONTROLLER://:39093
inter.broker.listener.name=PLAINTEXT
advertised.listeners=PLAINTEXT://localhost:39092
controller.listener.names=CONTROLLER
listener.security.protocol.map=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT,SSL:SSL,SASL_PLAINTEXT:SASL_PLAINTEXT,SASL_SSL:SASL_SSL
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/data/kafka/kafkadata/kafkadata3
num.partitions=1
num.recovery.threads.per.data.dir=1
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
获取集群uuid
[root@localhost kafka]# /data/kafka/kafka-1/bin/kafka-storage.sh random-uuid
fzSBf0PjTRi3zNH_0Abc-g
格式化kafka数据存储目录
/data/kafka/kafka-1/bin/kafka-storage.sh format -t fzSBf0PjTRi3zNH_0Abc-g -c /data/kafka/kafka-1/config/kraft/server.properties
/data/kafka/kafka-2/bin/kafka-storage.sh format -t fzSBf0PjTRi3zNH_0Abc-g -c /data/kafka/kafka-2/config/kraft/server.properties
/data/kafka/kafka-3/bin/kafka-storage.sh format -t fzSBf0PjTRi3zNH_0Abc-g -c /data/kafka/kafka-3/config/kraft/server.properties
启动kafka
nohup /data/kafka/kafka-1/bin/kafka-server-start.sh /data/kafka/kafka-1/config/kraft/server.properties >> /data/kafka/kafkadata/kafka-1.log &
nohup /data/kafka/kafka-2/bin/kafka-server-start.sh /data/kafka/kafka-2/config/kraft/server.properties >> /data/kafka/kafkadata/kafka-2.log &
nohup /data/kafka/kafka-3/bin/kafka-server-start.sh /data/kafka/kafka-3/config/kraft/server.properties >> /data/kafka/kafkadata/kafka-3.log &
创建主题,3个分区,3个副本
/data/kafka/kafka-3/bin/kafka-topics.sh --create --topic test-topic --bootstrap-server localhost:39092 --replication-factor 3 --partitions 3
列出主题,查看主题是否创建
/data/kafka/kafka-3/bin/kafka-topics.sh --list --bootstrap-server localhost:39092
生产消息
/data/kafka/kafka-3/bin/kafka-console-producer.sh --topic test-topic --bootstrap-server localhost:39092
消费消息
/data/kafka/kafka-3/bin/kafka-console-consumer.sh --topic test-topic --from-beginning --bootstrap-server localhost:39092
检查集群脚本状态
/data/kafka/kafka-3/bin/kafka-broker-api-versions.sh --bootstrap-server localhost:39092
集群的性能测试
生产者性能测试
/data/kafka/kafka-3/bin/kafka-producer-perf-test.sh --topic test-topic --num-records 50000 --record-size 1000 --throughput -1 --producer-props bootstrap.servers=localhost:39092
消费者性能测试
/data/kafka/kafka-3/bin/kafka-consumer-perf-test.sh --topic test-topic --bootstrap-server localhost:39092 --fetch-size 1048576 --messages 50000 --threads 1