在 Debian 上调 Kafka 参数,通常分为 修改 Kafka 配置文件、系统/内核参数优化、以及 运行时通过命令行/Admin API 调整 三种方式。下面按常见场景说明。
如果你是用 apt 安装的 Kafka(或手动解压):
/etc/kafka/
server.properties:Broker 核心参数producer.properties:生产者示例参数consumer.properties:消费者示例参数/opt/kafka/config/server.properties
编辑:
sudo vim /etc/kafka/server.properties
num.network.threads=8
num.io.threads=16
log.dirs=/data/kafka-logs
log.retention.hours=168
log.segment.bytes=1073741824
num.partitions=6
default.replication.factor=3
min.insync.replicas=2
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
修改后重启 Kafka:
sudo systemctl restart kafka
Kafka 对磁盘和网络敏感,建议调整:
ulimit -n 100000
永久生效:
sudo vim /etc/security/limits.conf
kafka soft nofile 100000
kafka hard nofile 100000
sudo sysctl vm.swappiness=1
永久:
echo "vm.swappiness=1" | sudo tee -a /etc/sysctl.conf
sudo sysctl -w net.core.somaxconn=4096
sudo sysctl -w net.ipv4.tcp_max_syn_backlog=8192
kafka-configs.sh --bootstrap-server localhost:9092 \
--entity-type topics --entity-name test-topic --describe
kafka-configs.sh --bootstrap-server localhost:9092 \
--entity-type topics --entity-name test-topic \
--alter --add-config retention.ms=86400000
kafka-console-producer.sh \
--bootstrap-server localhost:9092 \
--topic test \
--producer.config /etc/kafka/producer.properties
producer.properties:
acks=all
linger.ms=20
batch.size=65536
compression.type=lz4
kafka-configs.sh --bootstrap-server localhost:9092 \
--entity-type brokers --entity-name 0 \
--alter --add-config log.cleaner.threads=4
kafka-broker-api-versions.sh --bootstrap-server localhost:9092
或:
kafka-topics.sh --bootstrap-server localhost:9092 --describe
| 场景 | 建议 |
|---|---|
| 高吞吐 | 增大 batch.size / linger.ms |
| 低延迟 | 减小 linger.ms,acks=1 |
| 数据安全 | acks=all + min.insync.replicas |
| 磁盘满 | 缩短 retention |
| 消费者慢 | 增加 partition 数 |
如果你能说明:
我可以给你一套 针对 Debian 的 Kafka 参数模板。