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)
具體執行過程以下:java
[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