一键部署zookeeper集群

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一键部署zookeeper集群相关的知识,希望对你有一定的参考价值。

参考技术A #!/bin/sh

#install

IS_REDO="NONE"

NODE_NUM=0

SERVER_IP=""

WORK_DIR=$(dirname "$0")

cd $WORK_DIR

if [ "X"$WORK_DIR = "X" ] ; then

WORK_DIR="."

fi

while :

do

IS_REDO="NONE"

NODE_NUM=0

SERVER_IP=""

echo -n "请输入需要安装DataOS的服务器IP地址(单机格式:IP,集群格式:IP1,IP2,IP3) => "

read INSTALL_NODES_IPS

if [ "X"$INSTALL_NODES_IPS = "X" ] ; then

echo -e "输入IP[33[31m"$i"e[0m]信息无效,重新输入"

continue

fi

for i in `echo $INSTALL_NODES_IPS | sed 's/,/ /g'`

do

if ! echo $i | grep '[1-9][0-9]0,2.[0-9]1,3.[0-9]1,3.[1-9][0-9]0,2' ; then

echo -e "输入IP[33[31m"$i"e[0m]信息无效"

IS_REDO="REDO"

break

fi

SERVER_IP=$(echo $i | awk -F'.' 'if((!($1 ~ /^0/) && !($4 ~ /^0/)) && ((length($2)>1 && !($2 ~ /^0/)) || length($2)==1) && ((length($3)>1 && !($3 ~ /^0/)) || length($3)==1)) printf "%d.%d.%d.%d",$1,$2,$3,$4' | awk -F'.' 'if(($1>0 && $1=0 && $2=0 && $30 && $4<255)) printf "%d.%d.%d.%d",$1,$2,$3,$4')

if [ "X"$SERVER_IP = "X" ] ; then

echo -e "输入IP[33[31m"$i"e[0m]信息无效"

IS_REDO="REDO"

break

fi

NODE_NUM=$(($NODE_NUM+1))

done

if [ "X"$IS_REDO = "XNONE" ] ; then

if [ $NODE_NUM -ne 3 -a $NODE_NUM -ne 1 ] ; then

echo -e "输入IP信息校验通过,但不满足[33[31m3个节点e[0m]的DataOS集群初始化要求"

continue

fi

echo -e "输入IP信息校验通过,将初始化[33[32m"$NODE_NUM"个节点e[0m]的DataOS集群"

rm -rf $WORK_DIR/../nodes/*

for i in `echo $INSTALL_NODES_IPS | sed 's/,/ /g'`

do

mkdir -p $WORK_DIR"/../nodes/"$i

done

break

else

continue

fi

done

#################检查DataOS集群各节点的软件环境,安装zookeeper服务

IDX=0

SERVER_IP=""

for SERVER_IP in `ls $WORK_DIR"/../nodes/"`

do

let IDX=$IDX+1 && touch $WORK_DIR/../nodes/$SERVER_IP/id=$IDX

done

echo "请输入需要安装zookeeper服务的路径(例如:/data01) =>"

read zkPath

ZOOKEEPER_DATA_DIR=$zkPath/zookeeper/data

ZOOKEEPER_LOG_DIR=$zkPath/zookeeper/logs

tar -zxf $WORK_DIR/../software/zookeeper.tar.gz -C /tmp

if [[ -n $(cat /tmp/zookeeper/conf/zoo.cfg |grep dataDir) ]] ;then

sed -i "s/^dataDir=.*/dataDir=$ZOOKEEPER_DATA_DIR//////g" /tmp/zookeeper/conf/zoo.cfg

else

echo "dataDir=$ZOOKEEPER_DATA_DIR" >> /tmp/zookeeper/conf/zoo.cfg

fi

if [[ -n $(cat /tmp/zookeeper/conf/zoo.cfg |grep dataLogDir) ]] ;then

sed -i "s/^dataLogDir=.*/dataLogDir=$ZOOKEEPER_LOG_DIR//////g" /tmp/zookeeper/conf/zoo.cfg

else

echo "dataLogDir=$ZOOKEEPER_LOG_DIR" >> /tmp/zookeeper/conf/zoo.cfg

fi

IDX=0

SERVER_IP=""

NodeIP1=""

NodeIP2=""

NodeIP3=""

for SERVER_IP in `ls $WORK_DIR/../nodes/*/id=* | sed 's/// /g' | sed 's/=/ /g' | awk 'printf "%s %s ",$(NF-2),$NF' | sort -nk 2 | awk 'print $1'`

do

let IDX=$IDX+1

source $WORK_DIR"/../scripts/initCluster" $SERVER_IP || exit 255

if [ $IDX -eq 1 ] ; then

NodeIP1=$SERVER_IP

elif [ $IDX -eq 2 ]; then

NodeIP2=$SERVER_IP

else

NodeIP3=$SERVER_IP

fi

done

echo "server1.serverid=$NodeIP1:2888:3888" >> /tmp/zookeeper/conf/zoo.cfg

echo "server2.serverid=$NodeIP2:2888:3888" >> /tmp/zookeeper/conf/zoo.cfg

echo "server3.serverid=$NodeIP3:2888:3888" >> /tmp/zookeeper/conf/zoo.cfg

IDX=0

SERVER_IP=""

for SERVER_IP in `ls $WORK_DIR/../nodes/*/id=* | sed 's/// /g' | sed 's/=/ /g' | awk 'printf "%s %s ",$(NF-2),$NF' | sort -nk 2 | awk 'print $1'`

do

let IDX=$IDX+1

if [ $IDX -eq 1 ] ; then

echo 1 > /tmp/zookeeper/data/myid

sshpass -p "$SYSUSER_PASSWORD" scp -o StrictHostKeyChecking=no -P $SSH_PORT /tmp/zookeeper $SYSUSER_NAME@$NodeIP1:$zkPath

elif [ $IDX -eq 2 ]; then

echo 2 > /tmp/zookeeper/data/myid

sshpass -p "$SYSUSER_PASSWORD" scp -o StrictHostKeyChecking=no -P $SSH_PORT /tmp/zookeeper $SYSUSER_NAME@$NodeIP2:$zkPath

else

echo 3 > /tmp/zookeeper/data/myid

sshpass -p "$SYSUSER_PASSWORD" scp -r -o StrictHostKeyChecking=no -P $SSH_PORT /tmp/zookeeper $SYSUSER_NAME@$NodeIP3:$zkPath

fi

done

以上便是zookeeper集群的一键部署shell脚本,其中引用的initCluster脚本,将在后续更新。

以上是关于一键部署zookeeper集群的主要内容,如果未能解决你的问题,请参考以下文章

shell脚本之一键部署zookeeper集群服务,能用脚本解决的事情,绝对不手敲。

Zookeeper集群部署

zookeeper集群+kafka集群 部署

zookeeper高可用集群部署

zookeeper集群+kafka集群 部署

ZooKeeper 集群部署