Setting up and configuring a Cassandra cluster (using tools such as Ansible)

Introduction

Setting up and configuring a cluster is a very important step for large-scale data storage systems. Ansible is a tool used for automating management tasks, which can help us quickly establish and maintain distributed computer clusters, including Cassandra clusters.

Ansible Basic Environment Setup

First, we need to set up the basic environment for Ansible. This process involves the following steps:

Installing and Configuring Ansible

  • Ubuntu/Debian: sudo apt-get install ansible
  • CentOS/RHEL: sudo yum install ansible
  • MacOS: brew install ansible

Configuring Ansible's hosts File

We need to define our machines in the hosts file. We can use Ansible's pre-created hosts file templates to quickly establish them.

# hosts.yaml
[anomaly:192.168.1.100]
[anomaly:192.168.1.101]

# Other nodes can be added as needed

Creating Ansible Tasks

Next, we need to define an Ansible task to manage our Cassandra cluster.

# cassandra.yml

- name: Cassandra Cluster Setup
  hosts: anomaly
  become: yes

  tasks:
    - name: Cassandra Node Creation
      ansible.builtin.user:
        name: cassandra
        group: cassandra
        home: /usr/local/cassandra

    - name: Cassandra Data Directory
      file:
        path: /data
        state: directory
        mode: '0755'

    - name: Cassandra Startup Script
      copy:
        content: |
          #!/bin/bash
          cd /usr/local/cassandra && ./bin/start.sh
        dest: /etc/init.d/cassandra

    - name: Firewalld Allow Cassandra Port
      firewalld:
        port: 9042
        proto: tcp

Cassandra Cluster Setup and Configuration

Cassandra Installation and Update

We need to download the Cassandra binary package and install it.

# Install dependencies
sudo yum install java-8-openjdk-headless -y

# Download cassandra binary from https://www.apache.org/download.cgi?id= cassandra
wget https://www.apache.org/dist/cassandra/7.0.1/apache-cassandra-7.0.1-bin-airflow.tar.gz

# Install binary package
sudo tar -xvf apache-cassandra-7.0.1-bin-airflow.tar.gz -C /usr/local/

# Save path
CASSANDRA_DATA_DIR=/data/

# Run start.sh to start the service
sudo cd /usr/local/cassandra/bin/
./start.sh

Cassandra Cluster Setup

We need to create a Cassandra cluster and configure the relevant Cassandra files.

# Ensure cassandra service is running
sudo systemctl start cassandra

# Enter cassandra shell
cqlsh -u cassandra -r

# Create a new cluster
CREATE KEYSPACE mykeyspace WITH REPLICATION = {'class' : 'SimpleStrategy', 'replication_factor' : 3};

Cassandra Cluster Configuration

We need to write the relevant Cassandra cluster configurations into the configuration file.

# cassandra-config.yml

- name: Cassandra Configuration
  hosts: anomaly
  become: yes

  tasks:
    - name: Cassandra Configuration File
      copy:
        content: |
          data_dir = /data
          listen_address = localhost
          port = 9042
        dest: /etc/cassandra/cassandra.yaml

Summary and Suggestions

In this document, we discussed the basic steps for setting up and configuring a Cassandra cluster using Ansible. We first introduced the basic environment setup for Ansible, including installing and configuring Ansible, setting up the hosts file, and creating Ansible tasks.

Then, we demonstrated in detail the process of establishing a Cassandra cluster, including installing the binary package, running start.sh to start the service, creating a new cluster, and writing relevant Cassandra configurations.

Finally, we provide a sample cassandra-config.yml file that can help you quickly set up and configure a Cassandra cluster in practice.