You are on page 1of 3

Tony Zampogna

My Personal Blog

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:

1 Vagrant.configure(2) do |config|
2   config.vm.box = "CentOS 6.5 x86_64"
3   config.vm.box_url = "zoo_base"
4   config.ssh.insert_key = false
5 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.

1 export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk.x86_64
2 export PATH=$PATH:$JAVA_HOME/bin
3 export ZOOKEEPER_HOME=~/zookeeper-3.4.8
4 export PATH=$PATH:$ZOOKEEPER_HOME/bin
5 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.

1 tickTime=2000
2 dataDir=/tmp/zookeeper/
3 clientPort=2181
4 initLimit=5
5 syncLimit=2
6 server.1=192.168.50.11:2888:3888
7 server.2=192.168.50.12:2888:3888
8 server.3=192.168.50.13:2888:3888
9 server.4=192.168.50.14:2888:3888
10 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.

1 Vagrant.configure("2") do |config|
2   (1..5).each do |i|
3     config.vm.define "zoo#{i}" do |node|
4       node.vm.box = "zookeeper"
5       node.vm.box_url = "zoo#{i}"
6       node.vm.hostname = "zoo#{i}"
7       node.vm.network :private_network, ip: "192.168.50.1#{i}"
8  
9       # Zookeeper needs an ID file for each node
10       node.vm.provision "shell", inline: "mkdir -p /tmp/zookeeper; echo '#
11  
12       # Start Zookeeper
13       node.vm.provision "shell", inline: "~/zookeeper-3.4.8/bin/zkServer.s
14  
15       node.ssh.insert_key = false
16     end
17   end
18 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]

Share this:

 Share

Tony Zampogna / November 1, 2016 / Devops, Programming, Technology / Vagrant,


Zookeeper

You might also like