在CentOS环境中配置Kafka的步骤如下:
Kafka是用Java编写的,因此需要安装Java环境。
yum makecache fast
yum list | grep openjdk
yum install -y java-11-openjdk
java -version
为了优化Kafka的性能,需要调整一些系统参数。
创建并配置/etc/sysctl.d/custom.conf
文件:
cat << eof > /etc/sysctl.d/custom.conf
net.core.somaxconn = 32768
net.core.netdev_max_backlog = 32768
net.ipv4.ip_local_port_range = 1024 65000
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_max_tw_buckets = 30000
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_fack = 1
net.ipv4.tcp_tw_recycle = 0
eof
sysctl -p /etc/sysctl.d/custom.conf
创建并配置/etc/security/limits.conf
文件:
cat << eof >> /etc/security/limits.conf
* soft nproc 1000000
* hard nproc 1000000
* soft nofile 1000000
* hard nofile 1000000
eof
ulimit -n 1000000
从Kafka官网下载对应版本的Kafka,并解压到指定目录。
wget https://downloads.apache.org/kafka/2.8.1/kafka_2.13-2.8.1.tgz
tar -xzf kafka_2.13-2.8.1.tgz
mv kafka_2.13-2.8.1 /usr/local/kafka
编辑Kafka的配置文件/usr/local/kafka/config/server.properties
,根据实际需求调整以下配置项:
broker.id
: 每个Kafka broker的唯一标识。listeners
: Kafka监听的地址和端口。advertised.listeners
: 对外公布的Kafka地址和端口。zookeeper.connect
: Zookeeper连接地址(如果使用)。示例配置:
broker.id=1
listeners=PLAINTEXT://:9092
advertised.listeners=PLAINTEXT://10.100.8.201:9092
zookeeper.connect=10.100.8.201:2181
可以编写一个启动脚本,方便管理和启动Kafka服务。
#!/bin/bash
KAFKA_HOME=/usr/local/kafka
CONFIG_DIR=$KAFKA_HOME/config
# 启动Kafka服务器
$KAFKA_HOME/bin/zookeeper-server-start.sh $CONFIG_DIR/zookeeper.properties
$KAFKA_HOME/bin/kafka-server-start.sh $CONFIG_DIR/server.properties
将脚本设置为可执行:
chmod +x start_kafka.sh
使用以下命令验证Kafka是否正常运行:
$KAFKA_HOME/bin/kafka-topics.sh --list --bootstrap-server 10.100.8.201:9092
如果希望Kafka在系统启动时自动启动,可以使用systemd配置服务。
创建Kafka服务文件:
sudo tee /etc/systemd/system/kafka.service <<EOF
[Unit]
Description=Apache Kafka
After=network.target
[Service]
Type=simple
User=kafka
WorkingDirectory=$KAFKA_HOME
ExecStart=$KAFKA_HOME/bin/kafka-server-start.sh $CONFIG_DIR/server.properties
ExecStop=$KAFKA_HOME/bin/kafka-server-stop.sh
Restart=always
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start kafka
systemctl enable kafka
通过以上步骤,你就可以在CentOS环境中成功配置和运行Kafka了。根据实际需求,你可能还需要进一步调整配置文件和集群设置。