This script creates a virtual 3-node Apache ZooKeeper cluster on your local machine using Vagrant, VirtualBox and Ansible.
The Zookeeper service will run in a truly replicated mode over several machines, so you can experiment with host failures, client connections, misc cluster configurations, etc.
Also, if you want to create a virtual cluster for other services built on top of Zookeeper (e.g. Apache Kafka, HBase, Solr, Neo4j), this script might help as a starting point.
- System: 3x Ubuntu 14.04 server
- Memory: 256 MB each host
- IP address: 192.168.5.100-102
- Hostnames:
node-[x]
withx
have values 1, 2 or 3 - Zookeeper version: 3.4.8
- JVM: Oracle Java-7
You can easily customize the cluster parameters in the following files:
Vagrantfile
contains parameters for the system, memory, IP addresses and hostnamesprovision.yml
contains parameters for Zookeeper version, installation directory, mirror and the JVM version.
Note regarding the cluster size: It is recommended to run the cluster on an odd number of hosts (3, 5, etc). Zookeeper is designed to survive failure of minority of hosts. Zookeeper running on 3 or 4 hosts can survive failure of 1 host while Zookeper running on 5 hosts can survive failure of 2 hosts.
Depending on your hardware and network connection, the script execution might take between 10-20 minutes.
For Mac, this can be done with Homebrew:
brew install caskroom/cask/brew-cask
brew cask install virtualbox
brew cask install vagrant
brew install ansible
Make sure you are running Ansible v1.7.2 or higher with ansible --version
.
For other systems, checkout the installation pages of Vagrant, VirtualBox and Ansible.
git clone https://github.com/mkrcah/virtual-zookeeper-cluster.git
cd virtual-zookeeper-cluster
vagrant up
This command will create and boot the VMs (using VirtualBox), provision each node with JVM and Zookeeper and start the Zookeeper service on each node (using Ansible). Note, that this command might take a while since it needs to download the Ubuntu image and the JVM.
Each VM should now have the Zookeeper running on port 2181. Test that the service is running in non-error state by:
echo ruok | nc 192.168.5.100 2181
The server should respond with imok
.
If interested, checkout the list of all our-letter commands supported by the Zookeeper service
The easiest way to experiment with Zookeeper is to log into one of the machines and play with the command-line tool shipped with the Zookeeper:
vagrant ssh node-1
vagrant@node-1:~$ cd /opt/zookeeper-3.4.7/bin/
vagrant@node-1:/opt/zookeeper-3.4.7/bin$ ./zkCli.sh -server localhost:2181
[zk: localhost:2181(CONNECTED) 3] create /zk_test my_data
Created /zk_test
vagrant@node-1:/opt/zookeeper-3.4.7/bin$ ./zkCli.sh -server 192.168.5.101:2181
[zk: 192.168.5.101:2181(CONNECTED) 0] ls /
[zookeeper, zk_test]
[zk: 192.168.5.101:2181(CONNECTED) 1] get /zk_test
my_data
Happy Zookeeping!
In case of any questions, drop me an email or ping me on twitter.