How to setup a basic ZooKeeper ensemble

This article is a guide to setup a ZooKeeper ensemble. I use this to have a local environment for development and testing.

For Zookeeper to work, you really only need to configure a couple of things. The first is the zoo.cfg file, and the second is a myid file in the dataDir. See this link for more info.

Prerequisites

It assumes you are using the following software versions.

  • MacOS 10.11.3
  • Vagrant 1.8.5
  • Java 1.8.0
  • Zookeeper 3.4.8

Here are the steps

  1. First, create a workspace.

    mkdir -p ~/vagrant_boxes/zookeeper

    cd ~/vagrant_boxes/zookeeper

  2. Next, create a new vagrant box. I’m using a minimal CentOS vagrant box.

    vagrant box add “CentOS 6.5 x86_64” https://github.com/2creatives/vagrant-centos/releases/download/v6.5.3/centos65-x86_64-20140116.box

  3. We are going to create a vagrant box with the packages we need. So, first we initialize the vagrant box.

    vagrant init -m “CentOS 6.5 x86_64” zoo_base

  4. Next, change the Vagrantfile to the following:

      Vagrant.configure(2) do |config|
        config.vm.box = "CentOS 6.5 x86_64"
        config.vm.box_url = "zoo_base"
        config.ssh.insert_key = false
      end
    

  5. Now, install Zookeeper and it’s dependencies.

    vagrant up

    vagrant ssh

    sudo yum install java-1.8.0-openjdk-devel

    sudo yum install wget

    wget http://apache-mirror.rbc.ru/pub/apache/zookeeper/zookeeper-3.4.8/zookeeper-3.4.8.tar.gz ~

    gunzip -c *gz | tar xvf –

  6. Open up your ~/.bash_profile and append the following lines.

      export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk.x86_64
      export PATH=$PATH:$JAVA_HOME/bin
      export ZOOKEEPER_HOME=~/zookeeper-3.4.8
      export PATH=$PATH:$ZOOKEEPER_HOME/bin
      export ZOOKEEPER_CONF_DIR=$ZOOKEEPER_HOME/conf
    

  7. Source the profile.

    source ~/.bash_profile

  8. Create a ~/zookeeper-3.4.8/conf/zoo.cfg file with the following contents.

      tickTime=2000
      dataDir=/tmp/zookeeper/
      clientPort=2181
      initLimit=5
      syncLimit=2
      server.1=192.168.50.11:2888:3888
      server.2=192.168.50.12:2888:3888
      server.3=192.168.50.13:2888:3888
      server.4=192.168.50.14:2888:3888
      server.5=192.168.50.15:2888:3888
    

  9. Exit the SSH session and copy the VM for the other zookeeper nodes.

    exit

    vagrant halt

    vagrant package

    vagrant box add zookeeper ~/vagrant_boxes/zookeeper/package.box

  10. Edit the Vagrantfile to look like the following below. This will create 5 zookeeper nodes for us using the new Zookeeper VM.

      Vagrant.configure("2") do |config|
        (1..5).each do |i|
          config.vm.define "zoo#{i}" do |node|
            node.vm.box = "zookeeper"
            node.vm.box_url = "zoo#{i}"
            node.vm.hostname = "zoo#{i}"
            node.vm.network :private_network, ip: "192.168.50.1#{i}"
    
            # Zookeeper needs an ID file for each node
            node.vm.provision "shell", inline: "mkdir -p /tmp/zookeeper; echo '#{i}' >> /tmp/zookeeper/myid", privileged: false
    
            # Start Zookeeper
            node.vm.provision "shell", inline: "~/zookeeper-3.4.8/bin/zkServer.sh start", privileged: false
    
            node.ssh.insert_key = false
          end
        end
      end
    

  11. Bring the new Vagrant VMs up.

    vagrant up –no-provision

    vagrant provision

Running ZooKeeper

To test to see if the Zookeeper works, you can do the following.

  1. SSH into zoo1.

    vagrant ssh zoo1

  2. Start Zookeeper CLI.

    ~/zookeeper-3.4.8/bin/zkCli.sh -server 192.168.50.11:2181

  3. Create a new znode and associates the string “my_data” with the node.

    create /zk_test my_data

  4. Now exit the CLI and SSH session and log into zoo4.

    quit

    exit

    vagrant ssh zoo4

  5. Connect to the Zookeeper CLI again (notice the IP changed).

    ~/zookeeper-3.4.8/bin/zkCli.sh -server 192.168.50.14:2181

  6. You should be able to see the /zk_test znode with an ls command (it should look like so: “[zookeeper, zk_test]”)

    ls /
    [zookeeper, zk_test]

Leave a Reply