文章
Kafka 单节点 kraft 环境搭建
目录规划
cd /kafka # 创建根目录
# 将kafka安装包(kafka_2.13-3.9.1.tgz)拷贝到/kafka
cp /your/file/path/kafka_2.13-3.9.1.tgz /kafka/
tar -zxvf kafka_2.13-3.9.1.tgz # 解压安装包
环境依赖
dnf install -y java-17-openjdk # 安装OpenJDK17
用户设置
sudo useradd -r -m -d /kafka -s /bin/bash kafka # 创建系统用户(-r参数)
sudo chown -R kafka:kafka /kafka # 授权目录
Kafka配置
编辑配置文件:/kafka/kafka_2.13-3.9.1/config/kraft/server.properties
# 监听端点配置
listeners=PLAINTEXT://:9092,PLAINTEXT_INTERNAL://172.17.90.111:9094,CONTROLLER://:9093
# 服务端点公告
advertised.listeners=PLAINTEXT://localhost:9092,PLAINTEXT_INTERNAL://172.17.90.111:9094,CONTROLLER://localhost:9093
# 安全协议映射
listener.security.protocol.map=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT,PLAINTEXT_INTERNAL:PLAINTEXT,PLAINTEXT_PUBIC:PLAINTEXT,SSL:SSL,SASL_PLAINTEXT:SASL_PLAINTEXT,SASL_SSL:SASL_SSL
# 日志存储目录
log.dirs=/kafka/kafka_2.13-3.9.1/kraft-combined-logs
注:IP地址请根据网卡对应的IP进行修改
KRaft模式初始化
运行脚本
cd /kafka/kafka_2.13-3.9.1
./bin/kafka-storage.sh random-uuid # 生成UUID (示例: fWDamvz8T0-dLH0IFQP2Wg)
# 格式化存储
./bin/kafka-storage.sh format --standalone \
-t fWDamvz8T0-dLH0IFQP2Wg \
-c config/kraft/server.properties
测试启动
# 启动服务
sudo -u kafka bin/kafka-server-start.sh config/kraft/server.properties
# 停止服务
sudo -u kafka bin/kafka-server-stop.sh
系统服务配置
创建服务文件:/etc/systemd/system/kafka.service
[Unit]
Description=Apache Kafka Server
After=network.target
[Service]
User=kafka
Group=kafka
ExecStart=/kafka/kafka_2.13-3.9.1/bin/kafka-server-start.sh /kafka/kafka_2.13-3.9.1/config/kraft/server.properties
ExecStop=/kafka/kafka_2.13-3.9.1/bin/kafka-server-stop.sh
Restart=always
RestartSec=5
LimitNOFILE=1000000
[Install]
WantedBy=multi-user.target
关闭SELinux (可选)
# 检查SELinux状态
sudo vim /etc/selinux/config
# 修改以下参数后重启生效
SELINUX=disabled
启动服务
sudo systemctl daemon-reload
sudo systemctl enable kafka
sudo systemctl start kafka