Zookeeper离线安装脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Zookeeper离线安装脚本相关的知识,希望对你有一定的参考价值。

#!/bin/bash
#********************************************************************
#Author: yuankun
#Date: 2023-03-18
#FileName: install_zookeeper.sh
#Description: The test script
#Copyright (C): 2023 All rights reserved
#********************************************************************
JDK_FILE="jdk-8u321-linux-x64.tar.gz"
#JDK_FILE="jdk-11.0.12_linux-x64_bin.tar.gz"
ZOOKEEPER_FILE=apache-zookeeper-3.7.1-bin
TAR=tar.gz
ZOOKEEPER_DIR=/usr/local/src
DIR=`pwd`
JDK_DIR="/usr/local"

color ()
RES_COL=60
MOVE_TO_COL="echo -en \\\\033[$RES_COLG"
SETCOLOR_SUCCESS="echo -en \\\\033[1;32m"
SETCOLOR_FAILURE="echo -en \\\\033[1;31m"
SETCOLOR_WARNING="echo -en \\\\033[1;33m"
SETCOLOR_NORMAL="echo -en \\E[0m"
echo -n "$2" && $MOVE_TO_COL
echo -n "["
if [ $1 = "success" -o $1 = "0" ] ;then
$SETCOLOR_SUCCESS
echo -n $" OK "
elif [ $1 = "failure" -o $1 = "1" ] ;then
$SETCOLOR_FAILURE
echo -n $"FAILED"
else
$SETCOLOR_WARNING
echo -n $"WARNING"
fi
$SETCOLOR_NORMAL
echo -n "]"
echo


install_jdk()
if ! [ -f "$DIR/$JDK_FILE" ];then
color 1 "$JDK_FILE 文件不存在"
exit;
elif [ -d $JDK_DIR/jdk ];then
color 1 "JDK 已经安装"
exit
else
[ -d "$JDK_DIR" ] || mkdir -pv $JDK_DIR
fi
tar xvf $DIR/$JDK_FILE -C $JDK_DIR
cd $JDK_DIR && ln -s jdk* jdk

cat > /etc/profile.d/jdk.sh <<EOF
export JAVA_HOME=$JDK_DIR/jdk
#export JRE_HOME=\\$JAVA_HOME/jre
#export CLASSPATH=.:\\$JAVA_HOME/lib/:\\$JRE_HOME/lib/
export PATH=\\$PATH:\\$JAVA_HOME/bin
EOF
. /etc/profile.d/jdk.sh
java -version && color 0 "JDK 安装完成" || color 1 "JDK 安装失败" ; exit;



install_zookeeper()
cd $DIR
if [ -f "$ZOOKEEPER_FILE.$TAR" ]
then
echo 正在安装zookeeper,请稍等......
mv $ZOOKEEPER_FILE.$TAR $ZOOKEEPER_DIR;cd $ZOOKEEPER_DIR
tar xf $ZOOKEEPER_FILE.$TAR;ln -s $ZOOKEEPER_DIR/$ZOOKEEPER_FILE $ZOOKEEPER_DIR/zookeeper
rm -rf $ZOOKEEPER_FILE.$TAR
mkdir -p /data/zookeeper;cd $ZOOKEEPER_DIR/zookeeper/conf
cp zoo_sample.cfg zoo.cfg;sed -i s/dataDir=\\/tmp\\/zookeeper/dataDir=\\/data\\/zookeeper/ zoo.cfg
cat > /usr/lib/systemd/system/zookeeper.service <<EOF
[Unit]
Description=Zookeeper Service unit Configuration
After=network.target

[Service]
Type=forking
Environment=JAVA_HOME=$JDK_DIR/jdk
ExecStart=$ZOOKEEPER_DIR/zookeeper/bin/zkServer.sh start $ZOOKEEPER_DIR/zookeeper/conf/zoo.cfg
ExecStop=$ZOOKEEPER_DIR/zookeeper/bin/zkServer.sh stop
KillMode=none
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload;systemctl enable --now zookeeper
if [ $? -eq 0 ]
then
echo zookeeper启动成功!安装完成

