Install Apache Cassandra on AlmaLinux 9 with Best Steps

Posted on

Install Apache Cassandra on AlmaLinux 9 with Best Steps

Install Apache Cassandra on AlmaLinux 9 with Best Steps

In this guide, you will learn how to Install Apache Cassandra on AlmaLinux 9. Apache Cassandra is a distributed database management system that is built to handle large amounts of data across multiple data centers and the cloud. Key features include:

  • High availability: Cassandra is designed to have no single point of failure.
  • Scalability: Cassandra can scale horizontally to handle increasing amounts of data and traffic.
  • Fault tolerance: Cassandra can tolerate failures of individual nodes without losing data.
  • Performance: Cassandra is designed for high-performance reads and writes.

Written in Java, it’s a NoSQL database offering many things that other NoSQL and relational databases cannot. You can now proceed to the guide steps below on the Orcacore website to complete Cassandra Setup on AlmaLinux 9.

Steps To Install and Configure Apache Cassandra on AlmaLinux 9

To complete this guide, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide on Initial Server Setup with AlmaLinux 9.

1. Install Java for Cassandra Setup

Apache Cassandra is written in Java, so you need to have Java installed on your server.

First, update your local package index with the command below:

sudo dnf update -y

Then, use the command below to install OpenJDK on your server:

sudo dnf install java-1.8.0-openjdk-devel -y

Verify your Java installation by checking its version:

java -version
**Output**
openjdk version "1.8.0_352"
OpenJDK Runtime Environment (build 1.8.0_352-b08)
OpenJDK 64-Bit Server VM (build 25.352-b08, mixed mode)

2. Install Python and Pip 3 for Cassandra Setup

At this point, you need to install Python and Pip 3 on your server by using the command below:

sudo yum install python3 python-pip

Then, use the pip to install cqlsh on your server:

sudo pip3 install cqlsh

3. Install Apache Cassandra on AlmaLinux 9

At this point, you need to add the Cassandra repository to your server.

Add Apache Cassandra Repository

You need to create a yum repository file with your favorite text editor, here we use vi:

sudo vi /etc/yum.repos.d/cassandra.repo

Then, add the following content for the latest version to the file:

[cassandra]
name=Apache Cassandra
baseurl=https://redhat.cassandra.apache.org/41x/
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://downloads.apache.org/cassandra/KEYS

When you are done, save and close the file.

Run the system update:

sudo dnf update -y

Note: If you get an error while importing GPG keys, update the crypto policies to LEGACY to ensure compatibility then reboot your system.

# sudo update-crypto-policies --set LEGACY
# sudo reboot

Install Cassandra

At this point, use the following command to install Cassandra on AlmaLinux 9:

sudo dnf install cassandra -y

Create a systemd Unit File for Cassandra

At this point, you need to create a systemd unit file to manage Cassandra on AlmaLinux 9.

Create and open the file with your favorite text editor, here we use vi:

sudo vi /etc/systemd/system/cassandra.service

Add the following content to the file:

[Unit]
Description=Apache Cassandra
After=network.target

[Service]
PIDFile=/var/run/cassandra/cassandra.pid
User=cassandra
Group=cassandra
ExecStart=/usr/sbin/cassandra -f -p /var/run/cassandra/cassandra.pid
Restart=always

[Install]
WantedBy=multi-user.target

When you are done, save and close the file.

Then reload the daemon:

sudo systemctl daemon-reload

Start and enable your Cassandra service:

# sudo systemctl start cassandra
# sudo systemctl enable cassandra

Verify your Apache Cassandra is active and running on AlmaLinux 9:

sudo systemctl status cassandra

In your output you will see:

Install Apache Cassandra on AlmaLinux 9

Also, you can use the command below to check your Cassandra status:

nodetool status
Check Cassandra status

The UN option means that your service is up and normal.

Log in to your Cluster

At this point, you can log in to your cluster with the following command:

cqlsh

Your output should be similar to this:

**Output**
Connected to Test Cluster at 127.0.0.1:9042
[cqlsh 6.1.0 | Cassandra 4.1-rc1 | CQL spec 3.4.6 | Native protocol v5]
Use HELP for help.
cqlsh>

Note: If you get this error ImportError: cannot import name ‘authproviderhandling’ from ‘cqlshlib’, you can fix it by following the steps below:

Find the path where cqlshlib exists:

find /usr/lib/ -name cqlshlib

The path obtained (in my case) is:

/usr/lib/python3.6/site-packages/cqlshlib

Export the path using the below variable name:

export PYTHONPATH=$PYTHONPATH:/usr/lib/python3.6/site-packages/

Then, re-run the cqlsh command.

Here’s that you have Apache Cassandra installed on your AlmaLinux 9, let’s see how to configure it.

4. Configure Apache Cassandra Default Cluster

At this point, you can change your default cluster name. From the cqlsh shell run the command below to change your cluster name. Remember to replace the name with your own.

cqlsh> UPDATE system.local SET cluster_name = '<strong>Orca Cluster</strong>' WHERE KEY = 'local';

Then, exit from your cluster with the following command:

cqlsh> exit

Now you need to edit the Cassandra YAML file. Open the file with your favorite text editor, here we use vi:

sudo vi /etc/cassandra/default.conf/cassandra.yaml

Find the cluster_name directive and change it to your name:

cluster_name: 'Orca Cluster'

When you are done, save and close the file.

To apply the changes, restart Apache Cassandra on AlmaLinux 9 with the following command:

sudo systemctl restart cassandra

Again log in to your cluster and you will see that it changed to the name that you have given to it:

cqlsh
**Output**
Connected to Orca Cluster at 127.0.0.1:9042
[cqlsh 6.1.0 | Cassandra 4.1-rc1 | CQL spec 3.4.6 | Native protocol v5]
Use HELP for help.
cqlsh>

Conclusion

At this point, you have learned to Install and Configure Apache Cassandra on AlmaLinux 9. Apache Cassandra on AlmaLinux 9 is used for managing large-scale, distributed databases with high availability, fault tolerance, and scalability, making it ideal for handling big data applications.

Hope you enjoy it. Please subscribe to us on Facebook, YouTube, and Instagram.

You may also like these articles:

How To Install GitLab on Ubuntu 20.04

How To Install TensorFlow on Ubuntu 22.04

Install PostgreSQL 15 on Rocky Linux 9

Alternative Solutions for Installing Apache Cassandra on AlmaLinux 9

While the provided steps detail a standard installation process, here are two alternative methods for installing Apache Cassandra on AlmaLinux 9 that offer different approaches and benefits:

1. Using Docker

Docker provides a containerized environment, simplifying the installation process and isolating Cassandra from the host system. This approach is particularly useful for development and testing environments.

Explanation:

Docker containers encapsulate an application and its dependencies, ensuring consistency across different environments. By using a pre-built Cassandra Docker image, you avoid the complexities of manual installation and configuration. Docker also makes managing multiple Cassandra instances significantly easier.

Steps:

  1. Install Docker: If Docker isn’t already installed, use the following commands:

    sudo dnf install docker -y
    sudo systemctl start docker
    sudo systemctl enable docker
  2. Pull the Cassandra Image: Obtain the official Cassandra image from Docker Hub.

    sudo docker pull cassandra:latest
  3. Run the Cassandra Container: Start a new container using the pulled image. You can map host ports to the container for external access and configure persistent storage using volumes.

    sudo docker run -d --name cassandra -p 9042:9042 -v cassandra_data:/var/lib/cassandra cassandra:latest
    • -d: Runs the container in detached mode (in the background).
    • --name cassandra: Assigns the name "cassandra" to the container.
    • -p 9042:9042: Maps port 9042 on the host to port 9042 in the container (Cassandra’s default CQL port).
    • -v cassandra_data:/var/lib/cassandra: Creates a named volume "cassandra_data" and mounts it to the container’s Cassandra data directory, ensuring data persistence.
  4. Access Cassandra: Once the container is running, you can access Cassandra using cqlsh from within the container or from the host if you install cqlsh locally.

    • From the host (if cqlsh is installed): cqlsh <host_ip> (replace <host_ip> with the AlmaLinux server’s IP address or localhost if running locally).

    • From within the container: First, get a shell inside the container:

      sudo docker exec -it cassandra bash

      Then, run cqlsh:

      cqlsh

