利用SSH实现分布式应用的一键安装部署

Posted 鮀城小帅

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用SSH实现分布式应用的一键安装部署相关的知识,希望对你有一定的参考价值。

1. 一键安装流程步骤

1.1 步骤:

  1. 拷贝安装包到远端主机
  2. 创建安装目录并解压安装包
  3. 格式化磁盘挂着载数据目录
  4. 修改服务配置文件
  5. 启动服务并验证集群运行状态

1.2 服务一键启停及状态监测

2.分布式kafka集群安装部署脚本

2.1 脚本框架

#!/bin/bash

#第一步:关闭firewalld和selinux

#第二步:安装配置JDK

#第三步:安装配置zookeeper,并启动服务

#第四步:安装配置kafka,并启动服务

2.2 多主机执行指令函数封装

#!/bin/bash

HOST_LIST="192.168.126.136 192.168.126.137"

# 多主机执行指令函数封装
function remote_execute

        for host in $HOST_LIST;do            
                echo "++++++++++Execute Command < $@ > ON Host: $host +++++++++++"
                sshpass -p root ssh -o StrictHostKeyChecking=no root@$host $@
                if [ $? -eq 0 ];then
                        echo "$CMD_NUM Congratulation.Command < $@ > execute success"
                else
                        echo "$CMD_NUM Sorry.Command < $@ > execute failed"
                fi
        done


remote_execute "df -h"
remote_execute "cat /etc/fstab"

#第一步:关闭firewalld和selinux

#第二步:安装配置JDK

#第三步:安装配置zookeeper,并启动服务

#第四步:安装配置kafka,并启动服务

2.3 利用exec 管理安装部署日志

#!/bin/bash
#

#删除旧日志
if [ -e ./deploy_kafka.log ];then
        rm -f ./deploy_kafka/log
fi

#实现将echo语句重定向到log中,就不需要每个echo都进行输出
exec 1>> ./deploy_kafka.log 2>&1

HOST_LIST="192.168.126.136"
CMD_NUM=0

# 多主机执行指令函数封装
function remote_execute

        for host in $HOST_LIST;do
                CMD_NUM=`expr $CMD_NUM + 1`
                echo "++++++++++Execute Command < $@ > ON Host: $host +++++++++++"
                sshpass -p root ssh -o StrictHostKeyChecking=no root@$host $@
                if [ $? -eq 0 ];then
                        echo "$CMD_NUM Congratulation.Command < $@ > execute success"
                else
                        echo "$CMD_NUM Sorry.Command < $@ > execute failed"
                fi
        done


remote_execute "df -h"
remote_execute "cat /etc/fstab"

#第一步:关闭firewalld和selinux


#第二步:安装配置JDK

#第三步:安装配置zookeeper,并启动服务

#第四步:安装配置kafka,并启动服务

2.4 实现一键永久关闭 firewalld和selinux

(1)关闭firewall和selinux脚本如下:

#第一步:关闭firewalld和selinux
# 关闭防火墙
remote_execute "systemctl stop firewalld.service"
# 关闭防火墙服务的自启动
remote_execute "systemctl disable firewalld.service"
# 临时关闭防火墙
remote_execute "setenforce 0"    
# 永久关闭防火墙
remote_execute "sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux"

需要先关闭防火墙,并关闭防火墙的开机自启。

临时关闭防火墙:使用 "setenforce 0 ",表示 设置SELinux 成为permissive模式 临时关闭selinux防火墙。
永久关闭防火墙:输入命令vi /etc/selinux/config,将SELINUX=enforcing改为SELINUX=disabled,然后保存退出

(2)完整代码如下:

#!/bin/bash
#

#删除旧日志
if [ -e ./deploy_kafka.log ];then
        rm -f ./deploy_kafka/log
fi

#实现将echo语句重定向到log中,就不需要每个echo都进行输出
exec 1>> ./deploy_kafka.log 2>&1

HOST_LIST="192.168.126.136 192.168.126.137"
CMD_NUM=0

# 多主机执行指令函数封装
function remote_execute

        for host in $HOST_LIST;do
                CMD_NUM=`expr $CMD_NUM + 1`
                echo "++++++++++Execute Command < $@ > ON Host: $host +++++++++++"
                sshpass -p root ssh -o StrictHostKeyChecking=no root@$host $@
                if [ $? -eq 0 ];then
                        echo "$CMD_NUM Congratulation.Command < $@ > execute success"
                else
                        echo "$CMD_NUM Sorry.Command < $@ > execute failed"
                fi
        done


#remote_execute "df -h"
#remote_execute "cat /etc/fstab"

#第一步:关闭firewalld和selinux
remote_execute "systemctl stop firewalld.service"
remote_execute "systemctl disable firewalld.service"
remote_execute "setenforce 0"
remote_execute "sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux"

#第二步:安装配置JDK

#第三步:安装配置zookeeper,并启动服务

#第四步:安装配置kafka,并启动服务

2.5 多主机传输文件函数封装

(1)多主机传输文件函数封装

定义本地源文件存放的路径,定义上传到远程服务器后源文件的存放路径,还需要定义安装软件的路径,最后则是需要定义当前安装的应用包名称。

# 本地文件目录
LOCAL_DIR="/opt/tmp"
# 远程服务器文件路径
PACKAGE_DIR="/opt/package"
# 远程服务器软件按照路径
APP_DIR="/opt/source"
# jdk安装包
JDK_NAME="jdk-8u152-linux-x64.tar.gz"

封装多主机传输文件函数

