Shell ❀ Zookeeper和Kafka集群环境启停脚本
Posted 无糖可乐没有灵魂
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Shell ❀ Zookeeper和Kafka集群环境启停脚本相关的知识,希望对你有一定的参考价值。
主要实现内容:集群环境下Zookeeper和Kafka中间件服务启停操作以脚本控制
1 ZK集群环境启停脚本
$ vim /middleware/zookeeper/bin/zkCluster.sh
#!/bin/bash
# 服务安装路径需要修改为本地当前环境配置
zk_path=/middleware/zookeeper/bin/zkServer.sh
case $1 in
"start")
for i in master node1 node2
# 集群机器主机名称或IP地址需要修改为本地当前环境配置
do
ssh $i "$zk_path start"
echo -e "\\033[32m$i zookeeper service is STARTED!\\033[0m"
done
;;
"stop")
for i in master node1 node2
do
ssh $i "$zk_path stop"
echo -e "\\033[31m$i zookeeper service is STOPPED!\\033[0m"
done
;;
"status")
for i in master node1 node2
do
ssh $i "$zk_path status"
done
;;
*)
echo -e "\\033[31mInput ERROR,Please input \\"start/stop/status!\\"\\033[0m"
;;
esac
2 Kafka集群环境启停脚本
$ vim /middleware/kafka/bin/kafka-cluster.sh
#!/bin/bash
# 服务安装路径需要修改为本地当前环境配置
kafka_path=/middleware/kafka/
zk_path=/middleware/zookeeper/
function kafka_start()
for i in master node1 node2; do
# 集群机器主机名称或IP地址需要修改为本地当前环境配置
ssh $i "cd $kafka_path;sh ./bin/kafka-server-start.sh -daemon ./config/server.properties;cd - > /dev/null"
echo -e "\\033[32m$i Kafka service is STARTED!\\033[0m"
done
function kafka_stop()
for i in master node1 node2; do
ssh $i "cd $kafka_path;sh ./bin/kafka-server-stop.sh;cd - > /dev/null"
echo -e "\\033[32m$i Kafka service is STOPED!\\033[0m"
done
function zk_start()
for i in master node1 node2; do
ssh $i "cd $zk_path;sh ./bin/zkCluster.sh start;cd - > /dev/null"
echo -e "\\033[32m$i Zookeeper service is STARTED!\\033[0m"
done
function zk_stop()
for i in master node1 node2; do
ssh $i "cd $zk_path;sh ./bin/zkCluster.sh stop;cd - > /dev/null"
echo -e "\\033[32m$i Zookeeper service is STOPED!\\033[0m"
done
function kf_process()
while [[ true ]]; do
# 无限循环判断检测kafka进程
pgrep kafka
xx=`cat /root/test/aaa | wc -l`
if [[ $xx -eq 2 ]]; then
# 进程存在,1秒后重复检测
sleep 1s
else
# 进程不存在,退出循环
break
fi
done
case $1 in
start )
# 开启kafka服务,先判断ZK服务是否开启
pgrep zookeeper
if [[ $? -eq 0 ]]; then
# ZK服务开启,直接开启kafka服务
kafka_start
else
# ZK服务关闭,开启ZK+kafka服务
zk_start && kafka_start
fi
;;
stop )
# 关闭kafka服务,需要判断kafka进程是否存在,防止ZK直接关闭导致kafka数据存留
kafka_stop && kf_process && zk_stop
;;
* )
# 输入异常参数提醒
echo -e "\\033[31mInput ERROR,Please input \\"start/stop!\\"\\033[0m"
;;
esac
以上是关于Shell ❀ Zookeeper和Kafka集群环境启停脚本的主要内容,如果未能解决你的问题,请参考以下文章