Kafka集群搭建(亲测可用)

hadoop3.22,flink1.13,kafka2.7.0,zookeeper3.6.3等安装包……

下载地址:

链接:https://pan.baidu.com/s/1eHKkQw2ymnfq2p1XIzOCjw
提取码:i29n

 

其他集群搭建:

Zookeep集群搭建(亲测可用)

 

1.准备工作

三台虚拟机分别为:Flink1,Flink2,Flink3

配置好了java环境与免密登录以及映射。

按照下面的配置以及上面的组件版本,已经搭建成功,可以放心往下看。

 

2.kafka集群搭建

2.1解压安装包

使用xftp上传安装包并进入相应目录解压

tar -zvxf kafka_2.12-2.7.0.tgz -C /opt/module

 

2.2配置环境变量

此步骤三台虚拟机均需要

vim /etc/profile
export KAFKA_HOME=/opt/module/kafka_2.12-2.7.0
export PATH=$PATH:$KAFKA_HOME/bin

 

使环境变量生效

source /etc/profile

 

2.3创建存放消息的文件夹

cd /opt/module/kafka_2.12-2.7.0
mkdir logs

 

2.4修改配置文件

cd /opt/module/kafka_2.12-2.7.0/config
vim server.properties

修改或增加以下地方:

broker.id=0
delete.topic.enable=true
listeners=PLAINTEXT://Flink1:9092
log.dirs=/opt/module/kafka_2.12-2.7.0/logs
zookeeper.connect=Flink1:2181,Flink2:2181,Flink3:2181
unclean.leader.election.enable=false

broder.id:broder的唯一标识
delete.topic.enable:是否可以删除主题
listener:该参数用于客户端和broker进行连接,用来绑定私网ip,如果想绑定公网ip,应该设置的是:advertised.listeners这个参数。
log.dirs:kafka消息持久化的路径
zookeeper.connect:zk节点,这里注意,应该配置全部的zk节点
unclean.leader.election.enable:消息同步落后太多的副本可不可以进行leader选举

 

2.5同步到其他机器并修改配置文件

此处用scp指令,配置了免密就不用输入密码

scp -r kafka_2.12-2.7.0/ root@Flink2:/opt/module
scp -r kafka_2.12-2.7.0/ root@Flink3:/opt/module

修改Flink2的server.properties:

broker.id=1

listeners=PLAINTEXT://Flink2:9092

修改Flink2的server.properties:

broker.id=2

listeners=PLAINTEXT://Flink3:9092

 

2.6编写批量操作脚本

使用Flink1,进入/opt/module/kafka_2.12-2.7.0/bin,编写脚本

启动kafka集群:

vim start-kafka-cluster.sh

#!/bin/bash
brokers="Flink1 Flink2 Flink3"
KAFKA_HOME="/opt/module/kafka_2.12-2.7.0"
KAFKA_NAME="kafka_2.12-2.7.0"

echo "INFO : Begin to start kafka cluster ..."

for broker in $brokers
do
  echo "INFO : Starting ${KAFKA_NAME} on ${broker} ..."
  ssh ${broker} -C "source /etc/profile; sh ${KAFKA_HOME}/bin/kafka-server-start.sh -daemon ${KAFKA_HOME}/config/server.properties"
  if [[ $? -eq 0 ]]; then
      echo "INFO:[${broker}] Start successfully"
  fi
done
echo "INFO:Kafka cluster starts successfully !"

关闭kafka集群:

vim stop-kafka-cluster.sh

#!/bin/bash
brokers="Flink1 Flink2 Flink3"
KAFKA_HOME="/opt/module/kafka_2.12-2.7.0"
KAFKA_NAME="kafka_2.12-2.7.0"

echo "INFO : Begin to stop kafka cluster ..."

for broker in $brokers
do
  echo "INFO : Shut down ${KAFKA_NAME} on ${broker} ..."
  ssh ${broker} "source /etc/profile;bash ${KAFKA_HOME}/bin/kafka-server-stop.sh"
  if [[ $? -ne 0 ]]; then
      echo "INFO : Shut down ${KAFKA_NAME} on ${broker} is down"
  fi
done

echo "INFO : kafka cluster shut down completed!"

记得修改主机名称。

 

2.7启动kafka集群

start-kafka-cluster.sh

 

posted @ 2022-01-06 20:06  星予  阅读(1815)  评论(0编辑  收藏  举报