# 多主机传输文件函数封装
function remote_transfer

        SRC_FILE=$1
        DST_DIR=$2
        # 函数必须有2个参数,第一个参数是本地文件或目录,第二个参数为远端主机目录
        if [ $# -lt 2 ];then
                echo "Usage: $0 <file|dir> <dst_dir>"
                exit 1
        fi

        # 判断第1个参数是否存在,如果不存在则直接退出并提示给用户
        if [ ! -e $SRC_FILE ];then
                echo "ERROR - $SRC_FILE is not exist,Please check..."
                exit 1
        fi

        # 判断第2个参数,远端主机目录是否存在,如果不存在,则创建
        for host in $HOST_LIST;do
                echo "++++++++++++++++Transfer File To HOST: $host ++++++++++++++++"
                CMD_NUM=`expr $CMD_NUM + 1`
                # 判断第2个参数,远端主机目录是否存在;如果不存在,则创建
                sshpass -p root ssh -o StrictHostKeyChecking=no root@$host "if [ ! -e $DST_DIR ];then mkdir $DST_DIR -p;fi"
                sshpass -p root scp -o StrictHostKeyChecking=no $SRC_FILE root@$host:$DST_DIR/
                if [ $? -eq 0 ];then
                        echo "Remote Host: $host - $CMD_NUM - INFO - SCP $SRC_FILE To dir $DST_DIR Success"
                else
                        echo "Remote Host: $host - $CMD_NUM - ERROR - SCP $SRC_FILE To dir $DST_DIR Failed"
                fi
        done

(2)完整代码

#!/bin/bash
#

#删除旧日志
if [ -e ./deploy_kafka.log ];then
        rm -f ./deploy_kafka/log
fi

#实现将echo语句重定向到log中,就不需要每个echo都进行输出
exec 1>> ./deploy_kafka.log 2>&1

HOST_LIST="192.168.126.136 192.168.126.139"
CMD_NUM=0
LOCAL_DIR="/opt/tmp"
PACKAGE_DIR="/opt/package"
APP_DIR="/opt/source"
JDK_NAME="jdk-8u152-linux-x64.tar.gz"



# 多主机执行指令函数封装
function remote_execute

        for host in $HOST_LIST;do
                CMD_NUM=`expr $CMD_NUM + 1`
                echo "++++++++++Execute Command < $@ > ON Host: $host +++++++++++"
                sshpass -p root ssh -o StrictHostKeyChecking=no root@$host $@
                if [ $? -eq 0 ];then
                        echo "$CMD_NUM Congratulation.Command < $@ > execute success"
                else
                        echo "$CMD_NUM Sorry.Command < $@ > execute failed"
                fi
        done


# 多主机传输文件函数封装
function remote_transfer

        SRC_FILE=$1
        DST_DIR=$2
        # 函数必须有2个参数,第一个参数是本地文件或目录,第二个参数为远端主机目录
        if [ $# -lt 2 ];then
                echo "Usage: $0 <file|dir> <dst_dir>"
                exit 1
        fi

        # 判断第1个参数是否存在,如果不存在则直接退出并提示给用户
        if [ ! -e $SRC_FILE ];then
                echo "ERROR - $SRC_FILE is not exist,Please check..."
                exit 1
        fi

        # 判断第2个参数,远端主机目录是否存在,如果不存在,则创建
        for host in $HOST_LIST;do
                echo "++++++++++++++++Transfer File To HOST: $host ++++++++++++++++"
                CMD_NUM=`expr $CMD_NUM + 1`
                # 判断第2个参数,远端主机目录是否存在;如果不存在,则创建

                sshpass -p root ssh -o StrictHostKeyChecking=no root@$host "if [ ! -e $DST_DIR ];then mkdir $DST_DIR -p;fi"
                sshpass -p root scp -o StrictHostKeyChecking=no $SRC_FILE root@$host:$DST_DIR/
                if [ $? -eq 0 ];then
                        echo "Remote Host: $host - $CMD_NUM - INFO - SCP $SRC_FILE To dir $DST_DIR Success"
                else
                        echo "Remote Host: $host - $CMD_NUM - ERROR - SCP $SRC_FILE To dir $DST_DIR Failed"
                fi
        done


#测试多主机传输文件函数
remote_transfer /etc/fstab /tmp
remote_transfer /tmp/test /mnt/file

#第一步:关闭firewalld和selinux
remote_execute "systemctl stop firewalld.service"
remote_execute "systemctl disable firewalld.service"
remote_execute "setenforce 0"
remote_execute "sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux"

#第二步:安装配置JDK


#第三步:安装配置zookeeper,并启动服务

#第四步:安装配置kafka,并启动服务

2.6 安装配置JAVA环境

(1)安装配置Java环境脚本编写

# 上传本机目录下的jdk安装包到远程主机目录
remote_transfer $LOCAL_DIR/$JDK_NAME $PACKAGE_DIR
# 判断远程主机目录是否有安装路径,没有就创建
remote_execute "if [ ! -d $APP_DIR ];then mkdir -p $APP_DIR;fi"
# 解压远程主机目录下的安装包到安装路径下
remote_execute "tar zxvf $PACKAGE_DIR/$JDK_NAME -C $APP_DIR"

# 添加环境变量配置,单独创建一个sh文件用来存放环境变量,便于更替,同时也避免了频繁更新 profile 文件的不便
# EOF 命令可以生成文件并从首行追加内容
cat > $LOCAL_DIR/java.sh << EOF
export JAVA_HOME=/opt/source/jdk1.8.0_152
export PATH=\\$PATH:\\$JAVA_HOME/bin:\\$JAVA_HOME/jre/bin
export JAVA_HOME PATH
EOF

# 将java.sh文件上传到远程服务器的/etc/profile.d目录下,该目录下存放环境变量配置文件
remote_transfer $LOCAL_DIR/java.sh /etc/profile.d/
# 脚本文件中需要使用 source 来使 java.sh 生效,如果使用 sh 命令,就只在当前线程生效,当前脚本线程结束就会失效
remote_execute "source /etc/profile.d/java.sh"
# 查看版本号,若打印则说明jdk安装成功
remote_execute "java -version"

(2)完整代码

#!/bin/bash
#

#删除旧日志
if [ -e ./deploy_kafka.log ];then
	rm -f ./deploy_kafka/log
fi

#实现将echo语句重定向到log中,就不需要每个echo都进行输出
exec 1>> ./deploy_kafka.log 2>&1

HOST_LIST="192.168.126.136"
CMD_NUM=0
LOCAL_DIR="/opt/tmp"
PACKAGE_DIR="/opt/package"
APP_DIR="/opt/source"
JDK_NAME="jdk-8u152-linux-x64.tar.gz"



# 多主机执行指令函数封装
function remote_execute

	for host in $HOST_LIST;do
		CMD_NUM=`expr $CMD_NUM + 1`
		echo "++++++++++Execute Command < $@ > ON Host: $host +++++++++++"
		sshpass -p root ssh -o StrictHostKeyChecking=no root@$host $@
		if [ $? -eq 0 ];then
			echo "$CMD_NUM Congratulation.Command < $@ > execute success" 
		else
			echo "$CMD_NUM Sorry.Command < $@ > execute failed"
		fi
	done


# 多主机传输文件函数封装
function remote_transfer

	SRC_FILE=$1
	DST_DIR=$2
	# 函数必须有2个参数,第一个参数是本地文件或目录,第二个参数为远端主机目录
	if [ $# -lt 2 ];then
		echo "Usage: $0 <file|dir> <dst_dir>"
		exit 1
	fi
	
	# 判断第1个参数是否存在,如果不存在则直接退出并提示给用户
	if [ ! -e $SRC_FILE ];then
		echo "ERROR - $SRC_FILE is not exist,Please check..."
		exit 1
	fi

	# 判断第2个参数,远端主机目录是否存在,如果不存在,则创建
	for host in $HOST_LIST;do
		echo "++++++++++++++++Transfer File To HOST: $host ++++++++++++++++"
		CMD_NUM=`expr $CMD_NUM + 1`
		# 判断第2个参数,远端主机目录是否存在;如果不存在,则创建
		
 		sshpass -p root ssh -o StrictHostKeyChecking=no root@$host "if [ ! -e $DST_DIR ];then mkdir $DST_DIR -p;fi"
		sshpass -p root scp -o StrictHostKeyChecking=no $SRC_FILE root@$host:$DST_DIR/
		if [ $? -eq 0 ];then
                        echo "Remote Host: $host - $CMD_NUM - INFO - SCP $SRC_FILE To dir $DST_DIR Success"
                else
       			echo "Remote Host: $host - $CMD_NUM - ERROR - SCP $SRC_FILE To dir $DST_DIR Failed" 
		fi
	done



#第一步:关闭firewalld和selinux
remote_execute "systemctl stop firewalld.service"
remote_execute "systemctl disable firewalld.service"
remote_execute "setenforce 0"
remote_execute "sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux"

#第二步:安装配置JDK
remote_transfer $LOCAL_DIR/$JDK_NAME $PACKAGE_DIR
remote_execute "if [ ! -d $APP_DIR ];then mkdir -p $APP_DIR;fi"
remote_execute "tar zxvf $PACKAGE_DIR/$JDK_NAME -C $APP_DIR"

cat > $LOCAL_DIR/java.sh << EOF
export JAVA_HOME=/opt/source/jdk1.8.0_152
export PATH=\\$PATH:\\$JAVA_HOME/bin:\\$JAVA_HOME/jre/bin
export JAVA_HOME PATH
EOF

remote_transfer $LOCAL_DIR/java.sh /etc/profile.d/
remote_execute "source /etc/profile.d/java.sh"
remote_execute "java -version"


#第三步:安装配置zookeeper,并启动服务

#第四步:安装配置kafka,并启动服务


2.7 安装配置 zookeeper 代码实现

#!/bin/bash
#

#删除旧日志
if [ -e ./deploy_kafka.log ];then
	rm -f ./deploy_kafka/log
fi
set -e
#实现将echo语句重定向到log中,就不需要每个echo都进行输出
exec 1>> ./deploy_kafka.log 2>&1

HOST_LIST="192.168.126.136 192.168.126.137 192.168.126.138"
CMD_NUM=0
LOCAL_DIR="/opt/tmp"
PACKAGE_DIR="/opt/package"
APP_DIR="/opt/source"
JDK_NAME="jdk-8u152-linux-x64.tar.gz"
ZK_NAME="apache-zookeeper-3.7.1-bin.tar.gz"


# 多主机执行指令函数封装
function remote_execute

	for host in $HOST_LIST;do
		CMD_NUM=`expr $CMD_NUM + 1`
		echo "++++++++++Execute Command < $@ > ON Host: $host +++++++++++"
		sshpass -p root ssh -o StrictHostKeyChecking=no root@$host $@
		if [ $? -eq 0 ];then
			echo "$CMD_NUM Congratulation.Command < $@ > execute success" 
		else
			echo "$CMD_NUM Sorry.Command < $@ > execute failed"
		fi
	done


# 多主机传输文件函数封装
function remote_transfer

	SRC_FILE=$1
	DST_DIR=$2
	# 函数必须有2个参数,第一个参数是本地文件或目录,第二个参数为远端主机目录
	if [ $# -lt 2 ];then
		echo "Usage: $0 <file|dir> <dst_dir>"
		exit 1
	fi
	
	# 判断第1个参数是否存在,如果不存在则直接退出并提示给用户
	if [ ! -e $SRC_FILE ];then
		echo "ERROR - $SRC_FILE is not exist,Please check..."
		exit 1
	fi

	# 判断第2个参数,远端主机目录是否存在,如果不存在,则创建
	for host in $HOST_LIST;do
		echo "++++++++++++++++Transfer File To HOST: $host ++++++++++++++++"
		CMD_NUM=`expr $CMD_NUM + 1`
		# 判断第2个参数,远端主机目录是否存在;如果不存在,则创建
		
 		sshpass -p root ssh -o StrictHostKeyChecking=no root@$host "if [ ! -e $DST_DIR ];then mkdir $DST_DIR -p;fi"
		sshpass -p root scp -o StrictHostKeyChecking=no $SRC_FILE root@$host:$DST_DIR/
		if [ $? -eq 0 ];then
                        echo "Remote Host: $host - $CMD_NUM - INFO - SCP $SRC_FILE To dir $DST_DIR Success"
                else
       			echo "Remote Host: $host - $CMD_NUM - ERROR - SCP $SRC_FILE To dir $DST_DIR Failed" 
		fi
	done



#第一步:关闭firewalld和selinux
remote_execute "systemctl stop firewalld.service"
remote_execute "systemctl disable firewalld.service"
#remote_execute "setenforce 0"
remote_execute "sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux"

#第二步:安装配置JDK
remote_transfer $LOCAL_DIR/$JDK_NAME $PACKAGE_DIR
remote_execute "if [ ! -d $APP_DIR ];then mkdir -p $APP_DIR;fi"
remote_execute "tar zxvf $PACKAGE_DIR/$JDK_NAME -C $APP_DIR"

cat > $LOCAL_DIR/java.sh << EOF
export JAVA_HOME=/opt/source/jdk1.8.0_152
export PATH=\\$PATH:\\$JAVA_HOME/bin:\\$JAVA_HOME/jre/bin
export JAVA_HOME PATH
EOF

remote_transfer $LOCAL_DIR/java.sh /etc/profile.d/
remote_execute "source /etc/profile.d/java.sh"
remote_execute "java -version"


#第三步:安装配置zookeeper,并启动服务
remote_transfer $LOCAL_DIR/$ZK_NAME $PACKAGE_DIR
remote_execute "tar zxvf $PACKAGE_DIR/$ZK_NAME -C $APP_DIR"

remote_execute "if [ -e $APP_DIR/zookeeper ];then rm -f $APP_DIR/zookeeper;fi"
remote_execute "ln -sv $APP_DIR/apache-zookeeper-3.7.1-bin $APP_DIR/zookeeper"

remote_execute "cp $APP_DIR/zookeeper/conf/zoo_sample.cfg $APP_DIR/zookeeper/conf/zoo.cfg"

touch $LOCAL_DIR/zoo_tmp.conf
cat > $LOCAL_DIR/zoo_tmp.conf << EOF
server.1=192.168.126.136:2888:3888
server.2=192.168.126.137:2888:3888
server.3=192.168.126.138:2888:3888
EOF

remote_transfer $LOCAL_DIR/zoo_tmp.conf /tmp
remote_execute "cat /tmp/zoo_tmp.conf >> $APP_DIR/zookeeper/conf/zoo.cfg"

remote_execute "if [ -e /data/zk ];then rm -rf /data/zk; fi"
remote_execute "mkdir /data/zk -p"
remote_execute "sed -i 's/dataDir=\\/tmp\\/zookeeper/dataDir=\\/data\\/zk/g' $APP_DIR/zookeeper/conf/zoo.cfg"

#将myid写入到远端主机myid文件上
#这里的if方法使用的是单引号,原因是单引号可以忽略所有的字符;双引号会把 hostname 当成变量,在当前服务器就执行了,导致在执行时,hostname 会变成 linkhost02,而不是获取的远端主机的 hostname。
remote_execute 'if [ `hostname` == "linkhot02" ];then echo 1 > /data/zk/myid;fi'
remote_execute 'if [ `hostname` == "linkhot03" ];then echo 2 > /data/zk/myid;fi'
remote_execute 'if [ `hostname` == "linkhot04" ];then echo 3 > /data/zk/myid;fi'
# 查询远端主机的QuorumPeerMain进程id号,用来杀死当前已启动的zookeeper进程。
remote_execute "jps | grep QuorumPeerMain | grep -v grep | awk 'print $1' > /tmp/zk.pid"
remote_execute 'if [ -s /tmp/zk.pid ];then kill -9 `cat /tmp/zk.pid`;fi'
# 在保证没有zk进程运行的前提下,启动zookeeper服务
remote_execute "$APP_DIR/zookeeper/bin/zkServer.sh start"


#第四步:安装配置kafka,并启动服务


2.8 安装配置scala代码实现

(1)安装scala代码

# scala安装包
SCALA_NAME="scala-2.12.11.tgz"

……

#第四步:安装配置scala服务
# 上传scala安装包到远程目录,并解压
remote_transfer $LOCAL_DIR/$SCALA_NAME $PACKAGE_DIR
remote_execute "tar zxvf $PACKAGE_DIR/$SCALA_NAME -C $APP_DIR"

# 创建环境变量文件
cat > $LOCAL_DIR/scala.sh << EOF
export SCALA_HOME=$APP_DIR/scala-2.12.11
export PATH=\\$PATH:\\$SCALA_HOME/bin
export SCALA_HOME PATH
EOF

# 上传环境变量文件到远程目录,并执行source命令使之生效,查询版本信息
remote_transfer $LOCAL_DIR/scala.sh /etc/profile.d/
remote_execute "source /etc/profile.d/scala.sh"
remote_execute "scala -version"

(2)完整代码

#!/bin/bash
#

#删除旧日志
if [ -e ./deploy_kafka.log ];then
	rm -f ./deploy_kafka/log
fi
set -e
#实现将echo语句重定向到log中,就不需要每个echo都进行输出
exec 1>> ./deploy_kafka.log 2>&1

HOST_LIST="192.168.126.136 192.168.126.137 192.168.126.138"
CMD_NUM=0
LOCAL_DIR="/opt/tmp"
PACKAGE_DIR="/opt/package"
APP_DIR="/opt/source"
JDK_NAME="jdk-8u152-linux-x64.tar.gz"
ZK_NAME="apache-zookeeper-3.7.1-bin.tar.gz"
SCALA_NAME="scala-2.12.11.tgz"

# 多主机执行指令函数封装
function remote_execute

	for host in $HOST_LIST;do
		CMD_NUM=`expr $CMD_NUM + 1`
		echo "++++++++++Execute Command < $@ > ON Host: $host +++++++++++"
		sshpass -p root ssh -o StrictHostKeyChecking=no root@$host $@
		if [ $? -eq 0 ];then
			echo "$CMD_NUM Congratulation.Command < $@ > execute success" 
		else
			echo "$CMD_NUM Sorry.Command < $@ > execute failed"
		fi
	done


# 多主机传输文件函数封装
function remote_transfer

	SRC_FILE=$1
	DST_DIR=$2
	# 函数必须有2个参数,第一个参数是本地文件或目录,第二个参数为远端主机目录
	if [ $# -lt 2 ];then
		echo "Usage: $0 <file|dir> <dst_dir>"
		exit 1
	fi
	
	# 判断第1个参数是否存在,如果不存在则直接退出并提示给用户
	if [ ! -e $SRC_FILE ];then
		echo "ERROR - $SRC_FILE is not exist,Please check..."
		exit 1
	fi

	# 判断第2个参数,远端主机目录是否存在,如果不存在,则创建
	for host in $HOST_LIST;do
		echo "++++++++++++++++Transfer File To HOST: $host ++++++++++++++++"
		CMD_NUM=`expr $CMD_NUM + 1`
		# 判断第2个参数,远端主机目录是否存在;如果不存在,则创建
		
 		sshpass -p root ssh -o StrictHostKeyChecking=no root@$host "if [ ! -e $DST_DIR ];then mkdir $DST_DIR -p;fi"
		sshpass -p root scp -o StrictHostKeyChecking=no $SRC_FILE root@$host:$DST_DIR/
		if [ $? -eq 0 ];then
                        echo "Remote Host: $host - $CMD_NUM - INFO - SCP $SRC_FILE To dir $DST_DIR Success"
                else
       			echo "Remote Host: $host - $CMD_NUM - ERROR - SCP $SRC_FILE To dir $DST_DIR Failed" 
		fi
	done



#第一步:关闭firewalld和selinux
remote_execute "systemctl stop firewalld.service"
remote_execute "systemctl disable firewalld.service"
#remote_execute "setenforce 0"
remote_execute "sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux"

#第二步:安装配置JDK
remote_transfer $LOCAL_DIR/$JDK_NAME $PACKAGE_DIR
remote_execute "if [ ! -d $APP_DIR ];then mkdir -p $APP_DIR;fi"
remote_execute "tar zxvf $PACKAGE_DIR/$JDK_NAME -C $APP_DIR"

cat > $LOCAL_DIR/java.sh << EOF
export JAVA_HOME=/opt/source/jdk1.8.0_152
export PATH=\\$PATH:\\$JAVA_HOME/bin:\\$JAVA_HOME/jre/bin
export JAVA_HOME PATH
EOF

remote_transfer $LOCAL_DIR/java.sh /etc/profile.d/
remote_execute "source /etc/profile.d/java.sh"
remote_execute "java -version"


#第三步:安装配置zookeeper,并启动服务
remote_transfer $LOCAL_DIR/$ZK_NAME $PACKAGE_DIR
remote_execute "tar zxvf $PACKAGE_DIR/$ZK_NAME -C $APP_DIR"

remote_execute "if [ -e $APP_DIR/zookeeper ];then rm -f $APP_DIR/zookeeper;fi"
remote_execute "ln -sv $APP_DIR/apache-zookeeper-3.7.1-bin $APP_DIR/zookeeper"

remote_execute "cp $APP_DIR/zookeeper/conf/zoo_sample.cfg $APP_DIR/zookeeper/conf/zoo.cfg"

touch $LOCAL_DIR/zoo_tmp.conf
cat > $LOCAL_DIR/zoo_tmp.conf << EOF
server.1=192.168.126.136:2888:3888
server.2=192.168.126.137:2888:3888
server.3=192.168.126.138:2888:3888
EOF

remote_transfer $LOCAL_DIR/zoo_tmp.conf /tmp
remote_execute "cat /tmp/zoo_tmp.conf >> $APP_DIR/zookeeper/conf/zoo.cfg"

remote_execute "if [ -e /data/zk ];then rm -rf /data/zk; fi"
remote_execute "mkdir /data/zk -p"
remote_execute "sed -i 's/dataDir=\\/tmp\\/zookeeper/dataDir=\\/data\\/zk/g' $APP_DIR/zookeeper/conf/zoo.cfg"

#将myid写入到远端主机myid文件上
#这里的if方法使用的是单引号,原因是单引号可以忽略所有的字符;双引号会把 hostname 当成变量,在当前服务器就执行了,导致在执行时,hostname 会变成 linkhost02,而不是获取的远端主机的 hostname。
remote_execute 'if [ `hostname` == "linkhot02" ];then echo 1 > /data/zk/myid;fi'
remote_execute 'if [ `hostname` == "linkhot03" ];then echo 2 > /data/zk/myid;fi'
remote_execute 'if [ `hostname` == "linkhot04" ];then echo 3 > /data/zk/myid;fi'
# 查询远端主机的QuorumPeerMain进程id号,用来杀死当前已启动的zookeeper进程。
remote_execute "jps | grep QuorumPeerMain | grep -v grep | awk 'print $1' > /tmp/zk.pid"
remote_execute 'if [ -s /tmp/zk.pid ];then kill -9 `cat /tmp/zk.pid`;fi'
# 在保证没有zk进程运行的前提下,启动zookeeper服务
remote_execute "$APP_DIR/zookeeper/bin/zkServer.sh start"


#第四步:安装配置scala服务
remote_transfer $LOCAL_DIR/$SCALA_NAME $PACKAGE_DIR
remote_execute "tar zxvf $PACKAGE_DIR/$SCALA_NAME -C $APP_DIR"

cat > $LOCAL_DIR/scala.sh << EOF
export SCALA_HOME=$APP_DIR/scala-2.12.11
export PATH=\\$PATH:\\$SCALA_HOME/bin
export SCALA_HOME PATH
EOF

remote_transfer $LOCAL_DIR/scala.sh /etc/profile.d/
remote_execute "source /etc/profile.d/scala.sh"
remote_execute "scala -version"

# 第五步:按照配置kafka,并启动服务

2.9 安装配置kafka代码实现

(1)安装kafka代码实现

KAFKA_NAME="kafka_2.11-0.11.0.2.tgz"

……

# 第五步:按照配置kafka,并启动服务
remote_transfer $LOCAL_DIR/$KAFKA_NAME $PACKAGE_DIR
remote_execute "tar zxvf $PACKAGE_DIR/$KAFKA_NAME -C $APP_DIR"

remote_execute "if [ -e $APP_DIR/kafka ];then rm -rf $APP_DIR/kafka;fi"
remote_execute "ln -sv $APP_DIR/kafka_2.11-0.11.0.2 $APP_DIR/kafka"

remote_execute "if [ -e /data/kafka/log ];then rm -rf /data/kafka/log;fi"
remote_execute "mkdir -p /data/kafka/log"

remote_execute "sed -i '/zookeeper.connect=localhost:2181/d' $APP_DIR/kafka/config/server.properties"
remote_execute "sed -i '\\$azookeeper.connect=192.168.126.136:2181,192.168.126.137:2181,192.168.126.138:2181' $APP_DIR/kafka/config/server.properties"

remote_execute "if [ \\`hostname\\` == "linkhot02" ];then sed -i 's/broker.id=0/broker.id=100/g' $APP_DIR/kafka/config/server.properties;fi"
remote_execute "if [ \\`hostname\\` == "linkhot03" ];then sed -i 's/broker.id=0/broker.id=101/g' $APP_DIR/kafka/config/server.properties;fi"
remote_execute "if [ \\`hostname\\` == "linkhot04" ];then sed -i 's/broker.id=0/broker.id=102/g' $APP_DIR/kafka/config/server.properties;fi"

remote_execute "if [ \\`hostname\\` == "linkhot02" ];then sed -i '\\$alisteners=PLAINTEXT://192.168.126.136:9092' $APP_DIR/kafka/config/server.properties;fi"
remote_execute "if [ \\`hostname\\` == "linkhot03" ];then sed -i '\\$alisteners=PLAINTEXT://192.168.126.137:9092' $APP_DIR/kafka/config/server.properties;fi"
remote_execute "if [ \\`hostname\\` == "linkhot04" ];then sed -i '\\$alisteners=PLAINTEXT://192.168.126.138:9092' $APP_DIR/kafka/config/server.properties;fi"

remote_execute "sed -i 's/log.dirs=\\/tmp\\/kafka-logs/log.dirs=\\/data\\/kafka\\/log/g' $APP_DIR/kafka/config/server.properties"

remote_execute "jps | grep kafka | grep -v grep | awk 'print \\$1' > /tmp/kafka.pid"
remote_execute "if [ -s /tmp/kafka.pid ];then kill -9 \\`cat /tmp/kafka.pid\\`;fi"

remote_execute "$APP_DIR/kafka/bin/kafka-server-start.sh -daemon $APP_DIR/kafka/config/server.properties"

(2)完整代码

#!/bin/bash
#

#删除旧日志
if [ -e ./deploy_kafka.log ];then
	rm -f ./deploy_kafka/log
fi
set -e
#实现将echo语句重定向到log中,就不需要每个echo都进行输出
exec 1>> ./deploy_kafka.log 2>&1

HOST_LIST="192.168.126.136 192.168.126.137 192.168.126.138"
#HOST_LIST="192.168.126.138"
CMD_NUM=0
LOCAL_DIR="/opt/tmp"
PACKAGE_DIR="/opt/package"
APP_DIR="/opt/source"
JDK_NAME="jdk-8u152-linux-x64.tar.gz"
ZK_NAME="apache-zookeeper-3.7.1-bin.tar.gz"
SCALA_NAME="scala-2.12.11.tgz"
KAFKA_NAME="kafka_2.11-0.11.0.2.tgz"

# 多主机执行指令函数封装
function remote_execute

	for host in $HOST_LIST;do
		CMD_NUM=`expr $CMD_NUM + 1`
		echo "++++++++++Execute Command < $@ > ON Host: $host +++++++++++"
		sshpass -p root ssh -o StrictHostKeyChecking=no root@$host $@
		if [ $? -eq 0 ];then
			echo "$CMD_NUM Congratulation.Command < $@ > execute success" 
		else
			echo "$CMD_NUM Sorry.Command < $@ > execute failed"
		fi
	done


# 多主机传输文件函数封装
function remote_transfer

	SRC_FILE=$1
	DST_DIR=$2
	# 函数必须有2个参数,第一个参数是本地文件或目录,第二个参数为远端主机目录
	if [ $# -lt 2 ];then
		echo "Usage: $0 <file|dir> <dst_dir>"
		exit 1
	fi
	
	# 判断第1个参数是否存在,如果不存在则直接退出并提示给用户
	if [ ! -e $SRC_FILE ];then
		echo "ERROR - $SRC_FILE is not exist,Please check..."
		exit 1
	fi

	# 判断第2个参数,远端主机目录是否存在,如果不存在,则创建
	for host in $HOST_LIST;do
		echo "++++++++++++++++Transfer File To HOST: $host ++++++++++++++++"
		CMD_NUM=`expr $CMD_NUM + 1`
		# 判断第2个参数,远端主机目录是否存在;如果不存在,则创建
		
 		sshpass -p root ssh -o StrictHostKeyChecking=no root@$host "if [ ! -e $DST_DIR ];then mkdir $DST_DIR -p;fi"
		sshpass -p root scp -o StrictHostKeyChecking=no $SRC_FILE root@$host:$DST_DIR/
		if [ $? -eq 0 ];then
                        echo "Remote Host: $host - $CMD_NUM - INFO - SCP $SRC_FILE To dir $DST_DIR Success"
                else
       			echo "Remote Host: $host - $CMD_NUM - ERROR - SCP $SRC_FILE To dir $DST_DIR Failed" 
		fi
	done



#第一步:关闭firewalld和selinux
remote_execute "systemctl stop firewalld.service"
remote_execute "systemctl disable firewalld.service"
#remote_execute "setenforce 0"
remote_execute "sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux"

#第二步:安装配置JDK
remote_transfer $LOCAL_DIR/$JDK_NAME $PACKAGE_DIR
remote_execute "if [ ! -d $APP_DIR ];then mkdir -p $APP_DIR;fi"
remote_execute "tar zxvf $PACKAGE_DIR/$JDK_NAME -C $APP_DIR"

cat > $LOCAL_DIR/java.sh << EOF
export JAVA_HOME=/opt/source/jdk1.8.0_152
export PATH=\\$PATH:\\$JAVA_HOME/bin:\\$JAVA_HOME/jre/bin
export JAVA_HOME PATH
EOF

remote_transfer $LOCAL_DIR/java.sh /etc/profile.d/
remote_execute "source /etc/profile.d/java.sh"
remote_execute "java -version"


#第三步:安装配置zookeeper,并启动服务
remote_transfer $LOCAL_DIR/$ZK_NAME $PACKAGE_DIR
remote_execute "tar zxvf $PACKAGE_DIR/$ZK_NAME -C $APP_DIR"

remote_execute "if [ -e $APP_DIR/zookeeper ];then rm -f $APP_DIR/zookeeper;fi"
remote_execute "ln -sv $APP_DIR/apache-zookeeper-3.7.1-bin $APP_DIR/zookeeper"

remote_execute "cp $APP_DIR/zookeeper/conf/zoo_sample.cfg $APP_DIR/zookeeper/conf/zoo.cfg"

touch $LOCAL_DIR/zoo_tmp.conf
cat > $LOCAL_DIR/zoo_tmp.conf << EOF
server.1=192.168.126.136:2888:3888
server.2=192.168.126.137:2888:3888
server.3=192.168.126.138:2888:3888
EOF

remote_transfer $LOCAL_DIR/zoo_tmp.conf /tmp
remote_execute "cat /tmp/zoo_tmp.conf >> $APP_DIR/zookeeper/conf/zoo.cfg"

remote_execute "if [ -e /data/zk ];then rm -rf /data/zk; fi"
remote_execute "mkdir /data/zk -p"
remote_execute "sed -i 's/dataDir=\\/tmp\\/zookeeper/dataDir=\\/data\\/zk/g' $APP_DIR/zookeeper/conf/zoo.cfg"

#将myid写入到远端主机myid文件上
#这里的if方法使用的是单引号,原因是单引号可以忽略所有的字符;双引号会把 hostname 当成变量,在当前服务器就执行了,导致在执行时,hostname 会变成 linkhost02,而不是获取的远端主机的 hostname。
remote_execute 'if [ `hostname` == "linkhot02" ];then echo 1 > /data/zk/myid;fi'
remote_execute 'if [ `hostname` == "linkhot03" ];then echo 2 > /data/zk/myid;fi'
remote_execute 'if [ `hostname` == "linkhot04" ];then echo 3 > /data/zk/myid;fi'
# 查询远端主机的QuorumPeerMain进程id号,用来杀死当前已启动的zookeeper进程。
remote_execute "jps | grep QuorumPeerMain | grep -v grep | awk 'print $1' > /tmp/zk.pid"
remote_execute 'if [ -s /tmp/zk.pid ];then kill -9 `cat /tmp/zk.pid`;fi'
# 在保证没有zk进程运行的前提下,启动zookeeper服务
remote_execute "$APP_DIR/zookeeper/bin/zkServer.sh start"


#第四步:安装配置scala服务
remote_transfer $LOCAL_DIR/$SCALA_NAME $PACKAGE_DIR
remote_execute "tar zxvf $PACKAGE_DIR/$SCALA_NAME -C $APP_DIR"

cat > $LOCAL_DIR/scala.sh << EOF
export SCALA_HOME=$APP_DIR/scala-2.12.11
export PATH=\\$PATH:\\$SCALA_HOME/bin
export SCALA_HOME PATH
EOF

remote_transfer $LOCAL_DIR/scala.sh /etc/profile.d/
remote_execute "source /etc/profile.d/scala.sh"
remote_execute "scala -version"

# 第五步:按照配置kafka,并启动服务
remote_transfer $LOCAL_DIR/$KAFKA_NAME $PACKAGE_DIR
remote_execute "tar zxvf $PACKAGE_DIR/$KAFKA_NAME -C $APP_DIR"

remote_execute "if [ -e $APP_DIR/kafka ];then rm -rf $APP_DIR/kafka;fi"
remote_execute "ln -sv $APP_DIR/kafka_2.11-0.11.0.2 $APP_DIR/kafka"

remote_execute "if [ -e /data/kafka/log ];then rm -rf /data/kafka/log;fi"
remote_execute "mkdir -p /data/kafka/log"

remote_execute "sed -i '/zookeeper.connect=localhost:2181/d' $APP_DIR/kafka/config/server.properties"
remote_execute "sed -i '\\$azookeeper.connect=192.168.126.136:2181,192.168.126.137:2181,192.168.126.138:2181' $APP_DIR/kafka/config/server.properties"

remote_execute "if [ \\`hostname\\` == "linkhot02" ];then sed -i 's/broker.id=0/broker.id=100/g' $APP_DIR/kafka/config/server.properties;fi"
remote_execute "if [ \\`hostname\\` == "linkhot03" ];then sed -i 's/broker.id=0/broker.id=101/g' $APP_DIR/kafka/config/server.properties;fi"
remote_execute "if [ \\`hostname\\` == "linkhot04" ];then sed -i 's/broker.id=0/broker.id=102/g' $APP_DIR/kafka/config/server.properties;fi"

remote_execute "if [ \\`hostname\\` == "linkhot02" ];then sed -i '\\$alisteners=PLAINTEXT://192.168.126.136:9092' $APP_DIR/kafka/config/server.properties;fi"
remote_execute "if [ \\`hostname\\` == "linkhot03" ];then sed -i '\\$alisteners=PLAINTEXT://192.168.126.137:9092' $APP_DIR/kafka/config/server.properties;fi"
remote_execute "if [ \\`hostname\\` == "linkhot04" ];then sed -i '\\$alisteners=PLAINTEXT://192.168.126.138:9092' $APP_DIR/kafka/config/server.properties;fi"

remote_execute "sed -i 's/log.dirs=\\/tmp\\/kafka-logs/log.dirs=\\/data\\/kafka\\/log/g' $APP_DIR/kafka/config/server.properties"

remote_execute "jps | grep kafka | grep -v grep | awk 'print \\$1' > /tmp/kafka.pid"
remote_execute "if [ -s /tmp/kafka.pid ];then kill -9 \\`cat /tmp/kafka.pid\\`;fi"

remote_execute "$APP_DIR/kafka/bin/kafka-server-start.sh -daemon $APP_DIR/kafka/config/server.properties"

2.10 验证 kafka 集群运行状态

sleep 30

remote_execute "if [ `hostname` == "linkhot02" ];then $APP_DIR/kafka/bin/kafka-topic.sh --zookeeper localhost --create --topic test --partitions 5 --relication-factor=2;fi"

sleep 5

remote_execute "if [ \\`hostname\\` == "node01" ];then $APP_DIR/kafka/bin/kafka-topics.sh --zookeeper localhost  --topic test --describe;fi"

如下图,kafka的 topic 创建成功执行了。

2.11 最终版不熟脚本

#!/bin/bash
#

#删除旧日志
if [ -e ./deploy_kafka.log ];then
	rm -f ./deploy_kafka/log
fi
set -e
#实现将echo语句重定向到log中,就不需要每个echo都进行输出
exec 1>> ./deploy_kafka.log 2>&1

HOST_LIST="192.168.126.136 192.168.126.137 192.168.126.138"
#HOST_LIST="192.168.126.138"
CMD_NUM=0
LOCAL_DIR="/opt/tmp"
PACKAGE_DIR="/opt/package"
APP_DIR="/opt/source"
JDK_NAME="jdk-8u152-linux-x64.tar.gz"
ZK_NAME="apache-zookeeper-3.7.1-bin.tar.gz"
SCALA_NAME="scala-2.12.11.tgz"
KAFKA_NAME="kafka_2.11-0.11.0.2.tgz"

# 多主机执行指令函数封装
function remote_execute

	for host in $HOST_LIST;do
		CMD_NUM=`expr $CMD_NUM + 1`
		echo "++++++++++Execute Command < $@ > ON Host: $host +++++++++++"
		sshpass -p root ssh -o StrictHostKeyChecking=no root@$host $@
		if [ $? -eq 0 ];then
			echo "$CMD_NUM Congratulation.Command < $@ > execute success" 
		else
			echo "$CMD_NUM Sorry.Command < $@ > execute failed"
		fi
	done


# 多主机传输文件函数封装
function remote_transfer

	SRC_FILE=$1
	DST_DIR=$2
	# 函数必须有2个参数,第一个参数是本地文件或目录,第二个参数为远端主机目录
	if [ $# -lt 2 ];then
		echo "Usage: $0 <file|dir> <dst_dir>"
		exit 1
	fi
	
	# 判断第1个参数是否存在,如果不存在则直接退出并提示给用户
	if [ ! -e $SRC_FILE ];then
		echo "ERROR - $SRC_FILE is not exist,Please check..."
		exit 1
	fi

	# 判断第2个参数,远端主机目录是否存在,如果不存在,则创建
	for host in $HOST_LIST;do
		echo "++++++++++++++++Transfer File To HOST: $host ++++++++++++++++"
		CMD_NUM=`expr $CMD_NUM + 1`
		# 判断第2个参数,远端主机目录是否存在;如果不存在,则创建
		
 		sshpass -p root ssh -o StrictHostKeyChecking=no root@$host "if [ ! -e $DST_DIR ];then mkdir $DST_DIR -p;fi"
		sshpass -p root scp -o StrictHostKeyChecking=no $SRC_FILE root@$host:$DST_DIR/
		if [ $? -eq 0 ];then
                        echo "Remote Host: $host - $CMD_NUM - INFO - SCP $SRC_FILE To dir $DST_DIR Success"
                else
       			echo "Remote Host: $host - $CMD_NUM - ERROR - SCP $SRC_FILE To dir $DST_DIR Failed" 
		fi
	done



#第一步:关闭firewalld和selinux
remote_execute "systemctl stop firewalld.service"
remote_execute "systemctl disable firewalld.service"
#remote_execute "setenforce 0"
remote_execute "sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux"

#第二步:安装配置JDK
remote_transfer $LOCAL_DIR/$JDK_NAME $PACKAGE_DIR
remote_execute "if [ ! -d $APP_DIR ];then mkdir -p $APP_DIR;fi"
remote_execute "tar zxvf $PACKAGE_DIR/$JDK_NAME -C $APP_DIR"

cat > $LOCAL_DIR/java.sh << EOF
export JAVA_HOME=/opt/source/jdk1.8.0_152
export PATH=\\$PATH:\\$JAVA_HOME/bin:\\$JAVA_HOME/jre/bin
export JAVA_HOME PATH
EOF

remote_transfer $LOCAL_DIR/java.sh /etc/profile.d/
remote_execute "source /etc/profile.d/java.sh"
remote_execute "java -version"


#第三步:安装配置zookeeper,并启动服务
remote_transfer $LOCAL_DIR/$ZK_NAME $PACKAGE_DIR
remote_execute "tar zxvf $PACKAGE_DIR/$ZK_NAME -C $APP_DIR"

remote_execute "if [ -e $APP_DIR/zookeeper ];then rm -f $APP_DIR/zookeeper;fi"
remote_execute "ln -sv $APP_DIR/apache-zookeeper-3.7.1-bin $APP_DIR/zookeeper"

remote_execute "cp $APP_DIR/zookeeper/conf/zoo_sample.cfg $APP_DIR/zookeeper/conf/zoo.cfg"

touch $LOCAL_DIR/zoo_tmp.conf
cat > $LOCAL_DIR/zoo_tmp.conf << EOF
server.1=192.168.126.136:2888:3888
server.2=192.168.126.137:2888:3888
server.3=192.168.126.138:2888:3888
EOF

remote_transfer $LOCAL_DIR/zoo_tmp.conf /tmp
remote_execute "cat /tmp/zoo_tmp.conf >> $APP_DIR/zookeeper/conf/zoo.cfg"

remote_execute "if [ -e /data/zk ];then rm -rf /data/zk; fi"
remote_execute "mkdir /data/zk -p"
remote_execute "sed -i 's/dataDir=\\/tmp\\/zookeeper/dataDir=\\/data\\/zk/g' $APP_DIR/zookeeper/conf/zoo.cfg"

#将myid写入到远端主机myid文件上
#这里的if方法使用的是单引号,原因是单引号可以忽略所有的字符;双引号会把 hostname 当成变量,在当前服务器就执行了,导致在执行时,hostname 会变成 linkhost02,而不是获取的远端主机的 hostname。
remote_execute 'if [ `hostname` == "linkhot02" ];then echo 1 > /data/zk/myid;fi'
remote_execute 'if [ `hostname` == "linkhot03" ];then echo 2 > /data/zk/myid;fi'
remote_execute 'if [ `hostname` == "linkhot04" ];then echo 3 > /data/zk/myid;fi'
# 查询远端主机的QuorumPeerMain进程id号,用来杀死当前已启动的zookeeper进程。
remote_execute "jps | grep QuorumPeerMain | grep -v grep | awk 'print $1' > /tmp/zk.pid"
remote_execute 'if [ -s /tmp/zk.pid ];then kill -9 `cat /tmp/zk.pid`;fi'
# 在保证没有zk进程运行的前提下,启动zookeeper服务
remote_execute "$APP_DIR/zookeeper/bin/zkServer.sh start"


#第四步:安装配置scala服务
remote_transfer $LOCAL_DIR/$SCALA_NAME $PACKAGE_DIR
remote_execute "tar zxvf $PACKAGE_DIR/$SCALA_NAME -C $APP_DIR"

cat > $LOCAL_DIR/scala.sh << EOF
export SCALA_HOME=$APP_DIR/scala-2.12.11
export PATH=\\$PATH:\\$SCALA_HOME/bin
export SCALA_HOME PATH
EOF

remote_transfer $LOCAL_DIR/scala.sh /etc/profile.d/
remote_execute "source /etc/profile.d/scala.sh"
remote_execute "scala -version"

# 第五步:按照配置kafka,并启动服务
remote_transfer $LOCAL_DIR/$KAFKA_NAME $PACKAGE_DIR
remote_execute "tar zxvf $PACKAGE_DIR/$KAFKA_NAME -C $APP_DIR"

remote_execute "if [ -e $APP_DIR/kafka ];then rm -rf $APP_DIR/kafka;fi"
remote_execute "ln -sv $APP_DIR/kafka_2.11-0.11.0.2 $APP_DIR/kafka"

remote_execute "if [ -e /data/kafka/log ];then rm -rf /data/kafka/log;fi"
remote_execute "mkdir -p /data/kafka/log"

remote_execute "sed -i '/zookeeper.connect=localhost:2181/d' $APP_DIR/kafka/config/server.properties"
remote_execute "sed -i '\\$azookeeper.connect=192.168.126.136:2181,192.168.126.137:2181,192.168.126.138:2181' $APP_DIR/kafka/config/server.properties"

remote_execute "if [ \\`hostname\\` == "linkhot02" ];then sed -i 's/broker.id=0/broker.id=100/g' $APP_DIR/kafka/config/server.properties;fi"
remote_execute "if [ \\`hostname\\` == "linkhot03" ];then sed -i 's/broker.id=0/broker.id=101/g' $APP_DIR/kafka/config/server.properties;fi"
remote_execute "if [ \\`hostname\\` == "linkhot04" ];then sed -i 's/broker.id=0/broker.id=102/g' $APP_DIR/kafka/config/server.properties;fi"

remote_execute "if [ \\`hostname\\` == "linkhot02" ];then sed -i '\\$alisteners=PLAINTEXT://192.168.126.136:9092' $APP_DIR/kafka/config/server.properties;fi"
remote_execute "if [ \\`hostname\\` == "linkhot03" ];then sed -i '\\$alisteners=PLAINTEXT://192.168.126.137:9092' $APP_DIR/kafka/config/server.properties;fi"
remote_execute "if [ \\`hostname\\` == "linkhot04" ];then sed -i '\\$alisteners=PLAINTEXT://192.168.126.138:9092' $APP_DIR/kafka/config/server.properties;fi"

remote_execute "sed -i 's/log.dirs=\\/tmp\\/kafka-logs/log.dirs=\\/data\\/kafka\\/log/g' $APP_DIR/kafka/config/server.properties"

remote_execute "jps | grep kafka | grep -v grep | awk 'print \\$1' > /tmp/kafka.pid"
remote_execute "if [ -s /tmp/kafka.pid ];then kill -9 \\`cat /tmp/kafka.pid\\`;fi"

remote_execute "$APP_DIR/kafka/bin/kafka-server-start.sh -daemon $APP_DIR/kafka/config/server.properties"

sleep 30

remote_execute "if [ `hostname` == "linkhot02" ];then $APP_DIR/kafka/bin/kafka-topic.sh --zookeeper localhost --create --topic test --partitions 5 --relication-factor=2;fi"

sleep 5

remote_execute "if [ \\`hostname\\` == "node01" ];then $APP_DIR/kafka/bin/kafka-topics.sh --zookeeper localhost  --topic test --describe;fi"

以上是关于利用SSH实现分布式应用的一键安装部署的主要内容,如果未能解决你的问题,请参考以下文章

利用jenkins一键部署项目

[云原生专题-54]:Kubesphere云治理-操作-通过K8S的应用仓库一键部署微服务应用- 分布式协调服务中间件zookeeper的安装与部署

如何利用容器实现生产级别的redis sharding集群的一键交付

自定义Gradle Task实现一键批量部署Springboot应用

k3s单机版安装部署 附一键安装脚本

原创 :nfs软件服务利用ansible实现一键化部署