news 2026/10/21 2:22:56

centos 8 使用 kafka_2.13-4.3.1.tgz 安装kafka

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
centos 8 使用 kafka_2.13-4.3.1.tgz 安装kafka

在 CentOS 8 上安装 Kafka 4.3.1(kafka_2.13-4.3.1.tgz)需要特别注意,因为从 Kafka 3.3.x 开始,官方已逐步弃用 ZooKeeper,并在后续版本中全面转向 ‌KRaft(Kafka Raft)‌ 模式。Kafka 4.x 版本默认且推荐以 KRaft 模式运行,不再依赖独立的 ZooKeeper 服务。

以下是基于 CentOS 8 环境的完整安装与配置指南:

1. 环境准备:安装 Java 17

Kafka 4.x 版本要求 ‌Java 17‌ 或更高版本。CentOS 8 默认的 OpenJDK 可能是 1.8 或 11,需升级。

# 安装 Java 17 sudo yum install java-17-openjdk java-17-openjdk-devel -y # 验证版本 java -version # 应显示 openjdk version "17.0.x"

注意‌:若系统存在多个 Java 版本,请确保JAVA_HOME环境变量指向 Java 17。

export JAVA_HOME=/usr/lib/jvm/java-17-openjdk export PATH=$JAVA_HOME/bin:$PATH

2. 下载与解压 Kafka

假设你已将kafka_2.13-4.3.1.tgz上传至服务器,或直接从 Apache 镜像站下载。

# 创建安装目录 sudo mkdir -p /opt/kafka cd /opt/kafka # 解压安装包 sudo tar -xzf kafka_2.13-4.3.1.tgz # 进入解压后的目录(目录名可能为 kafka_2.13-4.3.1) cd kafka_2.13-4.3.1

3. 配置 KRaft 模式(无 ZooKeeper)

Kafka 4.3.1 使用 KRaft 模式,需修改配置文件并格式化存储目录。

3.1 生成集群 UUID

每个 Kafka 集群需要一个唯一的 ID。

KAFKA_CLUSTER_ID="$(bin/kafka-storage.sh random-uuid)" echo $KAFKA_CLUSTER_ID # 记录此 ID,后续步骤会用到
3.2 修改配置文件

编辑config/kraft/server.properties(注意:不是server.properties,而是kraft/子目录下的文件)。

vim config/server.properties

关键配置项修改如下:

# Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You under the Apache License, Version 2.0 # (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. ############################# Server Basics ############################# # The role of this server. Setting this puts us in KRaft mode process.roles=broker,controller # The node id associated with this instance's roles node.id=1 # List of controller endpoints used connect to the controller cluster controller.quorum.bootstrap.servers=192.168.88.9:9093 ############################# Socket Server Settings ############################# # The address the socket server listens on. # Combined nodes (i.e. those with `process.roles=broker,controller`) must list the controller listener here at a minimum. # If the broker listener is not defined, the default listener will use a host name that is equal to the value of java.net.InetAddress.getCanonicalHostName(), # with PLAINTEXT listener name, and port 9092. # FORMAT: # listeners = listener_name://host_name:port # EXAMPLE: # listeners = PLAINTEXT://your.host.name:9092 listeners=PLAINTEXT://0.0.0.0:9092,CONTROLLER://0.0.0.0:9093 # Name of listener used for communication between brokers. inter.broker.listener.name=PLAINTEXT # Listener name, hostname and port the broker or the controller will advertise to clients. # If not set, it uses the value for "listeners". advertised.listeners=PLAINTEXT://192.168.88.9:9092,CONTROLLER://192.168.88.9:9093 # A comma-separated list of the names of the listeners used by the controller. # If no explicit mapping set in `listener.security.protocol.map`, default will be using PLAINTEXT protocol # This is required if running in KRaft mode. controller.listener.names=CONTROLLER # Maps listener names to security protocols, the default is for them to be the same. See the config documentation for more details listener.security.protocol.map=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT,SSL:SSL,SASL_PLAINTEXT:SASL_PLAINTEXT,SASL_SSL:SASL_SSL # The number of threads that the server uses for receiving requests from the network and sending responses to the network num.network.threads=3 # The number of threads that the server uses for processing requests, which may include disk I/O num.io.threads=8 # The send buffer (SO_SNDBUF) used by the socket server socket.send.buffer.bytes=102400 # The receive buffer (SO_RCVBUF) used by the socket server socket.receive.buffer.bytes=102400 # The maximum size of a request that the socket server will accept (protection against OOM) socket.request.max.bytes=104857600 ############################# Log Basics ############################# # A comma separated list of directories under which to store log files log.dirs=/app/kafka/kraft-combined-logs # The default number of log partitions per topic. More partitions allow greater # parallelism for consumption, but this will also result in more files across # the brokers. num.partitions=1 # The number of threads per data directory to be used for log recovery at startup and flushing at shutdown. # This value is recommended to be increased based on the installation resources. num.recovery.threads.per.data.dir=2 ############################# Internal Topic Settings ############################# # The replication factor for the group metadata internal topics "__consumer_offsets", "__share_group_state" and "__transaction_state" # For anything other than development testing, a value greater than 1 is recommended to ensure availability such as 3. offsets.topic.replication.factor=1 share.coordinator.state.topic.replication.factor=1 share.coordinator.state.topic.min.isr=1 transaction.state.log.replication.factor=1 transaction.state.log.min.isr=1 ############################# Log Flush Policy ############################# # Messages are immediately written to the filesystem but by default we only fsync() to sync # the OS cache lazily. The following configurations control the flush of data to disk. # There are a few important trade-offs here: # 1. Durability: Unflushed data may be lost if you are not using replication. # 2. Latency: Very large flush intervals may lead to latency spikes when the flush does occur as there will be a lot of data to flush. # 3. Throughput: The flush is generally the most expensive operation, and a small flush interval may lead to excessive seeks. # The settings below allow one to configure the flush policy to flush data after a period of time or # every N messages (or both). This can be done globally and overridden on a per-topic basis. # The number of messages to accept before forcing a flush of data to disk #log.flush.interval.messages=10000 # The maximum amount of time a message can sit in a log before we force a flush #log.flush.interval.ms=1000 ############################# Log Retention Policy ############################# # The following configurations control the disposal of log segments. The policy can # be set to delete segments after a period of time, or after a given size has accumulated. # A segment will be deleted whenever *either* of these criteria are met. Deletion always happens # from the end of the log. # The minimum age of a log file to be eligible for deletion due to age log.retention.hours=168 # A size-based retention policy for logs. Segments are pruned from the log unless the remaining # segments drop below log.retention.bytes. Functions independently of log.retention.hours. #log.retention.bytes=1073741824 # The maximum size of a log segment file. When this size is reached a new log segment will be created. log.segment.bytes=1073741824 # The interval at which log segments are checked to see if they can be deleted according # to the retention policies log.retention.check.interval.ms=300000 ~
3.3 格式化存储目录