else
echo zookeeper启动失败!安装失败
fi
else
echo 文件不存在,退出安装!
exit;

fi

install_jdk
install_zookeeper

Linux脚本 03shell脚本离线安装配置集结JDK+InfluxDB+Zookeeper+Kafka(安装文件及脚本源码网盘分享)

安装文件及shell脚本网盘分享:

链接:https://pan.baidu.com/s/1ZQE6Chfvur_u_iefhbtFKQ 
提取码:l6nw

包含文件:

以下脚本仅针对特定离线版本安装,方便快速部署。

1.脚本

  • JDK安装脚本:jdkInstall.sh
  • 时序数据库InfluxDB安装脚本:influxdbInstall.sh
  • Zookeeper安装脚本:zookeeperInstall.sh
  • Kafka安装脚本:kafkaInstall.sh

2.使用说明

按需将安装文件与 shell 安装脚本放在同一个文件夹下。

(1)使用 chmod +x *.sh 命令给shell 脚本赋可执行权限。

(2)由于脚本设置了全局变量,执行需要使用 source xxx.sh 命令。

  • source jdkInstall.sh
  • source influxdbInstall.sh
  • source zookeeperInstall.sh
  • source kafkaInstall.sh

2.1 JDK安装脚本

脚本仅适用于jdk-8u241-linux-x64.tar.gz的安装部署。

  • 步骤 (2/3) JAVA_HOME文件夹可自定义,默认为/usr/local/java/jdk1.8.0_241
#!/bin/bash
rm -rf /usr/local/java/jdk1.8.0_241

echo "(1/3): 解压JDK安装文件..."
mkdir -p /usr/local/java/jdk1.8.0_241
tar -zxvf jdk-8u241-linux-x64.tar.gz
sleep 10
echo "(1/3): JDK安装文件解压完毕"

echo "(2/3): 配置JDK环境变量..."
mv -f jdk1.8.0_241/ /usr/local/java
sleep 2
cat <<'EOF' > /etc/profile.d/java.sh
export JAVA_HOME=/usr/local/java/jdk1.8.0_241
export PATH=$PATH:$JAVA_HOME/bin
EOF
sleep 1
source /etc/profile.d/java.sh
echo "JAVA_HOME目录:"$JAVA_HOME
echo "(2/3): JDK环境变量配置完毕"

echo "(3/3): 验证JDK版本..."
java -version
echo "(3/3): JDK版本验证完毕"

2.2 时序数据库InfluxDB安装脚本

脚本仅适用于influxdb-1.7.6.x86_64.rpm的安装部署。

  • 步骤 (2/6) 配置数据存储位置,根据实际情况进行配置。
  • 步骤 (4/6) 创建admin用户及数据库,根据实际情况进行配置。
#!/bin/bash 
# yum -y remove influxdb.x86_64

echo "(1/6): 安装InfluxDB..."
yum localinstall -y influxdb-1.7.6.x86_64.rpm
echo "(1/6): InfluxDB安装完毕"

echo "(2/6): 配置InfluxDB运行参数..."
mkdir -p /home/influxdb/meta
mkdir -p /home/influxdb/data
mkdir -p /home/influxdb/wal

chown -R influxdb:influxdb /home/influxdb

sed -i "26c dir = \\"/home/influxdb/meta\\"" /etc/influxdb/influxdb.conf
sed -i "45c dir = \\"/home/influxdb/data\\"" /etc/influxdb/influxdb.conf
sed -i "48c wal-dir = \\"/home/influxdb/wal\\"" /etc/influxdb/influxdb.conf
sed -i "247c enabled = true" /etc/influxdb/influxdb.conf
echo "(2/6): InfluxDB运行参数配置完毕"

echo "(3/6): 首次启动InfluxDB..."
systemctl start influxd
sleep 3
systemctl status influxd
echo "(3/6): InfluxDB首次启动完毕"

