准备:首先在主机器创建一个文件用于保存节点信息(域名或IP)。
前提:每台集群的kafka配置都没问题,都能单独启动。
1.创建脚本并编写
2.授权执行脚本
1.Kafka集群启动关闭脚本
mkdir -p /software/config-sh
mkdir -p /software/cluster-start-sh #集群启动脚本目录
cd /software/config-sh
vim ./slave
#添加如下
node1 #映射192.168.128.141
node2 #映射192.168.128.142
node3 #映射192.168.128.143
:wq #保存退出
cd /software/cluster-start-sh
#创建kafka集群启动脚本
vim kafka-start.sh
#添加如下
cat /export/onekey/slave | while read line
do
{
echo $line
ssh $line "source /etc/profile;
export JMX_PORT=9988;
nohup ${KAFKA_HOME}/bin/kafka-server-start.sh ${KAFKA_HOME}/config/server.properties >/dev/nul* 2>&1 & "
}&
wait
done
#关闭运行脚本
vim kafka-sop.sh
cat /export/onekey/slave | while read line
do
{
echo $line
ssh $line "source /etc/profile;
jps |grep Kafka |cut -d' ' -f1 |xargs kill -s 9"
}&
wait
done
#授权
chmod u+x start-kafka.sh
chmod u+x stop-kafka.sh
2.Zookeeper集群启动关闭脚本
cd /software/start-sh
vim zookeeper-start.sh
#启动脚本
cat /software/config-sh/slave | while read line
do
{
echo $line
ssh $line "/software/apache-zookeeper-3.5.6-bin/bin/zkServer.sh start "
}&
wait
done
vim zookeeper-stop.sh
#关闭脚本
cat /software/config-sh/slave | while read line
do
{
echo $line
ssh $line "source /etc/profile;
jps |grep QuorumPeerMain |cut -d' ' -f1 |xargs kill -s 9"
}&
wait
done
#授权
chmod u+x zookeeper-start.sh
chmod u+x zookeeper-stop.sh
3.另外可以编写传参的脚本可以表示操作类型,详情请参考:解决ssh命令集群启动zookeeper找不到java路径,Error:JAVA_HOME is not set and java could not be found in PATH-CSDN博客