使用之前生成的KAFKA_CLUSTER_ID格式化日志目录。‌此步骤只需执行一次‌。

bin/kafka-storage.sh format -t $KAFKA_CLUSTER_ID -c config/server.properties --standalone

4. 启动 Kafka

使用 KRaft 模式启动脚本

# 前台启动(用于测试,观察日志) bin/kafka-server-start.sh config/server.properties # 后台守护进程启动(生产环境推荐) bin/kafka-server-start.sh -daemon config/server.properties

5. 验证安装

创建测试主题并发送消息,验证 Kafka 是否正常工作。

# 1. 创建测试主题 bin/kafka-topics.sh --create --topic test-topic --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1 # 2. 列出主题 bin/kafka-topics.sh --list --bootstrap-server localhost:9092 # 3. 启动生产者控制台 bin/kafka-console-producer.sh --topic test-topic --bootstrap-server localhost:9092 # 输入几条测试消息,如: # Hello Kafka 4.3.1 # Test Message # 4. 新开终端窗口,启动消费者控制台 bin/kafka-console-consumer.sh --topic test-topic --from-beginning --bootstrap-server localhost:9092 # 应能看到生产者发送的消息

6. 配置 systemd 服务(可选,推荐)

为方便管理,创建 systemd 服务文件。

sudo vim /etc/systemd/system/kafka.service

内容如下:

[Unit] Description=Apache Kafka Server (KRaft Mode) After=network.target [Service] Type=simple User=root Environment="JAVA_HOME=/usr/lib/jvm/java-17-openjdk" WorkingDirectory=/opt/kafka/kafka_2.13-4.3.1 ExecStart=/opt/kafka/bin/kafka-server-start.sh /opt/kafka/config/server.properties ExecStop=/opt/kafka/bin/kafka-server-stop.sh Restart=on-failure RestartSec=5 [Install] WantedBy=multi-user.target

启用并启动服务:

sudo systemctl daemon-reload sudo systemctl enable kafka sudo systemctl start kafka sudo systemctl status kafka

常见问题排查

  1. ‌Java 版本错误‌:确保java -version输出为 17+,且JAVA_HOME正确设置。
  2. ‌端口冲突‌:检查 9092(Broker)和 9093(Controller)端口是否被占用。
  3. ‌权限问题‌:确保log.dirs指定目录对运行 Kafka 的用户可写。
  4. ‌防火墙‌:若需远程访问,开放相应端口:
    sudo firewall-cmd --permanent --add-port=9092/tcp sudo firewall-cmd --reload

至此,Kafka 4.3.1 已在 CentOS 8 上以 KRaft 模式成功安装并运行。

---------------------------------------------------


如果将advertised.listeners配置为PLAINTEXT://localhost:9092,外部客户端(运行在其他机器上的生产者或消费者)在尝试连接时会失败。