echo "(4/6): 创建用户及数据库..."
influx  -execute 'create user "dsj" with password '\\''dsj'\\'' with all privileges;' -format 'json' -pretty
sleep 1
influx  -execute 'create database "sjzt";' -format 'json' -pretty
sleep 1
echo "(4/6): 用户及数据库创建完毕"

echo "(5/6): 开启权限认证并重启InfluxDB..."
sed -i "259c auth-enabled = true" /etc/influxdb/influxdb.conf
systemctl restart influxd
sleep 3
systemctl status influxd
echo "(5/6): 开启权限认证并重启InfluxDB完毕"

echo "(6/6): 配置InfluxDB开机启动..."
cat <<EOF >> /etc/rc.loal
systemctl start influxd
EOF
echo "(6/6): InfluxDB开机启动配置完毕"

2.3 Zookeeper安装脚本

脚本仅适用于apache-zookeeper-3.6.2-bin.tar.gz的安装部署。

  • 步骤 (2/6) 内ZOOKEEPER_HOME可自定义
  • 步骤 (3/6) 内运行参数可配置
  • 步骤 (6/6) 内JAVA_HOME需根据实际情况进行修改,默认为/usr/local/java/jdk1.8.0_241
#!/bin/bash
rm -rf /usr/local/zookeeper

echo "(1/6): 解压Zookeeper安装文件..."
tar -zxvf apache-zookeeper-3.6.2-bin.tar.gz
sleep 10
echo "(1/6): Zookeeper安装文件解压完毕"

echo "(2/6): 配置Zookeeper环境变量..."
mv ./apache-zookeeper-3.6.2-bin/ /usr/local/zookeeper
sleep 2
cat <<'EOF' > /etc/profile.d/zookeeper.sh
export ZOOKEEPER_HOME=/usr/local/zookeeper
export PATH=$PATH:$ZOOKEEPER_HOME/bin
EOF
sleep 1
source /etc/profile.d/zookeeper.sh
echo "ZOOKEEPER_HOME目录:"$ZOOKEEPER_HOME
echo "(2/6): 配置Zookeeper环境变量完毕"

echo "(3/6): 配置Zookeeper运行参数..."
cp $ZOOKEEPER_HOME/conf/zoo_sample.cfg $ZOOKEEPER_HOME/conf/zoo.cfg
sed -i "12c dataDir=/usr/local/zookeeper/data" $ZOOKEEPER_HOME/conf/zoo.cfg
sed -i "13i dataLogDir=/usr/local/zookeeper/datalog" $ZOOKEEPER_HOME/conf/zoo.cfg
echo "(3/6): Zookeeper运行参数配置完毕"

echo "(4/6): 初次启动Zookeeper..."
zkServer.sh start
sleep 2
zkServer.sh status
echo "(4/6): Zookeeper初次启动完毕"

echo "(5/6): 开启2181端口..."
firewall-cmd --zone=public --add-port=2181/tcp --permanent && firewall-cmd --reload
echo "(5/6): 2181端口开启完毕"

echo "(6/6): Zookeeper加入到service服务并设置开机自启..."
cat <<'EOF' > /etc/rc.d/init.d/zookeeper
#!/bin/bash
# chkconfig: 2345 10 90
# description: service zookeeper
# processname: zookeeper
export JAVA_HOME=/usr/local/java/jdk1.8.0_241
ZOOKEEPER_HOME=/usr/local/zookeeper
case $1 in
     start) su root $ZOOKEEPER_HOME/bin/zkServer.sh start;;
     start-foreground) su root $ZOOKEEPER_HOME/bin/zkServer.sh start-foreground;;
     stop) su root $ZOOKEEPER_HOME/bin/zkServer.sh stop;;
     status) su root $ZOOKEEPER_HOME/bin/zkServer.sh status;;
     restart) su root $ZOOKEEPER_HOME/bin/zkServer.sh restart;;
     upgrade) su root $ZOOKEEPER_HOME/bin/zkServer.sh upgrade;;
     print-cmd) su root $ZOOKEEPER_HOME/bin/zkServer.sh print-cmd;;
     *) echo "require start|start-foreground|stop|status|restart|print-cmd";;
