Shell脚本实现自动安装zookeeper
程序员文章站
2022-04-16 17:24:22
a:本脚本运行的机器,linux rhel6
b,c,d,...:待安装zookeeper cluster的机器, linux rhel6
首先在脚本运行的机器a上确定...
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