【kafka】一键启动kafka脚本

3.1 创建文件
cd bin 跳转到bin文件夹里
touch  start-kafka-cluster.sh --新建一键启动文件
touch  stop-kafka-cluster.sh --新建一键停止文件

3.2 编写start
start-kafka-cluster.sh内容:
#!/bin/bash
brokers="hadoop01 hadoop02 hadoop03"
KAFKA_HOME="/home/hadoop/kafka_2.12-2.3.0"
KAFKA_NAME="kafka_2.12-2.3.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 !"

3.2 编写stop
stop-kafka-cluster.sh内容:
#!/bin/bash
brokers="hadoop01 hadoop02 hadoop03"
KAFKA_HOME="/home/hadoop/kafka_2.11-2.3.0"
KAFKA_NAME="kafka_2.11-2.3.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
注1:如 cat -v start-kafka-cluster.sh 或 cat -v start-kafka-cluster.sh 遇见每行末尾有“^M”,则使用cat filename |tr -d '\r' > newfilename 命令
注2:修改kafka-server-stop.sh(因为安装后本身stop文件不能真正停止kafka进程):
#PIDS=$(ps ax | grep -i 'kafka\.Kafka' | grep java | grep -v grep | awk '{print $1}')
PIDS=$(ps ax | grep -i 'kafka\.Kafka' | awk '{print $1}' | xargs kill -SIGTERM)

具体执行过程如下:

[hadoop@hadoop01 ~]$ jps --查看kafka是否启动
8736 NodeManager
8593 ResourceManager
8083 DataNode
7942 NameNode
21336 Jps
8330 SecondaryNameNode
13595 Kafka
[hadoop@hadoop01 ~]$ cd bin
[hadoop@hadoop01 bin]$ sh stop-kafka-cluster.sh --启动stop脚本
INFO : Begin to stop kafka cluster ...
INFO : Shut down kafka_2.12-2.3.0 on hadoop01 ...
No kafka server to stop
INFO : Shut down kafka_2.12-2.3.0 on hadoop01 is down
INFO : Shut down kafka_2.12-2.3.0 on hadoop02 ...
No kafka server to stop
INFO : Shut down kafka_2.12-2.3.0 on hadoop02 is down
INFO : Shut down kafka_2.12-2.3.0 on hadoop03 ...
No kafka server to stop
INFO : Shut down kafka_2.12-2.3.0 on hadoop03 is down
INFO : kafka cluster shut down completed!
[hadoop@hadoop01 bin]$ jps --查看是否停止成功
8736 NodeManager
8593 ResourceManager
8083 DataNode
21428 Jps
7942 NameNode
8330 SecondaryNameNode
[hadoop@hadoop01 bin]$ sh start-kafka-cluster.sh --启动start脚本
INFO : Begin to start kafka cluster ...
INFO : Starting kafka_2.12-2.3.0 on hadoop01 ...
INFO:[hadoop01] Start successfully
INFO : Starting kafka_2.12-2.3.0 on hadoop02 ...
INFO:[hadoop02] Start successfully
INFO : Starting kafka_2.12-2.3.0 on hadoop03 ...
INFO:[hadoop03] Start successfully
INFO:Kafka cluster starts successfully !
[hadoop@hadoop01 bin]$ jps --查看节点1是否启动成功
8736 NodeManager
8593 ResourceManager
8083 DataNode
21923 Jps
7942 NameNode
21847 Kafka
8330 SecondaryNameNode
[hadoop@hadoop01 bin]$ ssh hadoop02 --查看节点2是否启动成功
Last login: Wed Oct  2 09:21:59 2019 from gateway
[hadoop@hadoop02 ~]$ jps
10564 NodeManager
10888 QuorumPeerMain
15563 Jps
10431 DataNode
15439 Kafka

你可能感兴趣的:(【kafka】一键启动kafka脚本)