RHEL自动安装zookeeper的shell脚本

RHEL自动安装zookeeper的shell脚本

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
#   [email protected]
#
# @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="[email protected]"

    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]}
        [email protected]$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 ‘#!{{[email protected]==>‘ >> $ZOO_CFG"`

        for s in ${servers[*]}
        do
            ret=`ssh $destlogin "echo ‘$s‘ >> $ZOO_CFG"`
        done

        ret=`ssh $destlogin "echo ‘#!<[email protected]}}‘ >> $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

时间: 2024-12-15 01:33:36

RHEL自动安装zookeeper的shell脚本的相关文章

Linux自动安装JDK的shell脚本

Linux自动安装JDK的shell脚本 A:本脚本运行的机器,Linux B:待安装JDK的机器, Linux 首先在脚本运行的机器A上确定可以ssh无密码登录到待安装jdk的机器B上,然后就可以在A上运行本脚本: $ ./install-jdk.sh B的IP or: $ ./install-jdk.sh "B的IP" "JDK的URI" 就可以在机器B上安装JDK.jdk使用的tar包需要用户自己设定DEFAULT_JDK_SRC=?,保证可以wget得到即可

自动安装zabbix客户端shell脚本

一般工作中要部署新服务器,部署完成还要添加zabbix监控,所以安装zabbix客户端是很频繁的,我这里写了一个简单的自动安装脚本,不是很完善,但也可以平时拿来用. 脚本运行的时候可能会出来一些错误,可以看终止时的错误信息,很可能是缺少相关的包,根据信息安装包再重新运行即可. 注意修改配置的内容,要根据实际情况做相应的修改,比如server和hostname,server是服务端的ip,根据实际情况配置,hostname我这里设置的是客户端自己的ip,也可以根据自身情况修改.在获取本地ip时,这

CentOS7自动安装Bind服务器shell脚本

#!/bin/bash #################################################################### # Auto install bind # Create Date :  2018-05-31 # Written by :风花 # Organization:  hbgslz.com #################################################################### DOMAIN_

一个简单的监控网站是否正常并自动重启服务的shell脚本

#!/bin/sh if [ -z "`curl --connect-timeout 15 --max-time 20 --head --silent http://localhost/index.php|head -n 1|grep '200'`" ];then echo -e "$(date +%Y-%m-%d)\n" killall nginx killall php-fpm /usr/local/nginx/sbin/nginx -c conf/nginx.

一键安装MySQL 主从复制SHELL脚本

脚本思路: 在从库上远程备份主库数据,再在从库上进行数据恢复,同时启动从库. 环境: CentOS6.5 已安装MySQL多实例,端口为3306 ,3307. 现通过自动安装主从脚本将两实例做主从复制. #!/bin/bash . /etc/rc.d/init.d/functions if [ $# -ne 0 ];then    echo "Usage:sh `basename $0`"    exit 1 fi #MASTER VARIABLES MASTER_USER=root

linux (centos) 安装tomcat (用shell脚本安装为服务)

1.下载tomcat 从http://tomcat.apache.org/download-70.cgi链接下载linux版本(tar.gz) 2.解压,上传及赋权限 解压,用WinSCP把解压的安装文件上传到/home/software/tomcat目录 并用   chmod -R 755 /home/software/tomcat命令账权限. 3.编写安装为服务的脚本并运行 shell脚本如下: #! /bin/sh #shell script takes care of starting

ping主机的ip地址,判断以及自动安装sysstat软件包 等脚本

ping主机的ip地址.返回结果 vi pinghost.sh #!/bin/bash ping -c 3 -i 0.2 -W 3 $1 &> /dev/null if [ $? -eq 0 ] then echo "Host $1 is up." else echo "Host $1 is down." fi sh pinghost.sh ip地址 判断分数所在的区间,给出不同的分档结果 #!/bin/bash read -p "请输入您的

下载Google官方/CM Android源代码自动重新开始的Shell脚本

国内由于某种原因,下载CM或Google官方的Android源代码总容易中断.总看着机器,一中断就重新执行repo sync还太麻烦,所以我特意编写了一段shell脚本(download.sh).通过获取shell最后返回的状态码来决定是否再次执行repo sync命令. #!/bin/bash echo "======start repo sync======" repo sync # 第一次下载android源代码 while [ $? != 0 ]; do echo "

redhat 下安装jdk的shell脚本

每次都要卸载系统跟随的openjdk然后装orcle的jdk实在烦人,所以 编写一个脚本执行就省心了,只要将下面的bash脚本和你所在下载的 jdk-7u79-linux-x64.tar.gz包放在同一个文件夹即可. 别忘记,给脚本执行权限 #chmod +x shellname.sh #../shellname.sh #!/bin/bash #shell script to install jdk # 1. remove openjdk if exists. for i in $(rpm -q