文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Shell脚本实现自动安装zookeeper

2022-06-04 21:45

关注

A:本脚本运行的机器,Linux RHEL6
B,C,D,...:待安装zookeeper cluster的机器, Linux RHEL6

首先在脚本运行的机器A上确定可以ssh无密码登录到待安装zk的机器B,C,D,...上,然后就可以在A上运行本脚本:

$ ./install_zookeeper

前提:

B, C, D机器必须配置好repo,本脚本使用的是cdh5的repo, 下面的内容保存到:/etc/yum.repos.d/cloudera-cdh5.repo:

[cloudera-cdh5] # Packages for Cloudera's Distribution for Hadoop, Version 5, on RedHat or CentOS 6 x86_64 name=Cloudera's Distribution for Hadoop, Version 5 baseurl=http://archive.cloudera.com/cdh5/redhat/6/x86_64/cdh/5/ gpgkey = http://archive.cloudera.com/cdh5/redhat/6/x86_64/cdh/RPM-GPG-KEY-cloudera gpgcheck = 1 enabled = 1

自动安装脚本将自动在B,C,D机器上安装好zookeeper, 配置好相关配置文件。但没有启动它们。支持1,3,5,7个服务器。

#!/bin/bash  

#  

# @file  

#   install_zookeeper.sh  

#  

# @date  

#   2014-12-21  

#  

# @author  

#   cheungmine@hgdb.net  

#  

# @version  

#   0.0.1pre  

#  

# @usage  

#   ./install_zookeeper.sh  

################################################################################  

  

#***********************************************************  

# split_to_array  

#   split string into array  

#***********************************************************  

function split_to_array() {  

    OLD_IFS="$IFS"  

    IFS="$2"  

    array=($1)  

    IFS="$OLD_IFS"  

}  

  

  

#***********************************************************  

# install_zookeeper  

#   install zookeeper on 1, 3 or 5 servers  

#  

# Parameters:  

#   clientPort - the port at which the clients will connect to  

#   servers - varying arguments: 1, 3, 5, up to 7  

#     "zkServer:serverPort:appPort"  

#     zkServer - ipaddr of zookeeper server  

#     serverPort - communication port for zookeeper servers  

#     appPort - communication port between zookeeper with other applications  

#  

# Example:  

#   1) install_zookeeper 2181 zk1 zk2 zk3  

#   2) install_zookeeper 2181 192.168.122.201 192.168.122.202 192.168.122.203  

#   3) install_zookeeper "2181" "192.168.122.201:2888:3888" "192.168.122.202:2888:3888" "192.168.122.203:2888:3888"  

#   4) install_zookeeper "2181:/var/lib/zookeeper" "192.168.122.201:2888:3888" "192.168.122.202:2888:3888" "192.168.122.203:2888:3888"  

#***********************************************************  

ERR_INVALID_ZK_SERVERS=1001  

  

function install_zookeeper() {  

    echo -e "<INFO> install zookeeper on cluster ..."  

    #chk_root  

  

    local ret clientPort dataDir len i ZOO_CFG server serverPort appPort destip destlogin  

  

    serverPort=2888  

    appPort=3888  

  

    # parse the first argument  

    split_to_array $1 ":"  

  

    # the port at which the clients will connect  

    clientPort=${array[0]}  

  

    # the directory where the snapshot is stored  

    dataDir="/var/lib/zookeeper"  

    if [ ${#array[*]} -eq 2 ]; then  

        dataDir=${array[1]}  

    fi  

  

    echo -e "<INFO> clientPort: $clientPort"  

    echo -e "<INFO> dataDir: $dataDir"  

  

    # zookeeper configure file  

    ZOO_CFG="/usr/lib/zookeeper/conf/zoo.cfg"  

  

    # get list of servers: args  

    shift  

    local argc=$#  

  

    if [ $argc -eq 1 -o $argc -eq 3 -o $argc -eq 5 -o $argc -eq 7 ]; then  

        echo -e "<INFO> zookeeper servers in cluster: [$argc]"  

    else  

        echo -e "<ERROR> invalid zookeeper servers: [$argc]"  

        exit $ERR_INVALID_ZK_SERVERS;  

    fi  

  

    local argv="$@"  

  

    OLD_IFS="$IFS"  

    IFS=" "  

    local args=($argv)  

    IFS="$OLD_IFS"  

  

    # array variable  

    local ipaddrs=()  

    local servers=()  

  

    local sid=0  

    for a in ${args[@]}  

    do  

        let sid++  

  

        # check if server format is either of:  

        #   serverIP  

        # or:  

        #   serverIP:serverPort:appPort  

        split_to_array $a ":"  

        serverIP=${array[0]}  

  

        if [ ${#array[*]} -ne 3 ]; then  

            a="$serverIP:$serverPort:$appPort";  

        fi  

  

        local server="server.$sid=$a"  

        servers[sid-1]=$server  

        echo $server  

  

        ipaddrs[sid-1]=$serverIP  

    done  

  

    # output array to one line string: echo ${servers[@]}  

    # get length of array  

    len=${#servers[*]}  

    i=0  

    while [ $i -lt $len ]  

    do  

        let sid=i+1  

        destip=${ipaddrs[$i]}  

        destlogin=root@$destip  

        echo -e "<INFO> configuring server.$sid: $destip ...c"  

  

        ret=`ssh $destlogin "yum install -y zookeeper zookeeper-server && service zookeeper-server init --myid=$sid"`  

  

        ret=`ssh $destlogin "echo '#!{{install_zookeeper@hgdb.net==>' >> $ZOO_CFG"`  

  

        for s in ${servers[*]}  

        do  

            ret=`ssh $destlogin "echo '$s' >> $ZOO_CFG"`  

        done  

  

        ret=`ssh $destlogin "echo '#!<==install_zookeeper@hgdb.net}}' >> $ZOO_CFG"`  

  

        echo -e "OK."  

  

        let i++  

    done  

  

    echo "<INFO> zookeeper cluster installation completed successfully!"  

}  

  

#=======================================================================  

install_zookeeper "2181" "192.168.122.201" "192.168.122.202" "192.168.122.203"

根据配置修改最后一行:

install_zookeeper "2181" "192.168.122.201" "192.168.122.202" "192.168.122.203"

注意:需要把zk-cluster的每台机器上的防火墙停掉,再启动zookeeper:

$ /usr/lib/zookeeper/bin/zkServer.sh start-foreground

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-服务器
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