shell脚本启动kafka集群

前提:kafka集群之间已配置ssh免密登录,kafka集群配置路径一致

要实现kafka群起脚本
思路:通过ssh登录后,循环启动每台机器的kafka和zookeeper

kafka和zookeeper单机启动脚本:remote_server.sh 

#!/bin/bash
#zookeeper 进程号
ZK_PID=`ps -ef|grep zookeeper|grep -v grep|awk '{print $2}'`
#kafka 进程号
KAFKA_PID=`ps -ef|grep kafka|grep -v grep|awk '{print $2}'`
#机器的hostname
NODE_NAME=`hostname`
#选址“启动服务”
case $1 in "start"){
#如果zookeeper进程存在
if [ -n "$ZK_PID" ];then
  echo "zkServer is already started"
else
  echo "starting zkServer.....";
  sh /apphome/zookeeper/zookeeper-3.4.14/bin/zkServer.sh "start";
  if [ $? -eq 0 ]; then
    echo "zkServer of $NODE_NAME started Success!"
  else
    echo "zkServer of $NODE_NAME started failure!"
  fi
fi
#如果kafka进程存在
if [ -n "$KAFKA_PID" ];then
  echo "KAFKA Server is already started"
else
  echo "starting KAFKA Server.....";
#启动kafka
  echo "/apphome/kafka_2.11-0.11.0.0/bin/kafka-server-start.sh -daemon /apphome/kafka_2.11-0.11.0.0/config/server.properties" |sh
  if [ $? -eq 0 ]; then
    echo "KAFKA Server of $NODE_NAME started Success!"
  else
    echo "KAFKA Server of $NODE_NAME started failure!"
  fi
fi

};;
#如果选址“停止服务”
"stop"){
if [ -z "$KAFKA_PID" ];then
  echo "KAFKA Server is already stopped"
else
  echo "stopping KAFKA Server.....";
  sh /apphome/kafka_2.11-0.11.0.0/bin/kafka-server-stop.sh
  if [ $? -eq 0 ]; then
    echo "KAFKA Server of $NODE_NAME stopped Success!"
  else
    echo "KAFKA Server of $NODE_NAME stopped failure!"
  fi
fi

if [ -z "$ZK_PID" ];then
  echo "zkServer is already stopped"
else
  echo "stopping zkServer.....";
  sh /apphome/zookeeper/zookeeper-3.4.14/bin/zkServer.sh "stop";
  if [ $? -eq 0 ]; then
    echo "zkServer of $NODE_NAME stopped Success!"
  else
    echo "zkServer of $NODE_NAME stopped failure!"
  fi
fi

};;
esac

kafka集群启动入口:start-server-check.sh,在此脚本中连接ssh调用remote_server.sh 

#!/bin/bash
for ip in rabbit-node1 rabbit-node2 rabbit-node3
  do
   ssh $ip 'bash -s' 

脚本执行:sh start-server-check.sh start  和  sh start-server-check.sh start

你可能感兴趣的:(ssh,linux)