Ubuntu Kafka网络调优实战指南
一 基础网络与监听配置
listeners=INTERNAL://10.0.1.10:9092,EXTERNAL://0.0.0.0:9093
advertised.listeners=INTERNAL://10.0.1.10:9092,EXTERNAL://kafka.example.com:9093
listener.security.protocol.map=INTERNAL:PLAINTEXT,EXTERNAL:SSL
inter.broker.listener.name=INTERNAL
security.inter.broker.protocol=PLAINTEXT
要点:listeners 定义“监听谁”,advertised.listeners 定义“告诉客户端用谁”,两者必须同时正确;内外网分离时,内部用私有地址与协议,外部用可被客户端访问的域名与加密协议。二 Ubuntu系统层网络参数
# /etc/security/limits.conf
* soft nofile 65536
* hard nofile 65536
root soft nofile 65536
root hard nofile 65536
# /etc/sysctl.d/99-kafka-network.conf
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 4096
net.core.netdev_max_backlog = 5000
net.core.rmem_max = 134217728
net.core.wmem_max = 134217728
net.ipv4.tcp_rmem = 4096 87380 134217728
net.ipv4.tcp_wmem = 4096 65536 134217728
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_sack = 1
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_tw_reuse = 1
sysctl -p /etc/sysctl.d/99-kafka-network.conf 并重启 Kafka;验证 ulimit -n 与 ss -s。三 Kafka Broker网络与I/O关键参数
num.network.threads=8
num.io.threads=16
socket.send.buffer.bytes=1048576
socket.receive.buffer.bytes=1048576
socket.request.max.bytes=104857600
replica.fetch.max.bytes=10485760
message.max.bytes=10485760
compression.type=lz4 # 或 snappy
四 生产者与消费者网络行为
batch.size=524288 # 512KB
linger.ms=50 # 允许适度攒批
compression.type=lz4
acks=1 # 高吞吐场景常用;强一致可用 all
fetch.min.bytes=1048576 # 1MB
fetch.max.wait.ms=500
max.poll.records=1000
五 验证与监控
# 生产者压测:100万条、10并发、1KB消息
bin/kafka-producer-perf-test.sh \
--topic perf-test \
--num-records 1000000 \
--record-size 1024 \
--throughput -1 \
--producer-props bootstrap.servers=kafka.example.com:9093 \
compression.type=lz4 \
batch.size=524288 \
linger.ms=50
# 消费者压测
bin/kafka-consumer-perf-test.sh \
--topic perf-test \
--messages 1000000 \
--broker-list kafka.example.com:9093 \
--fetch-size 1048576