CentOS7 Kafka 2.3.0 伪分布式集群安装

请先安装Zookeeper,参考资料:《CentOS7 ZooKeeper 3.5.5 伪分布式集群安装》

1、创建目录/opt/kafka,并下载kafka安装包。

mkdir /opt/kafka
cd /opt/kafka
wget http://mirror.bit.edu.cn/apache/kafka/2.3.0/kafka_2.12-2.3.0.tgz

2、解压Kafka。

tar zxvf kafka_2.12-2.3.0.tgz
mv kafka_2.12-2.3.0 kafka-server1
cp -rf kafka-server1 kafka-server2
cp -rf kafka-server1 kafka-server3

3、创建Kafka数据目录。

mkdir -p /opt/kafka/kafka-data/server1
mkdir -p /opt/kafka/kafka-data/server2
mkdir -p /opt/kafka/kafka-data/server3

4、配置server.properties

编辑kafka-server1的server.properties:

sed -i 's$broker.id=0$broker.id=1$' kafka-server1/config/server.properties
sed -i 's$#listeners=PLAINTEXT://:9092$listeners=PLAINTEXT://:9091$' kafka-server1/config/server.properties
sed -i 's$#advertised.listeners=PLAINTEXT://your.host.name:9092$advertised.listeners=PLAINTEXT://192.168.56.88:9091$' kafka-server1/config/server.properties
sed -i 's$log.dirs=/tmp/kafka-logs$log.dirs=/opt/kafka/kafka-data/server1$' kafka-server1/config/server.properties
sed -i 's$zookeeper.connect=localhost:2181$zookeeper.connect=192.168.56.88:2181,192.168.56.88:2182,192.168.56.88:2183$' kafka-server1/config/server.properties

编辑kafka-server2的server.properties:

sed -i 's$broker.id=0$broker.id=2$' kafka-server2/config/server.properties
sed -i 's$#listeners=PLAINTEXT://:9092$listeners=PLAINTEXT://:9092$' kafka-server2/config/server.propertiessed -i 's$#advertised.listeners=PLAINTEXT://your.host.name:9092$advertised.listeners=PLAINTEXT://192.168.56.88:9092$' kafka-server1/config/server.properties
sed -i 's$log.dirs=/tmp/kafka-logs$log.dirs=/opt/kafka/kafka-data/server2$' kafka-server2/config/server.properties
sed -i 's$zookeeper.connect=localhost:2181$zookeeper.connect=192.168.56.88:2181,192.168.56.88:2182,192.168.56.88:2183$' kafka-server2/config/server.properties

编辑kafka-server3的server.properties:

sed -i 's$broker.id=0$broker.id=3$' kafka-server3/config/server.properties
sed -i 's$#listeners=PLAINTEXT://:9092$listeners=PLAINTEXT://:9093$' kafka-server3/config/server.properties
sed -i 's$#advertised.listeners=PLAINTEXT://your.host.name:9092$advertised.listeners=PLAINTEXT://192.168.56.88:9093$' kafka-server3/config/server.properties
sed -i 's$log.dirs=/tmp/kafka-logs$log.dirs=/opt/kafka/kafka-data/server3$' kafka-server3/config/server.properties
sed -i 's$zookeeper.connect=localhost:2181$zookeeper.connect=192.168.56.88:2181,192.168.56.88:2182,192.168.56.88:2183$' kafka-server3/config/server.properties

说明:上面示例中的192.168.56.88是宿主机的IP。

5、设置zookeeper开机启动

mkdir /opt/kafka/kafka-command
cd /opt/kafka/kafka-command/

echo -e "\
#!/bin/bash\n\
# kafka 启动脚本\n\
/opt/kafka/kafka-server1/bin/kafka-server-start.sh -daemon /opt/kafka/kafka-server1/config/server.properties\n\
/opt/kafka/kafka-server2/bin/kafka-server-start.sh -daemon /opt/kafka/kafka-server2/config/server.properties\n\
/opt/kafka/kafka-server3/bin/kafka-server-start.sh -daemon /opt/kafka/kafka-server3/config/server.properties\n\
" > start-kafka.sh

echo -e "\
#!/bin/bash\n\
# kafka 停止脚本\n\
/opt/kafka/kafka-server1/bin/kafka-server-stop.sh -daemon /opt/kafka/kafka-server1/config/server.properties\n\
/opt/kafka/kafka-server2/bin/kafka-server-stop.sh -daemon /opt/kafka/kafka-server2/config/server.properties\n\
/opt/kafka/kafka-server3/bin/kafka-server-stop.sh -daemon /opt/kafka/kafka-server3/config/server.properties\n\
" > stop-kafka.sh

echo -e "\
#!/bin/bash\n\
# kafka 重启脚本\n\
/opt/kafka/kafka-server1/bin/kafka-server-stop.sh -daemon /opt/kafka/kafka-server1/config/server.properties\n\
/opt/kafka/kafka-server2/bin/kafka-server-stop.sh -daemon /opt/kafka/kafka-server2/config/server.properties\n\
/opt/kafka/kafka-server3/bin/kafka-server-stop.sh -daemon /opt/kafka/kafka-server3/config/server.properties\n\
/opt/kafka/kafka-server1/bin/kafka-server-start.sh -daemon /opt/kafka/kafka-server1/config/server.properties\n\
/opt/kafka/kafka-server2/bin/kafka-server-start.sh -daemon /opt/kafka/kafka-server2/config/server.properties\n\
/opt/kafka/kafka-server3/bin/kafka-server-start.sh -daemon /opt/kafka/kafka-server3/config/server.properties\n\
" > restart-kafka.sh

chmod +x *.sh

groupadd kafka
useradd -r -g kafka -s /bin/false kafka
chown -R kafka.kafka /opt/kafka

cd /usr/lib/systemd/system

echo -e "\
[Unit]\n\
Description=Kafka Service\n\
Requires=zookeeper\n\
After=network.target zookeeper\n\
[Service]\n\
Type=forking\n\
ExecStart=/opt/kafka/kafka-command/start-kafka.sh\n\
ExecStop=/opt/kafka/kafka-command/stop-kafka.sh\n\
ExecReload=/opt/kafka/kafka-command/restart-kafka.sh\n\
User=kafka\n\
Group=kafka\n\
[Install]\n\
WantedBy=multi-user.target\n\
" > kafka.service

systemctl enable kafka
systemctl start kafka

6、防火墙开放相应端口

firewall-cmd --zone=public --add-port=9091/tcp --permanent
firewall-cmd --zone=public --add-port=9092/tcp --permanent
firewall-cmd --zone=public --add-port=9093/tcp --permanent
firewall-cmd --reload

未经允许不得转载:君子如兰 » CentOS7 Kafka 2.3.0 伪分布式集群安装

赞 (0) 打赏

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