esac
EOF
chmod +x /etc/rc.d/init.d/zookeeper
chkconfig --add /etc/rc.d/init.d/zookeeper
chkconfig zookeeper on
echo "(6/6): Zookeeper加入到service服务并设置开机自启完毕"

2.4 Kafka安装脚本

脚本仅适用于kafka_2.12-2.6.0.tgz的安装部署。

  • 步骤 (2/6) 内KAFKA_HOME可自定义,Kafka会存储较多数据,需选择空间较大磁盘。
  • 步骤 (3/6) 内运行参数可配置
  • 步骤 (6/6) 内JAVA_HOME需根据实际情况进行修改,默认为/usr/local/java/jdk1.8.0_241
#!/bin/bash
rm -rf /usr/local/kafka

echo "(1/6): 解压Kafka安装文件..."
tar -zxvf kafka_2.12-2.6.0.tgz
sleep 10
echo "(1/6): Kafka安装文件解压完毕"

echo "(2/6): 配置Kafka环境变量..."
mv ./kafka_2.12-2.6.0/ /usr/local/kafka
sleep 2
cat <<'EOF' > /etc/profile.d/kafka.sh
export KAFKA_HOME=/usr/local/kafka
export PATH=$PATH:$KAFKA_HOME/bin
EOF
sleep 1
source /etc/profile.d/kafka.sh
echo "KAFKA_HOME目录:"$KAFKA_HOME
echo "(2/6): Kafka环境变量配置完毕"

echo "(3/6): 配置Kafka运行参数..."
sed -i "32i listeners=PLAINTEXT://localhost:9092" $KAFKA_HOME/config/server.properties
sed -i "61c log.dirs=$KAFKA_HOME/kafka-logs" $KAFKA_HOME/config/server.properties
echo "(3/6): Kafka运行参数配置完毕"

echo "(4/6): 首次启动Kafka..."
kafka-server-start.sh -daemon $KAFKA_HOME/config/server.properties
echo "(4/6): Kafka首次启动完毕"

echo "(5/6): 开启9092端口..."
firewall-cmd --zone=public --add-port=9092/tcp --permanent && firewall-cmd --reload
echo "(5/6): 9092端口开启完毕"

echo "(6/6): Kafka加入到service服务并设置开机自启..."
cat <<'EOF' > /etc/rc.d/init.d/kafka
#!/bin/bash
# chkconfig: 2345 40 60
# description: service kafka
# processname: kafka
export JAVA_HOME=/usr/local/java/jdk1.8.0_241
KAFKA_HOME=/usr/local/kafka
case $1 in
	start) $KAFKA_HOME/bin/kafka-server-start.sh -daemon $KAFKA_HOME/config/server.properties;;
	stop) $KAFKA_HOME/bin/kafka-server-stop.sh;;
	status) $JAVA_HOME/bin/jps | grep Kafka;;
	restart) $KAFKA_HOME/bin/kafka-server-stop.sh
			 $KAFKA_HOME/bin/kafka-server-start.sh -daemon $KAFKA_HOME/config/server.properties;;
	*) echo "require start|stop|status|restart";;
esac
EOF
chmod +x /etc/rc.d/init.d/kafka
chkconfig --add /etc/rc.d/init.d/kafka
chkconfig kafka on
echo "(6/6): Kafka加入到service服务并设置开机自启完毕"

以上是关于Zookeeper离线安装脚本的主要内容,如果未能解决你的问题,请参考以下文章

hadoop离线day01--大数据导读Apache Zookeeper

五.MySQL安装:二进制离线安装脚本(5.6版本)

windows下的zookeeper安装

linux安装zookeeper集群保姆教程,包括集群启停脚本

使用PowerShell 自动安装.NET4.6.1离线包

docker离线安装脚本