实验环境
主机名/IP | 角色 | 系统 |
server01/192.168.1.201 | controller | ubuntu22.04 |
server02/192.168.1.202 | controller | ubuntu22.04 |
server03/192.168.1.203 | broker | ubuntu22.04 |
server04/192.168.1.204 | broker | ubuntu22.04 |
All Nodes
安装kafka
apt install openjdk-11-jre-headless -y
wget https://dlcdn.apache.org/kafka/3.8.0/kafka_2.13-3.8.0.tgz
tar -zxvf kafka_2.13-3.8.0.tgz -C /usr/local/etc
server01
修改配置文件
vim /usr/local/etc/kafka_2.13-3.8.0/config/kraft/server.properties
process.roles=controller
node.id=1
controller.quorum.voters=1@192.168.1.201:9093,2@192.168.1.202:9093
listeners=CONTROLLER://192.168.1.201:9093
controller.listener.names=CONTROLLER
listener.security.protocol.map=CONTROLLER:PLAINTEXT
server02
vim /usr/local/etc/kafka_2.13-3.8.0/config/kraft/server.properties
process.roles=controller
node.id=2
controller.quorum.voters=1@192.168.1.201:9093,2@192.168.1.202:9093
listeners=CONTROLLER://192.168.1.202:9093
controller.listener.names=CONTROLLER
listener.security.protocol.map=CONTROLLER:PLAINTEXT
server03
vim /usr/local/etc/kafka_2.13-3.8.0/config/kraft/server.properties
process.roles=broker
node.id=3
controller.quorum.voters=1@192.168.1.201:9093,2@192.168.1.202:9093
listeners=PLAINTEXT://192.168.1.203:9092
inter.broker.listener.name=PLAINTEXT
controller.listener.names=CONTROLLER
listener.security.protocol.map=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
server04
vim /usr/local/etc/kafka_2.13-3.8.0/config/kraft/server.properties
process.roles=broker
node.id=4
controller.quorum.voters=1@192.168.1.201:9093,2@192.168.1.202:9093
listeners=PLAINTEXT://192.168.1.204:9092
inter.broker.listener.name=PLAINTEXT
controller.listener.names=CONTROLLER
listener.security.protocol.map=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
server01
生成uuid
/usr/local/etc/kafka_2.13-3.8.0/bin/kafka-storage.sh random-uuid
# 这个生成的uuid后面要用在其他节点上
viJ79TulTXaN7yG53fdp7w
All Nodes
初始化集群数据目录
/usr/local/etc/kafka_2.13-3.8.0/bin/kafka-storage.sh format -t viJ79TulTXaN7yG53fdp7w -c /usr/local/etc/kafka_2.13-3.8.0/config/kraft/server.properties
/usr/local/etc/kafka_2.13-3.8.0/bin/kafka-server-start.sh /usr/local/etc/kafka_2.13-3.8.0/config/kraft/server.properties
检查节点状态
/usr/local/etc/kafka_2.13-3.8.0/bin/kafka-metadata-quorum.sh --bootstrap-server 192.168.1.203:9092 describe --status
/usr/local/etc/kafka_2.13-3.8.0/bin/kafka-metadata-quorum.sh --bootstrap-server 192.168.1.204:9092 describe --status
ClusterId: viJ79TulTXaN7yG53fdp7w
LeaderId: 1
LeaderEpoch: 2
HighWatermark: 4014
MaxFollowerLag: 0
MaxFollowerLagTimeMs: 0
CurrentVoters: [1,2]
CurrentObservers: [3,4]
输出信息表示:
- ClusterId:集群的ID
- LeaderId:当前的控制器领导者节点的 ID。在这个例子中,领导者节点 ID 是
1
- LeaderEpoch:显示当前领导者的任期号。任期号每次选举领导者时都会增加
- HighWatermark:表示所有副本已确认的最高位移
- MaxFollowerLag:显示最大跟随者的滞后量。这里是
0
,表示跟随者与领导者同步 - MaxFollowerLagTimeMs:显示最大跟随者滞后的时间(ms)
- CurrentVoters:列出当前参与投票的节点 ID
- CurrentObservers:列出当前的观察者节点 ID。观察者不会参与投票,仅用于同步数据