A: This script runs the machine, the Linux RHEL6
B,c,d,... : To be installed zookeeper cluster machine, Linux RHEL6
First, on the machine A that runs the script, determine if SSH can be logged in without a password to the machine b,c,d to install ZK,... , and then you can run this script on a:
Copy Code code as follows:
Premise:
B, C, D machine must be configured well repo, this script uses Cdh5 repo, the following content is saved to:/etc/yum.repos.d/cloudera-cdh5.repo:
Copy Code code as follows:
[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
The automatic installation script will automatically install the zookeeper on the B,c,d machine and configure the related configuration files. But they are not started. Supports 1,3,5,7 servers.
Copy Code code as follows:
#!/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= "$"
Array= ($)
ifs= "$OLD _ifs"
}
#***********************************************************
# Install_zookeeper
# Install Zookeeper on 1, 3 or 5 servers
#
# Parameters:
# clientport-the port at which the clients'll 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 argument
Split_to_array $ ":"
# The port at which the clients'll 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[@]}
Todo
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]
Todo
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[*]}
Todo
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"
Modify the last line based on configuration:
Copy Code code as follows:
Install_zookeeper "2181" "192.168.122.201" "192.168.122.202" "192.168.122.203"
Note: You need to stop the firewall on each of the Zk-cluster machines and start zookeeper:
Copy Code code as follows:
$/usr/lib/zookeeper/bin/zkserver.sh Start-foreground