❌ 为什么无法访问?

Kafka 的连接机制分为两步:

  1. ‌初始连接‌:客户端使用你提供的地址(例如 Kafka 服务器的真实 IP)连接到 Broker。
  2. ‌元数据返回‌:Broker 接受连接后,会向客户端返回集群的元数据信息,其中包含advertised.listeners配置的地址。‌客户端后续的所有读写操作都会使用这个返回的地址进行通信。‌

当advertised.listeners=PLAINTEXT://localhost:9092时:

  • 外部客户端成功建立初始连接后,Kafka 会告诉客户端:“请通过localhost:9092与我通信”。
  • 客户端收到指令后,会尝试连接‌它自己本机‌(Client 所在的机器)的localhost:9092。
  • 由于 Kafka 服务并不运行在客户端机器上,连接必然失败,通常报错为Connection to node -1 could not be established或TimeoutException。

正确配置方案

根据客户端与 Kafka 服务器的网络关系,选择以下一种配置:

场景 1:局域网内访问(推荐)

如果客户端和 Kafka 在同一局域网内,使用 Kafka 服务器的‌内网 IP 地址‌。

# 假设 Kafka 服务器内网 IP 为 192.168.1.100 advertised.listeners=PLAINTEXT://192.168.1.100:9092
场景 2:公网/跨网络访问

如果客户端通过公网访问,且服务器有公网 IP(或通过 NAT 映射),使用‌公网 IP 或域名‌。

# 假设公网 IP 为 203.0.113.5,或域名为 kafka.example.com advertised.listeners=PLAINTEXT://203.0.113.5:9092 # 或者 advertised.listeners=PLAINTEXT://kafka.example.com:9092
场景 3:Docker 容器部署

如果 Kafka 运行在 Docker 中,且客户端在宿主机或其他容器:

  • ‌宿主机访问‌:使用host.docker.internal(Docker Desktop)或宿主机的实际 IP。
  • ‌其他容器访问‌:使用 Docker 网络中的服务名或容器 IP。
# docker-compose 示例 environment: KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://host.docker.internal:9092

配套配置检查

修改advertised.listeners时,请确保listeners配置正确,以便 Kafka 能实际接收请求:

# listeners 定义 Kafka 实际监听的网卡接口 # 0.0.0.0 表示监听所有网卡,允许来自任何 IP 的连接请求 listeners=PLAINTEXT://0.0.0.0:9092 # advertised.listeners 定义对外公布的地址 # 必须替换为客户端可访问的真实 IP 或域名 advertised.listeners=PLAINTEXT://<你的服务器IP>:9092

验证方法

  1. 修改配置后重启 Kafka。
  2. 在‌另一台机器‌上使用telnet或nc测试连通性:
telnet <Kafka服务器IP> 9092

3使用 Kafka 客户端工具(如kafka-console-producer.sh)指定该 IP 进行测试:

bin/kafka-console-producer.sh --bootstrap-server <Kafka服务器IP>:9092 --topic test

如果能正常发送消息,说明配置正确。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/10/21 2:22:35

旧电脑变身家庭影音服务器:NAS搭建、硬盘挂载与局域网优化全攻略

抱歉&#xff0c;我这边没有收到有效的项目信息。你填写的项目标题、正文、关键词和摘要描述均为空&#xff0c;我无法凭空判断你要写的领域、主题和核心技术点&#xff0c;硬写只会变成一篇泛泛而谈的空文&#xff0c;这既浪费你的时间&#xff0c;也不符合我做内容一贯的标准…

作者头像 李华
网站建设 2026/10/21 2:22:28

CSS(盒子模型)

1.盒子模型1.1组成以及边框设置1.1.1盒子类型&#xff1a;区块盒子&#xff08;块级盒子&#xff09;和行内盒子&#xff08;内联盒子&#xff09;1.1.1盒子类型区块盒子&#xff08;块级盒子&#xff09;和行内盒子&#xff08;内联盒子&#xff09;注意&#xff1a;区块盒子一…

作者头像 李华
网站建设 2026/10/21 2:22:25

写一半数据库失败怎么办?HarmonyOS RDB 事务回滚实践【鸿蒙心迹】

大家好&#xff0c;我是[晚风依旧似温柔]&#xff0c;新人一枚&#xff0c;欢迎大家关注~ 本文目录&#xff1a;前言一、转账与订单&#xff1a;为什么不能“分两次随便写”二、HarmonyOS RDB 事务核心 API关键接口一览事务的内部控制原则三、构建最小实践场景&#xff1a;主表…

作者头像 李华
网站建设 2026/10/21 2:21:26

AI芯片软硬件协同设计:从算子映射到性能调优的实战指南

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华
网站建设 2026/10/21 2:20:43

机器人竞赛硬件入门:电源、电机、通信与调试避坑指南

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华