Code Example (Docker Compose – optional):

For more complex configurations (e.g., multi-node clusters), Docker Compose can be used. Create a docker-compose.yml file:

version: "3.8"
services:
  cassandra:
    image: cassandra:latest
    ports:
      - "9042:9042"
    volumes:
      - cassandra_data:/var/lib/cassandra
volumes:
  cassandra_data:

Then, start the container using:

sudo docker-compose up -d

2. Using Kubernetes (for Cluster Deployment)

For production environments requiring scalability, resilience, and automated management, deploying Cassandra on Kubernetes is a robust solution.

Explanation:

Kubernetes is a container orchestration platform that automates the deployment, scaling, and management of containerized applications. Using Kubernetes, you can define Cassandra as a stateful set, ensuring each Cassandra node has a persistent identity and storage. Kubernetes handles node failures, scaling, and rolling updates automatically. This method greatly simplifies the operational aspects of running a Cassandra cluster.

Steps (Simplified):

  1. Install and Configure Kubernetes: This step involves setting up a Kubernetes cluster, which is beyond the scope of this article. You can use tools like Minikube (for local development) or cloud-managed Kubernetes services (e.g., Amazon EKS, Google Kubernetes Engine, Azure Kubernetes Service).

  2. Deploy Cassandra Using Helm (Recommended): Helm is a package manager for Kubernetes. There are pre-built Helm charts for deploying Cassandra.

    • Install Helm: Follow the Helm installation instructions for your platform.

    • Add the DataStax Helm Repository: DataStax provides a well-maintained Cassandra Helm chart.

      helm repo add datastax https://helm.datastax.com/public/
      helm repo update
    • Install Cassandra:

      helm install my-cassandra datastax/cassandra

      Replace my-cassandra with a name for your Cassandra deployment. You can customize the deployment by providing a values.yaml file with configuration options (e.g., number of nodes, storage size).

  3. Access Cassandra: Determine the service name and port exposed by the Helm chart and use cqlsh to connect to the Cassandra cluster. You might need to configure port forwarding or expose the service as a LoadBalancer depending on your Kubernetes environment.

Code Example (Kubernetes StatefulSet – illustrative):

A basic example of a Cassandra StatefulSet definition (this would be part of a more comprehensive Kubernetes configuration):

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: cassandra
spec:
  serviceName: cassandra
  replicas: 3
  selector:
    matchLabels:
      app: cassandra
  template:
    metadata:
      labels:
        app: cassandra
    spec:
      containers:
      - name: cassandra
        image: cassandra:latest
        ports:
        - containerPort: 9042
        volumeMounts:
        - name: cassandra-data
          mountPath: /var/lib/cassandra
  volumeClaimTemplates:
  - metadata:
      name: cassandra-data
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 10Gi

This YAML file defines a StatefulSet named "cassandra" with 3 replicas, using the official Cassandra image. It also defines a volume claim template to provision persistent storage for each node. This is a simplified example, and a real-world Kubernetes deployment would require more detailed configuration.

These alternative methods – using Docker or Kubernetes – offer streamlined and scalable solutions for deploying Apache Cassandra on AlmaLinux 9, depending on your specific needs and environment. Remember to adapt these methods to your specific requirements and consult the official documentation for Docker, Kubernetes, and Cassandra for detailed information and best practices.

Leave a Reply

Your email address will not be published. Required fields are marked *