利用脚本批量操作Kafka集群

2种常用操作:

kafka-server-start.sh -daemon home/kafka/config/server.properties  #启动

kafka-server-stop.sh   #停止

假设有4台机,IP及主机名如下:

192.168.100.105 c1
192.168.100.110 c2
192.168.100.115 c3
192.168.100.120 c4

假设Kafka安装在/home/目录下,也就是/home/kafka

* 先登录root账号再进行以下操作。

1.创建脚本

创建并赋予运行权限

mk batch-kafka.sh
chmod u+x batch-kafka.sh

2.编辑脚本

vim batch-kafka.sh

添加以下内容:

#!/bin/bash
usage="Usage: $0 (start|stop)"

if [ $# -lt 1 ]; then
  echo $usage
  exit 1
fi

action=$1
iparray=(c1 c2 c3 c4)
path="/home/kafka"

echo "$action Kafka cluster"

for ip in ${iparray[*]}
do
    echo "ssh to $ip"
    case $action in
        "start")
        ssh $ip $path/bin/kafka-server-start.sh -daemon $path/config/server.properties
        ;;

        "stop")
        ssh $ip $path/bin/kafka-server-stop.sh
        ;;
    esac
    sleep 1s
done

exit 0

3.编辑kafka-server-start.sh、kafka-server-stop.sh

* 该步骤在每台机都要执行

vim /home/kafka/bin/kafka-server-start.sh
vim /home/kafka/bin/kafka-server-stop.sh

2个文件各添加一行内容:

export JAVA_HOME=/usr/bin/jdk1.8.0

这里的/usr/bin/jdk1.8.0是Java安装目录,请按实际情况修改。

4.运行脚本

只需要在其中一台机运行即可

批量启动:

./batch-kafka.sh start

批量停止:

./batch-kafka.sh stop
原文地址:https://www.cnblogs.com/live41/p/15636926.html