Updated on 5/14/20 with a minor update on 11/13/21
Problem scenario
You want to install Apache Zookeeper with a script. You want the same script to work regardless of what distribution of Linux you are using (e.g., Ubuntu/Debian, RedHat (including CentOS/Fedora), or SUSE). How do you write a script to install the latest version of Zookeeper?
Solution
The file that is attached called zki.txt is not needed but is attached as a reference copy of the below. Create a file in /tmp/ and call it zookeeper.sh. Below is the content of /tmp/zookeeper.sh:
#!/bin/bash
# Written by www.continualintegration.com
version=3.7.0 #change this as needed
debianjavaversion=11
distro=$(cat /etc/*-release | grep NAME)
debflag=$(echo $distro | grep -i "ubuntu")
if [ -z "$debflag" ]
then # If it is not Ubuntu, test if it is Debian.
debflag=$(echo $distro | grep -i "debian")
echo "determining Linux distribution..."
else
echo "You have Ubuntu Linux!"
fi
rhflag=$(echo $distro | grep -i "red*hat")
if [ -z "$rhflag" ]
then #If it is not RedHat, see if it is CentOS or Fedora.
rhflag=$(echo $distro | grep -i "centos")
if [ -z "$rhflag" ]
then #If it is neither RedHat nor CentOS, see if it is Fedora.
echo "It does not appear to be CentOS or RHEL..."
rhflag=$(echo $distro | grep -i "fedora")
fi
fi
if [ -z "$rhflag" ]
then
echo "...still determining Linux distribution..."
else
echo "You have a RedHat distribution (e.g., CentOS, RHEL, or Fedora)"
yum -y install java-1.8.0-openjdk* nc # install nc for initial testing only.
JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk
echo 'export JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk' >> ~/.bashrc
source ~/.bashrc
source /etc/environment
fi
if [ -z "$debflag" ]
then
echo "...still determining Linux distribution..."
else
echo "You are using either Ubuntu Linux or Debian Linux."
apt-get -y update # This is necessary on new AWS Ubuntu servers.
apt -y install openjdk-$debianjavaversion-jre-headless unzip
fi
suseflag=$(echo $distro | grep -i "suse")
if [ -z "$suseflag" ]
then
if [ -z "$debflag" ]
then
if [ -z "$rhflag" ]
then
echo "*******************************************"
echo "Could not determine the Linux distribution!"
echo "Installation aborted. Nothing was done."
echo "******************************************"
exit
fi
fi
else
zypper -n install java-1_8_0-openjdk
fi
curl https://dlcdn.apache.org/zookeeper/zookeeper-$version/apache-zookeeper-$version-bin.tar.gz > /tmp/apache-zookeeper-$version-bin.tar.gz
cp /tmp/apache-zookeeper-$version-bin.tar.gz /bin/apache-zookeeper-$version-bin.tar.gz
cd /bin
tar -zxvf apache-zookeeper-$version-bin.tar.gz
sudo mkdir -p /usr/lib/jvm/java-$debianjavaversion-openjdk-amd64/conf/management/
sudo touch /usr/lib/jvm/java-$debianjavaversion-openjdk-amd64/conf/management/management.properties
echo 'tickTime=2000
dataDir=/var/zookeeper
clientPort=2181' > /bin/apache-zookeeper-$version-bin/conf/zoo.cfg
echo "To find the version that is installed, and confirm installation worked, run these two commands."
echo "cd /bin/apache-zookeeper-"$version"-bin/bin"
echo "sudo /bin/bash zkServer.sh start-foreground"
echo "Then start a second terminal session to the server"
echo "From the second terminal session, run this echo command:"
echo "echo status | nc 127.0.0.1 2181"
echo "Then use Ctrl-c on the first terminal to exit out and move on."