上一节:函数封装
②脚本安装java环境、脚本安装配置zookeeper、scala、kafka
1 脚本一键部署kafka分布式应用
1.1 脚本安装配置java环境
准备好java安装包,存放到/opt/tmp目录下。我这里使用的是jdk-8u212-linux-x64.tar.gz,在网上找对应的版本下载即可。
1.1.1 部署流程思路
步骤一:传输安装包;
步骤二:判断路径存在,若不存在则创建source路径;
步骤三:解压安装包到应用路径;
步骤四:配置环境变量;
步骤五:验证java是否安装成功。
LOCAL_DIR="/opt/tmp"
PACKAGE_DIR="/opt/package"
APP_DIR="/opt/source"
JDK_NAME="jdk-8u212-linux-x64.tar.gz"
# 安装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_212
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"
执行:
sh install.sh
tailf output.log
验证:
ll /opt/tmp
-rw-r--r-- 1 root root 116 Aug 8 03:47 java.sh
-rw-r--r-- 1 root root 195013152 Aug 7 23:37 jdk-8u212-linux-x64.tar.gz
ll /opt/package
-rw-r--r-- 1 root root 195013152 Aug 8 03:47 jdk-8u212-linux-x64.tar.gz
ll /opt/source
drwxr-xr-x 7 10 143 245 Apr 2 2019 jdk1.8.0_212
1.2 脚本安装配置zookeeper
准备好zookeeper安装包,存放到/opt/tmp目录下。我这里使用的是apache-zookeeper-3.7.2-bin.tar.gz,在网上找对应的版本下载即可。
ZK_NAME="apache-zookeeper-3.7.2-bin.tar.gz"
1.2.1 部署流程思路
步骤一:管理主机传输zookeeper安装包到三节点;
remote_transfer $LOCAL_DIR/$ZK_NAME $PACKAGE_DIR
步骤二:解压安装包;
tar -zxvf apache-zookeeper-3.7.2-bin.tar.gz -C /opt/source/
remote_execute "tar zxvf $PACKAGE_DIR/$ZK_NAME -C $APP_DIR"
步骤三:创建zookeeper软连接;
ln -sv /opt/source/apache-zookeeper-3.7.2-bin/ /opt/source/zookeeper
remote_execute "if [ -e $APP_DIR/zookeeper ];then rm -f $APP_DIR/zookeeper"
remote_execute "ln -sv $APP_DIR/apache-zookeeper-3.7.2-bin $APP_DIR/zookeeper"
步骤四:修改zookeeper配置文件;
(1)、样例配置文件更名
cp zoo_sample.cfg zoo.cfg
remote_execute "cp $APP_DIR/zookeeper/conf/zoo_sample.cfg $APP_DIR/zookeeper/conf/zoo.cfg"
(2)、配置文件zoo.cfg末尾增加内容
server.1=10.0.1.10:2888:3888
server.2=10.0.1.20:2888:3888
server.3=10.0.1.30:2888:3888
cat > $LOCAL_DIR/zoo_tmp.conf << EOF
server.1=10.0.1.10:2888:3888
server.2=10.0.1.20:2888:3888
server.3=10.0.1.30:2888:3888
EOF
remote_transfer $LOCAL_DIR/zoo_tmp.conf /tmp
remote_execute "cat /tmp/zoo_tmp.conf >> $APP_DIR/zookeeper/conf/zoo.cfg"
(3)、创建zookeeper数据目录
mkdir /data/zk -pv
remote_execute "if [ -e /data/zk ];then rm -rf /data/zk;fi"
remote_execute "mkdir /data/zk -pv"
(4)、配置文件数据目录更改
dataDir=/tmp/zookeeper更改为dataDir=/data/zk
remote_execute "sed -i 's/dataDir=\/tmp\/zookeeper/dataDir=\/data\/zk/g' $APP_DIR/zookeeper/conf/zoo.cfg"
(5)、数据目录生成唯一的myid文件
node01:echo 1> /data/zk/myid
node02:echo 2 > /data/zk/myid
node03:echo 3 > /data/zk/myid
remote_execute 'if [ `hostname` == "node01" ];then echo 1 > /data/zk/myid;fi'
remote_execute 'if [ `hostname` == "node02" ];then echo 2 > /data/zk/myid;fi'
remote_execute 'if [ `hostname` == "node03" ];then echo 3 > /data/zk/myid;fi'
步骤五:判断主机是否有zookeeper进程,如有则kill
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'
步骤六:启动服务并验证。
/opt/source/zookeeper/bin/zkServer.sh start
/opt/source/zookeeper/bin/zkServer.sh status
remote_execute "$APP_DIR/zookeeper/bin/zkServer.sh start"
remote_execute "$APP_DIR/zookeeper/bin/zkServer.sh status"
完整代码:
ZK_NAME="apache-zookeeper-3.7.2-bin.tar.gz"
# 安装配置zookeeper,并启动服务
function install_zk
{
remote_transfer $LOCAL_DIR/$ZK_NAME $PACKAGE_DIR
remote_execute "tar zxf $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.2-bin $APP_DIR/zookeeper"
remote_execute "cp $APP_DIR/zookeeper/conf/zoo_sample.cfg $APP_DIR/zookeeper/conf/zoo.cfg"
cat > $LOCAL_DIR/zoo_tmp.conf << EOF
server.1=10.0.1.10:2888:3888
server.2=10.0.1.20:2888:3888
server.3=10.0.1.30: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 -pv"
remote_execute "sed -i 's/dataDir=\/tmp\/zookeeper/dataDir=\/data\/zk/g' $APP_DIR/zookeeper/conf/zoo.cfg"
remote_execute 'if [ `hostname` == "node01" ];then echo 1 > /data/zk/myid;fi'
remote_execute 'if [ `hostname` == "node02" ];then echo 2 > /data/zk/myid;fi'
remote_execute 'if [ `hostname` == "node03" ];then echo 3 > /data/zk/myid;fi'
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'
remote_execute "$APP_DIR/zookeeper/bin/zkServer.sh start"
remote_execute "$APP_DIR/zookeeper/bin/zkServer.sh status"
}
install_zk
验证:
[root@node01 shell]# tailf output.log
64 ++++command < /opt/source/zookeeper/bin/zkServer.sh start > in host: node01
ZooKeeper JMX enabled by default
Using config: /opt/source/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
successful.command < /opt/source/zookeeper/bin/zkServer.sh start >
65 ++++command < /opt/source/zookeeper/bin/zkServer.sh start > in host: node02
ZooKeeper JMX enabled by default
Using config: /opt/source/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
successful.command < /opt/source/zookeeper/bin/zkServer.sh start >
66 ++++command < /opt/source/zookeeper/bin/zkServer.sh start > in host: node03
ZooKeeper JMX enabled by default
Using config: /opt/source/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
successful.command < /opt/source/zookeeper/bin/zkServer.sh start >
1.3 脚本安装配置scala
准备好scala安装包,存放到/opt/tmp目录下。我这里使用的是scala-2.12.11.tgz,在网上找对应的版本下载即可。
1.3.1 部署流程思路
第一步:管理主机(/opt/tmp)传输scala安装包到远端生产主机目录(/opt/package);
第二步:远端生产主机解压缩scala安装包到安装目录(/opt/source);
第三步:管理主机(/opt/tmp)本地生成scala环境变量配置文件scala.sh;
第四步:管理主机传输scala.sh到所有远端生产主机/etc/profile.d/下;
第五步:所有远端生产主机使用source指令生效scala环境变量;
第六步:使用scala-version验证scala环境是否配置成功。
SCALA_NAME="scala-2.12.11.tgz"
function install_scala
{
remote_transfer $LOCAL_DIR/$SCALA_NAME $PACKAGE_DIR
remote_execute "tar zxf $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"
}
install_scala
验证:
[root@node01 shell]# tailf output.log
82 ++++command < scala -version > in host: node01
Scala code runner version 2.12.11 -- Copyright 2002-2020, LAMP/EPFL and Lightbend, Inc.
successful.command < scala -version >
83 ++++command < scala -version > in host: node02
Scala code runner version 2.12.11 -- Copyright 2002-2020, LAMP/EPFL and Lightbend, Inc.
successful.command < scala -version >
84 ++++command < scala -version > in host: node03
Scala code runner version 2.12.11 -- Copyright 2002-2020, LAMP/EPFL and Lightbend, Inc.
successful.command < scala -version >
1.4 脚本安装配置kafka
1.4.1 部署流程思路
第一步:管理主机(/opt/tmp)传输kafka安装包到所有远端生产主机/opt/package
第二步:所有远端生产主机(/opt/package)解压缩kafka安装包到安装目录(/opt/source)
remote_transfer $LOCAL_DIR/$KAFKA_NAME $PACKAGE_DIR
remote_execute "tar zxf $PACKAGE_DIR/$KAFKA_NAME -C $APP_DIR"
第三步:创建kafka_2.12-2.6.1目录软连接,创建之前先判断软连接是否存在,如果存在则删除
remote_execute "if [ -e $APP_DIR/kafka ];then rm -f $APP_DIR/kafka;fi"
remote_execute "ln -sv $APP_DIR/kafka_2.12-2.6.1 $APP_DIR/kafka"
第四步:创建kafka数据存储目录;创建之前先判断该目录是否存在,如果存在则删除
remote_execute "if [ -e /data/kafka/log ];then rm -f /data/kafka/log"
remote_execute "mkdir -p /data/kafka/log"
第五步:修改kafka配置文件(/opt/source/kafka/config/server.properties)
(1)、修改内容
zookeeper.connect=localhost:2181替换为
zookeeper.connect=10.0.1.10:2181,10.0.1.20:2181,10.0.1.30:2181
remote_execute "sed -i '/zookeeper.connect=localhost:2181/d' $APP_DIR/kafka/config/server.properties"
remote_execute "sed -i '\$azookeeper.connect=10.0.1.10:2181,10.0.1.20:2181,10.0.1.30:2181' $APP_DIR/kafka/config/server.properties"
(2)、修改内容
broker.id=0修改为:
node01:broker.id=100
node02:broker.id=101
node03:broker.id=102
remote_execute "if [ \`hostname\` == "node01" ];then sed -i 's/broker.id=0/broker.id=100/g' $APP_DIR/kafka/config/server.properties;fi"
remote_execute "if [ \`hostname\` == "node02" ];then sed -i 's/broker.id=0/broker.id=101/g' $APP_DIR/kafka/config/server.properties;fi"
remote_execute "if [ \`hostname\` == "node03" ];then sed -i 's/broker.id=0/broker.id=102/g' $APP_DIR/kafka/config/server.properties;fi"
(3)、追加内容
node01:listeners=PLAINTEXT://10.0.1.10:9092
node02:listeners=PLAINTEXT://10.0.1.20:9092
node03:listeners=PLAINTEXT://10.0.1.30:9092
remote_execute "if [ \`hostname\` == "node01" ];then sed -i '\$alisteners=PLAINTEXT://10.0.1.10:9092' $APP_DIR/kafka/config/server.properties;fi"
remote_execute "if [ \`hostname\` == "node02" ];then sed -i '\$alisteners=PLAINTEXT://10.0.1.20:9092' $APP_DIR/kafka/config/server.properties;fi"
remote_execute "if [ \`hostname\` == "node03" ];then sed -i '\$alisteners=PLAINTEXT://10.0.1.30:9092' $APP_DIR/kafka/config/server.properties;fi"
(4)、修改内容
log.dirs=/tmp/kafka-logs修改为log.dirs=/data/kafka/log
remote_execute "sed -i 's/log.dirs=\/tmp\/kafka-logs/log.dirs=\/data\/kafka\/log/g' $APP_DIR/kafka/config/server.properties"
第六步:启动kafka服务;启动前先判断kafka进程是否存在,如果存在先杀死
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"
验证:
[root@node01 shell]# tailf output.log
136 ++++command < /opt/source/kafka/bin/kafka-server-start.sh -daemon /opt/source/kafka/config/server.properties > in host: node01
successful.command < /opt/source/kafka/bin/kafka-server-start.sh -daemon /opt/source/kafka/config/server.properties >
137 ++++command < /opt/source/kafka/bin/kafka-server-start.sh -daemon /opt/source/kafka/config/server.properties > in host: node02
successful.command < /opt/source/kafka/bin/kafka-server-start.sh -daemon /opt/source/kafka/config/server.properties >
138 ++++command < /opt/source/kafka/bin/kafka-server-start.sh -daemon /opt/source/kafka/config/server.properties > in host: node03
successful.command < /opt/source/kafka/bin/kafka-server-start.sh -daemon /opt/source/kafka/config/server.properties >
[root@node01 shell]# jps
31425 QuorumPeerMain
35350 Jps
32476 Kafka
第七步:创建测试topic(test),并查看详情以验证kafka环境是否配置成功。
remote_execute "$APP_DIR/kafka/bin/kafka-server-start.sh -daemon $APP_DIR/kafka/config/server.properties"
sleep 30
remote_execute "if [ \`hostname\` == "node01" ];then $APP_DIR/kafka/bin/kafka-topics.sh --zookeeper localhost --create --topic test --partitions 5 --replication-factor=2;fi"
sleep 5
remote_execute "if [ \`hostname\` == "node01" ];then $APP_DIR/kafka/bin/kafka-topics.sh --zookeeper localhost --topic test --describe;fi"
整个项目完整代码:
[root@node01 shell]# cat /shell/init-kafka.sh
#!/bin/bash
#
if [ -e ./output.log ];then
rm -rf ./output.log
fi
exec 1>> ./output.log 2>&1
set -e
# 初始化变量
HOST_LIST="node01 node02 node03"
USER_PASS="000000"
CMD_NUM=0
LOCAL_DIR="/opt/tmp"
PACKAGE_DIR="/opt/package"
APP_DIR="/opt/source"
JDK_NAME="jdk-8u212-linux-x64.tar.gz"
ZK_NAME="apache-zookeeper-3.7.2-bin.tar.gz"
SCALA_NAME="scala-2.12.11.tgz"
KAFKA_NAME="kafka_2.12-2.6.1.tgz"
# 多主机执行主机指令封装
function remote_execute
{
for host in $HOST_LIST;do
CMD_NUM=`expr $CMD_NUM + 1`
echo "$CMD_NUM ++++command < $@ > in host: $host"
sshpass -p$USER_PASS ssh -o StrictHostKeyChecking=no root@$host $@
if [ $? -eq 0 ];then
echo "successful.command < $@ > "
else
echo "sorry.command < $@ > "
fi
done
}
# 示例:remote_execute 'du'
# 多主机传输文件函数封装
function remote_transfer
{
SRC_FILE=$1
DST_DIR=$2
# 函数必须有2个参数,第一个参数是本地文件或目录,第二个参数为远端主机目录
if [ $# -lt 2 ];then
echo "Usage:$0 <file|dir <dst_dir>>"
exit 1
fi
# 判断第一个参数是否存在,如果不存在则直接退出并提示给用户
if [ ! -e $SRC_FILE ];then
echo "ERROR - $SEC_FIEL is not exist,Please check...."
exit 1
fi
for host in $HOST_LIST;do
echo "+++++++++++Transfer FILE TO HOST: $host+++++++++++"
CMD_NUM=`expr $CMD_NUM + 1`
# 判断目录参数是否存在,如果不存在则直接创建
ssh -o StrictHostKeyChecking=no root@$host "if [ ! -e $DST_DIR ];then mkdir $DST_DIR -p;fi"
scp -r -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 filed"
fi
done
}
# 示例:remote_transfer output.log /tmp/log
# 关闭firewalld和selinux
function stop_firewalld
{
remote_execute "systemctl stop firewalld"
remote_execute "systemctl disable firewalld"
remote_execute "setenforce 0"
remote_execute "sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux"
}
stop_firewalld
# 安装JDK
function install_java
{
remote_transfer $LOCAL_DIR/$JDK_NAME $PACKAGE_DIR
remote_execute "if [ ! -d $APP_DIR ];then mkdir -p $APP_DIR;fi"
remote_execute "tar zxf $PACKAGE_DIR/$JDK_NAME -C $APP_DIR"
cat > $LOCAL_DIR/java.sh << EOF
export JAVA_HOME=/opt/source/jdk1.8.0_212
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"
}
install_java
# 安装配置zookeeper,并启动服务
function install_zk
{
remote_transfer $LOCAL_DIR/$ZK_NAME $PACKAGE_DIR
remote_execute "tar zxf $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.2-bin $APP_DIR/zookeeper"
remote_execute "cp $APP_DIR/zookeeper/conf/zoo_sample.cfg $APP_DIR/zookeeper/conf/zoo.cfg"
cat > $LOCAL_DIR/zoo_tmp.conf << EOF
server.1=10.0.1.10:2888:3888
server.2=10.0.1.20:2888:3888
server.3=10.0.1.30: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 -pv"
remote_execute "sed -i 's/dataDir=\/tmp\/zookeeper/dataDir=\/data\/zk/g' $APP_DIR/zookeeper/conf/zoo.cfg"
remote_execute 'if [ `hostname` == "node01" ];then echo 1 > /data/zk/myid;fi'
remote_execute 'if [ `hostname` == "node02" ];then echo 2 > /data/zk/myid;fi'
remote_execute 'if [ `hostname` == "node03" ];then echo 3 > /data/zk/myid;fi'
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'
remote_execute "$APP_DIR/zookeeper/bin/zkServer.sh start"
remote_execute "$APP_DIR/zookeeper/bin/zkServer.sh status"
}
install_zk
# 安装配置scala环境
function install_scala
{
remote_transfer $LOCAL_DIR/$SCALA_NAME $PACKAGE_DIR
remote_execute "tar zxf $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"
}
install_scala
# 安装配置kafka,并启动服务
function install_kafka
{
remote_transfer $LOCAL_DIR/$KAFKA_NAME $PACKAGE_DIR
remote_execute "tar zxf $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.12-2.6.1 $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=10.0.1.10:2181,10.0.1.20:2181,10.0.1.30:2181' $APP_DIR/kafka/config/server.properties"
remote_execute "if [ \`hostname\` == "node01" ];then sed -i 's/broker.id=0/broker.id=100/g' $APP_DIR/kafka/config/server.properties;fi"
remote_execute "if [ \`hostname\` == "node02" ];then sed -i 's/broker.id=0/broker.id=101/g' $APP_DIR/kafka/config/server.properties;fi"
remote_execute "if [ \`hostname\` == "node03" ];then sed -i 's/broker.id=0/broker.id=102/g' $APP_DIR/kafka/config/server.properties;fi"
remote_execute "if [ \`hostname\` == "node01" ];then sed -i '\$alisteners=PLAINTEXT://10.0.1.10:9092' $APP_DIR/kafka/config/server.properties;fi"
remote_execute "if [ \`hostname\` == "node02" ];then sed -i '\$alisteners=PLAINTEXT://10.0.1.20:9092' $APP_DIR/kafka/config/server.properties;fi"
remote_execute "if [ \`hostname\` == "node03" ];then sed -i '\$alisteners=PLAINTEXT://10.0.1.30: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\` == "node01" ];then $APP_DIR/kafka/bin/kafka-topics.sh --zookeeper localhost --create --topic test --partitions 5 --replication-factor=2;fi"
sleep 5
remote_execute "if [ \`hostname\` == "node01" ];then $APP_DIR/kafka/bin/kafka-topics.sh --zookeeper localhost --topic test --describe;fi"
}
install_kafka
编写脚本的时候需要注意:
set -e
是一个命令,用来设置脚本在遇到错误时立即退出。
单引号和双引号的区别:
在Shell脚本中,双引号 " "
和单引号 ' '
都用于定义字符串,但它们之间有以下主要区别:
转义字符:
-
- 在双引号中,大多数特殊字符会被识别并解释,例如
$
、\
、!
(在某些Shell中)等。如果需要在字符串中包含这些特殊字符的字面值,可以使用反斜杠\
来转义它们。 - 在单引号中,所有特殊字符都会被当作普通字符处理,不会被解释或转义。这意味着即使使用
\
也无法转义特殊字符。
- 在双引号中,大多数特殊字符会被识别并解释,例如
变量替换:
-
- 在双引号中,变量会被替换为它们的值。例如,如果有一个变量
var="world"
,那么在字符串"Hello $var"
中,$var
会被替换为"world"
。 - 在单引号中,变量不会被替换,即使变量名前面有
$
符号。例如,'Hello $var'
会原样输出为Hello $var
。
- 在双引号中,变量会被替换为它们的值。例如,如果有一个变量
空格和换行:
-
- 双引号内的空格和换行会被保留为字符串的一部分。
- 单引号内的空格和换行也会被保留,但它们不会被解释为特殊字符。