How Do You Install Zookeeper on Any Type of Linux?

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.  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.4.13 #change this as needed

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-9-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 -L http://supergsego.com/apache/zookeeper/zookeeper-$version/zookeeper-$version.tar.gz > /tmp/zookeeper-$version.tar.gz

cp /tmp/zookeeper-$version.tar.gz /bin/zookeeper-$version.tar.gz

cd /bin

tar -zxvf zookeeper-$version.tar.gz

sudo mkdir -p /usr/lib/jvm/java-9-openjdk-amd64/conf/management/

sudo touch /usr/lib/jvm/java-9-openjdk-amd64/conf/management/management.properties

echo 'tickTime=2000
dataDir=/var/zookeeper
clientPort=2181' > /bin/zookeeper-$version/conf/zoo.cfg

echo "To find the version that is installed, and confirm installation worked, run these two commands."
echo "cd /bin/zookeeper-"$version"/bin"
echo "sudo 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."

Leave a comment

Your email address will not be published. Required fields are marked *