Tutorial on ZooKeeper – Part 4: Use Ansible to Setup ZooKeeper Servers

From the last tutorial, we know how to install and configure a ZooKeeper cluster starting from scratch.

In order to simplify the deployment, I’ve written a ansible-zookeeper role, which is originally forked from AnsibleShipyard and modified to support both standalone mode and replicated mode (also known as cluster), to deploy ZooKeeper servers.

Note: This role requires Java support. You can install manually or use role ansible-java as a dependency. This role assumes that all your to-be-deployed servers have been already installed these required softwares.

Clone the Role

Before the deployment, you have to first clone the role to your ansible playbooks folder (just for better management), of course you can specify to other folders.

1
2
$ cd /your/ansible/playbooks/
$ git clone https://github.com/dixudx/ansible-zookeeper.git

Prepare your own inventory

Just as last tutorial said, you have to be clear enough about your ZooKeeper servers. Touch your inventory file to store these servers. In order to differentiate these two modes for better demonstration, I create two sections, one is zookeeper_servers for standalone mode, the other is cluster_hosts for replicated mode. You can also give them other names. Take your choice.

1
$ vim myinventory

Start the Deployment

The default mode of this role is standalone, which means you can omit below parts in your yaml file.

1
vars:
  install_mode: "standalone"

Standalone Mode

Once you have your own inventory file, you can create a blow playbook named zookeeper_standalone.yml and insert below contents.

1
- hosts: zookeeper_servers
  vars:
    install_mode: "standalone"
  sudo: yes

  roles:
    - {role: ansible-zookeeper}

If the servers have not been installed Java, you can append below part to the above playbook.

1
dependencies:
  - { role: ansible-java, java_version: 7 }

Now you can start deploying standalone ZooKeeper servers.

1
$ ansible-playbook -i myinventory zookeeper_standalone.yml

Replicated Mode

Just as standalone mode, create a playbook named zookeeper_cluster.yml and insert below contents.

1
- hosts: cluster_hosts
  vars:
    install_mode: "cluster"
  sudo: yes

  roles:
    - {role: ansible-zookeeper}

Also you can add ansible-java as a dependency.

Run below command to deploy ZooKeeper cluster.

1
$ ansible-playbook - i myinventory zookeeper_cluster.yml