Photo by Samson Creative. on Unsplash

This post explains about deploying a 3-node Cassandra cluster in your laptop by simply using a ‘vagrant up’ command.

Usually, developers struggle in setting up tools and project environment in their laptop especially in Windows. If the purpose is just to check the connectivity with their program and a third party tool, its time consuming to understand the configurations of the third party tool and manually do the setup. It will be a more difficult task if its a database cluster setup. Therefore, the following automation can be a good friend in such cases.

I prepared an Ansible role to automate Cassandra cluster deployment. Details of vagrant setup and Ansible role will be explained below.

Required tools set:

  • Virtualbox
  • Vagrant

Execution steps:

Output:

Ansible playbook will execute a set of tasks and eventually a Cassandra cluster status will be displayed.

The final result of ‘vagrant up’ command

Vagrant

In vagrant, Ansible local provisioner is used for this automated deployment. This helps to avoid initial difficulties in Ansible installation and setup, especially, you do not need to worry about SSH key exchanges.

This vagrant file will deploy 3 nodes and 1 controller which will be used as Ansible controller node. Ansible will be installed automatically and execute ‘cassandraCluster’ playbook from the controller node when you issue ‘ vagrant up’ command.

Hardware resources for each vagrant nodes can be customized as per your hardware availability. ‘natdnshostresolver1’ and ‘natdnsproxy1’ will be enabled on all nodes to give a boost on networking performance.

This inventory file plays a key role in this setup. This will be the global configuration for host IPs, private keys and cassandra seed nodes. Vagrant and Ansible roles will fetch the IP address from this inventory yml file.

Ansible role

This is a simple Ansible role to deploy 3 node cassandra cluster. Following shows the tasks used in the role:

Most of the important parameters in Cassandra.yaml are configured using Ansible template. Following shows the variables used in this role:

It was an interesting challenge to configure a list of Cassandra seed IPs. I have mentioned node 2 and node 3 as seed nodes in inventory file. Those two IPs will be passed to cassandra.yaml file from ‘cassandra_seeds_resolved’ variable.

Best practice tips:

  • Make use of variable as much as possible and avoid hard coding in Ansible tasks.
  • Prepare Ansible roles with the concept of idempotency. This is to ensure that your environment is consistent. Also, this will help in reusing the same environment to test the changes in your Ansible roles.
  • I used an Ansible task to check if java already installed. These sort of verification can help if we want to run this playbook in some other environment.
  • Display cluster status at last of all tasks, so you can easily identify whether playbook execution successfully created the cluster.

Connectivity Testing

In addition, I prepared a simple java program to check the connectivity. You just need to parse one of the node IP and port (mentioned in inventory file), then the driver can fetch the other node’s IP and display the cluster details.

Connectivity test

Destroying the setup

Just like how we deployed this setup, destroying it is also super easy. Anytime, you can destroy this whole setup by simply using the ‘vagrant destroy’ command.

Conclusion

I hope this post helps to give an idea about how deployment in local environment can be automated using tools like Vagrant and Ansible.

Please note that this can be faster and more efficient if you use cloud environment instead of Vagrant.( I will write that in another post — “Deployment using Terraform and Ansible”).

References

  1. Full source code available in https://github.com/apkan/vagrant-cassandra-ansible.git
  2. https://www.vagrantup.com/docs/provisioning/ansible_local.html
  3. https://galaxy.ansible.com/andrewrothstein/cassandra-cluster
  4. https://docs.datastax.com/en/developer/java-driver/3.6/manual/

Follow us on Twitter 🐦 and Facebook 👥 and join our Facebook Group 💬.

To join our community Slack 🗣️ and read our weekly Faun topics 🗞️, click here⬇