在 Debian/Ubuntu 上配置 Apache Kafka 时,主要涉及 Kafka 自身的配置文件 和 系统/环境相关文件。下面按必须改和常见改给你一个清晰清单。
Kafka 的配置文件一般在:
$KAFKA_HOME/config/
常见路径(取决于安装方式):
/opt/kafka/config/
/usr/local/kafka/config/
server.properties(最重要 ✅)这是 Kafka Broker 的核心配置,必须改。
vim config/server.properties
# broker 唯一 ID
broker.id=0
# 监听地址(非常重要)
listeners=PLAINTEXT://0.0.0.0:9092
# 对外暴露地址(客户端连这个)
advertised.listeners=PLAINTEXT://<服务器IP>:9092
# Kafka 数据目录
log.dirs=/var/lib/kafka-logs
# Zookeeper 地址(Kafka 2.x)
zookeeper.connect=localhost:2181
✅ 生产环境一定要改 advertised.listeners
否则客户端连不上。
config/zookeeper.properties
dataDir=/var/lib/zookeeper
clientPort=2181
⚠️ Kafka 3.x 以后可用 KRaft 模式,不再依赖 Zookeeper
/etc/hosts(常见坑)如果 Kafka 使用 hostname:
vim /etc/hosts
127.0.0.1 localhost
<服务器IP> kafka-node1
vim /etc/security/limits.conf
* soft nofile 65536
* hard nofile 65536
vim /etc/sysctl.conf
vm.swappiness=1
net.core.somaxconn=1024
生效:
sysctl -p
如果你用的是 Kafka 3.x 无 Zookeeper:
server.propertiesprocess.roles=broker,controller
node.id=1
controller.quorum.voters=1@localhost:9093
listeners=PLAINTEXT://0.0.0.0:9092,CONTROLLER://localhost:9093
meta.properties(自动生成)/var/lib/kafka-logs/meta.properties
vim /etc/systemd/system/kafka.service
[Unit]
Description=Apache Kafka
After=network.target
[Service]
Type=simple
User=kafka
ExecStart=/opt/kafka/bin/kafka-server-start.sh /opt/kafka/config/server.properties
ExecStop=/opt/kafka/bin/kafka-server-stop.sh
Restart=always
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable kafka
systemctl start kafka
| 类型 | 文件 |
|---|---|
| Kafka 核心 | config/server.properties ✅ |
| Zookeeper | config/zookeeper.properties |
| 系统 | /etc/hosts |
| 性能 | /etc/security/limits.conf |
| 内核 | /etc/sysctl.conf |
| 服务 | /etc/systemd/system/kafka.service |
你可以直接告诉我:
我可以给你 一份可直接用的完整配置示例。