Friday, June 26, 2015

Installation Script for Apache Zookeeper-3.3.5 on Linux

One of my previous blogs describes how to setup a Zookeeper cluster manually. Here's a quick fix: an installation script for the same. You need to run the following script (after storing the content in a .sh file) on your machine and you can install a zookeeper cluster on a set of remote machines. All you need as prerequisite on those machines is the zookeeper-3.3.5 setup at a common location. You can also use this script for other versions, with a bit of modification to the script(replacing the version used in the script to yours. It should have not been hard coded, I know.. My bad).



#!/bin/bash

zkServerEntryPart1="server.";
zkServerEntryPart2="=zoo";
zkServerEntryPart3=":2888:3888NEW_LINE";
zkServerEntry="";

# Local FS Setup location
echo "Enter the path of the folder in which the zookeeper setup is stored. For example '/home/abc/setups'"
read -e setupsLocation
# Directory Name
echo "Enter the directory path where zookeeper is to be installed : "
read -e zookeeperSetupLocation

# Read the number of zookeeper servers
echo "Enter the number of machines in the zookeeper cluster : ";
read -e n;

# Read the zookeeper server details
for ((  i = 1 ;  i <= n;  i++  ))
do
# obtain the zookeeper server ips of all machines in the cluster
echo "Enter the IP of zookeeper machine $i:"
read -e zookeeperServer;
zookeeperServerIPList[i]=$zookeeperServer
temp=$zkServerEntryPart1""$i""$zkServerEntryPart2""$i""$zkServerEntryPart3;
zkServerEntry=$zkServerEntry""$temp;

# obtain the usernames for all the zookeeper servers
echo "Enter the username of machine ${zookeeperServerIPList[i]}"
read -e username
userNameList[i]=$username
done

# Copy the setup to all the storm machines
for ((  i = 1 ;  i <= n;  i++  ))
do
echo "Enter the data directory location of zookeeper for the machine ${zookeeperServerIPList[i]}"
read -e dataDir
     
# create the required folders on the machines
ssh -t ${userNameList[i]}@${zookeeperServerIPList[i]} "if [ ! -d $zookeeperSetupLocation ]; then
sudo mkdir $zookeeperSetupLocation;
sudo chown -R ${userNameList[i]}: $zookeeperSetupLocation;
fi
if [ ! -d $dataDir ]; then
sudo mkdir $dataDir;
sudo chown -R ${userNameList[i]}: $dataDir;
fi"

# copy the zookeeper setup at the specified location on the machines
scp -r -q $setupsLocation/zookeeper-3.3.5 ${userNameList[i]}@${zookeeperServerIPList[i]}:$zookeeperSetupLocation/zookeeper-3.3.5

# create and configure the 'zoo.cfg' and 'myid' files
ssh -t ${userNameList[i]}@${zookeeperServerIPList[i]} "touch $zookeeperSetupLocation/zookeeper-3.3.5/conf/zoo.cfg;
echo -e "dataDir=$dataDir" >> $zookeeperSetupLocation/zookeeper-3.3.5/conf/zoo.cfg;
echo -e "syncLimit=2" >> $zookeeperSetupLocation/zookeeper-3.3.5/conf/zoo.cfg;
echo -e "initLimit=5" >> $zookeeperSetupLocation/zookeeper-3.3.5/conf/zoo.cfg;
echo -e "clientPort=2181" >> $zookeeperSetupLocation/zookeeper-3.3.5/conf/zoo.cfg;
echo  "$zkServerEntry" >> $zookeeperSetupLocation/zookeeper-3.3.5/conf/zoo.cfg;
sed -i 's/NEW_LINE/\n/g' $zookeeperSetupLocation/zookeeper-3.3.5/conf/zoo.cfg
touch $dataDir/myid;                           
echo -e $i >> $dataDir/myid;"

# update the /etc/hosts file
hostFileEntry=${zookeeperServerIPList[i]}" zoo"$i;
for ((  j = 1 ;  j <= n;  j++  ))
do
ssh -t ${userNameList[j]}@${zookeeperServerIPList[j]} "sudo cp /etc/hosts /etc/hosts.bak;
sudo cp /etc/hosts /etc/hosts1;
sudo chmod 777 /etc/hosts1;
sudo echo -e "$hostFileEntry" >> /etc/hosts1;
sudo mv /etc/hosts1 /etc/hosts;"
done
done

Hope it helped. My next blog post is about a small script to start-up the installed zookeeper cluster.

No comments:

Post a Comment