Kafka一键启动脚本
(1)编辑环境变量
vim /etc/profile
添加内容:
export KAFKA_HOME=/export/server/kafka
export PATH=$PATH:$KAFKA_HOME/bin
(2)创建kafka一键启动脚本
vim /usr/local/bin/kafkacmd.sh
添加内容(Kafka所在服务器的hostname
按照自己的进行配置):
#! /bin/bash
# Kafka代理节点地址
hosts=(node1 node2 node3)
# 打印启动分布式脚本信息
mill=`date "+%N"`
tdate=`date "+%Y-%m-%d %H:%M:%S,${mill:0:3}"`
echo [$tdate] INFO [Kafka Cluster] begins to execute the $1 operation.
# 执行分布式开启命令
function start()
{
for i in ${hosts[@]}
do
smill=`date "+%N"`
stdate=`date "+%Y-%m-%d %H:%M:%S,${smill:0:3}"`
ssh $i "source /etc/profile;echo [$stdate] INFO [Kafka Broker $i] begins to execute the startup operation.;kafka-server-start.sh $KAFKA_HOME/config/server.properties>/dev/null" &
sleep 1
done
}
# 执行分布式关闭命令
function stop()
{
for i in ${hosts[@]}
do
smill=`date "+%N"`
stdate=`date "+%Y-%m-%d %H:%M:%S,${smill:0:3}"`
ssh $i "source /etc/profile;echo [$stdate] INFO [Kafka Broker $i] begins to execute the shutdown operation.;kafka-server-stop.sh>/dev/null;" &
sleep 1
done
}
# 查看Kafka代理节点状态
function status()
{
for i in ${hosts[@]}
do
smill=`date "+%N"`
stdate=`date "+%Y-%m-%d %H:%M:%S,${smill:0:3}"`
ssh $i "source /etc/profile;echo [$stdate] INFO [Kafka Broker $i] status message is :;jps | grep Kafka;" &
sleep 1
done
}
# 判断输入的Kafka命令参数是否有效
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
*)
echo "Usage: $0 {start|stop|status}"
RETVAL=1
esac
(3)给脚本赋权
chmod 777 /usr/local/bin/kafkacmd.sh
(4)如果要使用stop
vim /export/server/kafka/bin/kafka-server-stop.sh
添加内容:
# PIDS=$(ps ax | grep -i 'kafka\.Kafka' | grep java | grep -v grep | awk '{print $1}')
PIDS=$(ps ax | grep -i 'Kafka' | grep java | grep -v grep | awk '{print $1}')
(5)再分发kafka-server-stop.sh(将此脚本发放到kafka集群所在服务器中)
scp /export/server/kafka/bin/kafka-server-stop.sh node2:/export/server/kafka/bin/
scp /export/server/kafka/bin/kafka-server-stop.sh node3:/export/server/kafka/bin/
(6)使用
# 查看状态
kafkacmd.sh status
# 开启
kafkacmd.sh start
# 停止
kafkacmd.sh stop