解释:
ZK_VERSION=3.8.4 #ZK_VERSION=3.8.0 #ZK_VERSION=3.6.3 #ZK_VERSION=3.7.1 #ZK_URL=https://archive.apache.org/dist/zookeeper/zookeeper-${ZK_VERSION}/apache-zookeeper-${ZK_VERSION}-bin.tar.gz #3.5.0及以下版本下载链接 ZK_URL=https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-${ZK_VERSION}/apache-zookeeper-${ZK_VERSION}-bin.tar.gz #ZK_URL="https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/stable/apache-zookeeper-${ZK_VERSION}-bin.tar.gz" #ZK_URL=https://archive.apache.org/dist/zookeeper/zookeeper-${ZK_VERSION}/zookeeper-${ZK_VERSION}.bin.tar.gz #ZK_VERSION=3.4.14 #3.5.0以下版本下载链接 #ZK_URL=https://archive.apache.org/dist/zookeeper/zookeeper-${ZK_VERSION}/zookeeper-${ZK_VERSION}.tar.gz NODE1=172.16.0.180 NODE2=172.16.0.181 NODE3=172.16.0.182 . /etc/os-release HOST=`hostname -I|awk '{print $1}'` color () { RES_COL=60 MOVE_TO_COL="echo -en \\033[${RES_COL}G" 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 "$1" && $MOVE_TO_COL echo -n "[" if [ $2 = "success" -o $2 = "0" ] ;then ${SETCOLOR_SUCCESS} echo -n $" OK " elif [ $2 = "failure" -o $2 = "1" ] ;then ${SETCOLOR_FAILURE} echo -n $"FAILED" else ${SETCOLOR_WARNING} echo -n $"WARNING" fi ${SETCOLOR_NORMAL} echo -n "]" echo } zk_myid () { read -p "请输入node编号(默认为 1): " MYID if [ -z "$MYID" ] ;then MYID=1 elif [[ ! "$MYID" =~ ^[0-9]+$ ]];then color "请输入正确的node编号!" 1 exit else true fi } install_jdk() { if [ $ID = 'centos' -o $ID = 'rocky' ];then yum -y install java-1.8.0-openjdk-devel || { color "安装JDK失败!" 1; exit 1; } else apt update apt install openjdk-11-jdk -y || { color "安装JDK失败!" 1; exit 1; } #apt install openjdk-8-jdk -y || { color "安装JDK失败!" 1; exit 1; } fi java -version } install_zookeeper() { if [ -f apache-zookeeper-${ZK_VERSION}-bin.tar.gz ] ;then cp apache-zookeeper-${ZK_VERSION}-bin.tar.gz /usr/local/src/ else wget -P /usr/local/src/ --no-check-certificate $ZK_URL || { color "下载失败!" 1 ;exit ; } fi tar xf /usr/local/src/${ZK_URL##*/} -C /usr/local ln -s /usr/local/apache-zookeeper-*-bin/ /usr/local/zookeeper echo 'PATH=/usr/local/zookeeper/bin:$PATH' > /etc/profile.d/zookeeper.sh . /etc/profile.d/zookeeper.sh mkdir -p /usr/local/zookeeper/data echo $MYID > /usr/local/zookeeper/data/myid cat > /usr/local/zookeeper/conf/zoo.cfg <<EOF tickTime=2000 initLimit=10 syncLimit=5 dataDir=/usr/local/zookeeper/data clientPort=2181 maxClientCnxns=128 autopurge.snapRetainCount=3 autopurge.purgeInterval=24 server.1=${NODE1}:2888:3888 server.2=${NODE2}:2888:3888 server.3=${NODE3}:2888:3888 EOF cat > /lib/systemd/system/zookeeper.service <<EOF [Unit] Description=zookeeper.service After=network.target [Service] Type=forking #Environment=/usr/local/zookeeper ExecStart=/usr/local/zookeeper/bin/zkServer.sh start ExecStop=/usr/local/zookeeper/bin/zkServer.sh stop ExecReload=/usr/local/zookeeper/bin/zkServer.sh restart [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl enable --now zookeeper.service systemctl is-active zookeeper.service if [ $? -eq 0 ] ;then color "zookeeper 安装成功!" 0 else color "zookeeper 安装失败!" 1 exit 1 fi } zk_myid install_jdk install_zookeeper