您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Shell腳本中怎么自動安裝zookeeper,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
A:本腳本運行的機器,Linux RHEL6
B,C,D,...:待安裝zookeeper cluster的機器, Linux RHEL6
首先在腳本運行的機器A上確定可以ssh無密碼登錄到待安裝zk的機器B,C,D,...上,然后就可以在A上運行本腳本:
代碼如下:
$ ./install_zookeeper
前提:
B, C, D機器必須配置好repo,本腳本使用的是cdh6的repo, 下面的內容保存到:/etc/yum.repos.d/cloudera-cdh6.repo:
代碼如下:
[cloudera-cdh6]
# 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/cdh6/redhat/6/x86_64/cdh/5/
gpgkey = http://archive.cloudera.com/cdh6/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
關于Shell腳本中怎么自動安裝zookeeper就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。