Kafka的网络配置主要通过server.properties文件实现,关键参数及配置说明如下:
协议://IP:端口。若需监听所有网络接口,可使用0.0.0.0(如PLAINTEXT://0.0.0.0:9092);若仅限本地访问,用localhost(如PLAINTEXT://localhost:9092)。PLAINTEXT://192.168.1.100:9092,外网环境需替换为公网IP或域名(如PLAINTEXT://kafka.example.com:9092)。Kafka依赖Zookeeper管理集群状态,需在server.properties中正确配置zookeeper.connect参数,格式为host:port(单机Zookeeper)或host1:port1,host2:port2,host3:port3(集群Zookeeper)。例如:
zookeeper.connect=localhost:2181(单机)
zookeeper.connect=zoo1:2888:3888,zoo2:2888:3888,zoo3:2888:3888(集群,需配合Zookeeper的myid文件)
/etc/netplan/01-netcfg.yaml(Ubuntu)或/etc/sysconfig/network-scripts/ifcfg-eth0(CentOS),设置静态IP、网关和DNS。例如Ubuntu配置:network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
addresses: ["192.168.1.100/24"]
gateway4: "192.168.1.1"
nameservers:
addresses: ["8.8.8.8", "8.8.4.4"]
应用配置:sudo netplan apply。ufw(Ubuntu):sudo ufw allow 9092/tcp;使用firewalld(CentOS):sudo firewall-cmd --permanent --add-port=9092/tcp && sudo firewall-cmd --reload。sudo service iptables stop(CentOS)或sudo ufw disable(Ubuntu),但生产环境不建议。编辑/etc/sysctl.conf文件,调整TCP参数以提升网络性能:
net.core.rmem_max = 16777216 # 接收缓冲区最大值
net.core.wmem_max = 16777216 # 发送缓冲区最大值
net.ipv4.tcp_rmem = 4096 87380 16777216 # TCP接收缓冲区分段
net.ipv4.tcp_wmem = 4096 65536 16777216 # TCP发送缓冲区分段
net.ipv4.tcp_congestion_control = cubic # 拥塞控制算法(Linux默认)
net.ipv4.tcp_no_metrics_save = 1 # 不保存TCP metrics,减少资源占用
net.ipv4.tcp_low_latency = 1 # 优先低延迟
应用配置:sudo sysctl -p。
Kafka处理大量并发连接时,需增加文件描述符限制。编辑/etc/security/limits.conf文件,添加以下内容:
* soft nofile 65536 # 普通用户软限制
* hard nofile 65536 # 普通用户硬限制
root soft nofile 65536 # root用户软限制
root hard nofile 65536 # root用户硬限制
重新登录或重启系统使配置生效。
bin目录,执行./kafka-server-start.sh ../config/server.properties启动Kafka。netstat -tulnp | grep 9092或ss -tulnp | grep 9092,确认Kafka正在监听指定端口。# 创建Topic
./kafka-topics.sh --create --topic test-topic --bootstrap-server 192.168.1.100:9092 --replication-factor 1 --partitions 1
# 生产消息
./kafka-console-producer.sh --topic test-topic --bootstrap-server 192.168.1.100:9092
# 消费消息
./kafka-console-consumer.sh --topic test-topic --from-beginning --bootstrap-server 192.168.1.100:9092
通过以上步骤,可完成Kafka在Linux上的网络配置,并确保其稳定运行。根据实际需求(如集群部署、安全需求),还需进一步调整参数(如advertised.listeners的多地址配置、SSL加密等)。