linux shell

关注公众号 jb51net

关闭
首页 > 脚本专栏 > linux shell > shell脚本部署zookeeper集群

shell脚本中一键部署zookeeper集群服务的方法

作者:码海小虾米_

Zookeeper是一个开源的分布式的,为分布式框架提供协调服务的Apache项目,这篇文章主要介绍了shell脚本一键部署zookeeper集群服务的方法,需要的朋友可以参考下

一键部署zookeeper集群服务

1. 编写脚本情况分析

2. 脚本代码部分

#!/bin/bash
#一键部署zookeeper集群服务器脚本
# 判断/opt目录下是否有指定安装包
if [ -e /opt/apache-zookeeper-3.5.7-bin.tar.gz ];then
    continue
else
    echo -e '\033[35m /opt目录中没有安装包[apache-zookeeper-3.5.7-bin.tar.gz]\033[0m'
    exit
fi
# 配置yum本地源
yumm (){
        if ls /etc/yum.repos.d/ | grep repo.bak &> /dev/null
        then
            mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/repo.bak/
        else
            mkdir /etc/yum.repos.d/repo.bak/
            mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/repo.bak/
        fi
        echo -e "[local]\nname=local\nbaseurl=file:///mnt\nenable=1\ngpgcheck=0" > /etc/yum.repos.d/local.repo
        if df -h | grep /dev/sr0 &> /dev/null
        then
            umount /dev/sr0
            mount /dev/cdrom /mnt &> /dev/null
        else
            mount /dev/cdrom /mnt &> /dev/null
            if [ $? -ne 0 ]
            then
                echo "光盘不存在,检查是否加载镜像,状态是否已连接!"
                exit
            fi
        fi
}
yumm
if yum clean all &> /dev/null
then
    if yum makecache &> /dev/null
    then
        echo "yum本地源配置完成!可以正常安装程序!"
    else
        echo "yum缓存建立失败!"
    fi
else
    echo "yum缓存清除失败......"        
fi
read -p "当前服务器的myid为:" num
read -p "输入要设置的zookeeper服务器1的ip:" ip1
read -p "输入要设置的zookeeper服务器2的ip:" ip2
read -p "输入要设置的zookeeper服务器3的ip:" ip3
yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
cd /opt/
tar zxvf apache-zookeeper-3.5.7-bin.tar.gz
mv apache-zookeeper-3.5.7-bin /usr/local/zookeeper-3.5.7 &> /dev/null
cd /usr/local/zookeeper-3.5.7/conf/ 
/usr/bin/cp zoo_sample.cfg zoo.cfg
sed -i '12 s/tmp\/zookeeper/usr\/local\/zookeeper-3.5.7\/data/' /usr/local/zookeeper-3.5.7/conf/zoo.cfg
sed -i '13c dataLogDir=\/usr\/local\/zookeeper-3.5.7\/logs' /usr/local/zookeeper-3.5.7/conf/zoo.cfg
sed -i '15c server.1='$ip1':3188:3288' /usr/local/zookeeper-3.5.7/conf/zoo.cfg
sed -i '16c server.2='$ip2':3188:3288' /usr/local/zookeeper-3.5.7/conf/zoo.cfg
sed -i '17c server.3='$ip3':3188:3288' /usr/local/zookeeper-3.5.7/conf/zoo.cfg
mkdir /usr/local/zookeeper-3.5.7/data &> /dev/null
mkdir /usr/local/zookeeper-3.5.7/logs &> /dev/null
echo $num > /usr/local/zookeeper-3.5.7/data/myid
echo '#!/bin/bash
#chkconfig:2345 20 90
#description: Zookeeper Service Control Script
ZK_HOME='/usr/local/zookeeper-3.5.7'
case $1 in
start)
echo "-----zookeeper启动-----"
$ZK_HOME/bin/zkServer.sh start
;;
stop)
echo "----zookeeper停止-------"
$ZK_HOME/bin/ zkServer.sh stop
;;
restart)
echo "----zookeeper重启-------"
$ZK_HOME/bin/zkServer.sh restart
;;
status)
echo  "-----zookeeper状态------"
$ZK_HOME/bin/zkServer.sh status
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
esac' > /etc/init.d/zookeeper
chmod +x /etc/init.d/zookeeper
chkconfig --add zookeeper
echo "所有服务执行完脚本后执行 service zookeeper start 开启服务"
echo "所有服务执行完脚本后执行 service zookeeper status 查看状态"

3. 实验测试部分

1.在三台服务器的/opt目录下放入指定zookeeper安装包apache-zookeeper-3.5.7-bin.tar.gz和一键部署脚本。

2.执行脚本,输入myid和服务器IP

3.验证结果

到此这篇关于shell脚本之一键部署zookeeper集群服务的文章就介绍到这了,更多相关shell脚本部署zookeeper集群内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文