Installing Cassandra
Setup
Package | Version | Install Location |
---|---|---|
Apache Cassandra | 4.0.0 | /opt/apache-cassandra-4.0.0/ |
Java | jdk1.8.0_241 | /opt/jdk1.8.0_241 |
Python | python-2.7.5-76.el7.x86_64 | /usr/bin/python |
Prerequisites
- Access as root user or user having root privileges
- Latest version of Java 8
- Latest version of Python 2.7 or Python 3.6+
- Create a new user "cassandra". This user will be used to run cassandra service.
Download Java 8 latest version from Oracle official website and install on server
# /opt/jdk1.8.0_241/bin/java -version
java version "1.8.0_241"
Java(TM) SE Runtime Environment (build 1.8.0_241-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.241-b07, mixed mode)
Python 2.7 is available in the Linux OS repository can be be installed using YUM.
# /usr/bin/python --version
Python 2.7.5
# useradd cassandra
# id cassandra uid=1003(cassandra) gid=1003(cassandra) groups=1003(cassandra)
# id cassandra uid=1003(cassandra) gid=1003(cassandra) groups=1003(cassandra)
Install Cassandra
- Login as root user or with user having root privileges
- Download Cassandra binary tarball from the official website
- Extract Cassandra binary tarball in /opt
- Change ownership of cassandra install directory to cassandra user
- Check the extracted contents in /opt
- Create cassandra log directory
- Open a new SSH terminal and login as cassandra user
- Update cassandra user profile to include PATH for java & cassandra binaries
- Bring the profile changes in effect
- Repeat the same step for root user and update the profile to include cassandra and java binary install path
# curl -OL http://apache.mirror.digitalpacific.com.au/cassandra/4.0.0/apache-cassandra-4.0.0-bin.tar.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 44.3M 100 44.3M 0 0 3404k 0 0:00:13 0:00:13 --:--:-- 4290k
# tar xfz apache-cassandra-4.0.0-bin.tar.gz -C /opt/
# chown -R cassandra:cassandra /opt/apache-cassandra-4.0.0/
# ls -l /opt/apache-cassandra-4.0.0/
drwxr-xr-x 2 root root 230 Aug 21 12:23 bin
-rw-r--r-- 1 cassandra cassandra 4832 Jul 22 17:02 CASSANDRA-14092.txt
-rw-r--r-- 1 cassandra cassandra 431737 Jul 22 17:02 CHANGES.txt
drwxr-xr-x 3 cassandra cassandra 4096 Aug 21 12:23 conf
drwxr-xr-x 3 cassandra cassandra 33 Aug 21 12:23 doc
drwxr-xr-x 3 cassandra cassandra 4096 Aug 21 12:23 lib
-rw-r--r-- 1 cassandra cassandra 12960 Jul 22 17:02 LICENSE.txt
-rw-r--r-- 1 cassandra cassandra 135759 Jul 22 17:02 NEWS.txt
-rw-r--r-- 1 cassandra cassandra 349 Jul 22 17:02 NOTICE.txt
drwxr-xr-x 3 cassandra cassandra 230 Aug 21 12:23 pylib
drwxr-xr-x 4 cassandra cassandra 169 Aug 21 12:23 tools
# mkdir /var/log/cassandra
# vi .bash_profile
PATH=$PATH:$HOME/.local/bin:$HOME/bin:/opt/jdk1.8.0_241/bin:/opt/apache-cassandra-4.0.0/bin
# source .bash_profile
Setup Cassandra as Systemd service
- Open a new SSH terminal and login as root user or user having root privileges
- Create cassandra service systemd file
- Make the systemd file executable
- For RHEL7 add the service under systemctl
Modify the below systemd config file and update the highlighted options with correct binary path for Java and Cassandra. In our case:
Cassandra is installed under /opt/apache-cassandra-4.0.0 and
Java is installed under /opt/jdk1.8.0_241
# vi /etc/init.d/cassandra
#!/bin/bash
#
# /etc/init.d/cassandra
#
# Startup script for Cassandra
#
# chkconfig: 2345 80 20
# description: Starts and stops Cassandra
# pidfile: /var/run/cassandra/cassandra.pid
### BEGIN INIT INFO
# Provides: cassandra
# Required-Start: $remote_fs $network $named $time
# Required-Stop: $remote_fs $network $named $time
# Should-Start: ntp mdadm
# Should-Stop: ntp mdadm
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: distributed storage system for structured data
# Description: Cassandra is a distributed (peer-to-peer) system for
# the management and storage of structured data.
### END INIT INFO
. /etc/rc.d/init.d/functions
export CASSANDRA_HOME=/opt/apache-cassandra-4.0.0
export CASSANDRA_CONF=/opt/apache-cassandra-4.0.0/conf
export CASSANDRA_INCLUDE=$CASSANDRA_HOME/bin/cassandra.in.sh
export CASSANDRA_OWNR=cassandra
NAME="cassandra"
log_file=/var/log/cassandra/cassandra.log
pid_file=/var/run/cassandra/cassandra.pid
lock_file=/var/lock/subsys/$NAME
CASSANDRA_PROG=/opt/apache-cassandra-4.0.0/bin/cassandra
# The first existing directory is used for JAVA_HOME if needed.
JVM_SEARCH_DIRS="/opt/jdk1.8.0_241 /usr/lib/jvm/jre /usr/lib/jvm/jre-1.8.* /usr/lib/jvm/java-1.8.*/jre"
# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
# If JAVA_HOME has not been set, try to determine it.
if [ -z "$JAVA_HOME" ]; then
# If java is in PATH, use a JAVA_HOME that corresponds to that. This is
# both consistent with how the upstream startup script works, and with
# the use of alternatives to set a system JVM (as is done on Debian and
# Red Hat derivatives).
java="`/usr/bin/which java 2>/dev/null`"
if [ -n "$java" ]; then
java=`readlink --canonicalize "$java"`
JAVA_HOME=`dirname "\`dirname \$java\`"`
else
# No JAVA_HOME set and no java found in PATH; search for a JVM.
for jdir in $JVM_SEARCH_DIRS; do
if [ -x "$jdir/bin/java" ]; then
JAVA_HOME="$jdir"
break
fi
done
# if JAVA_HOME is still empty here, punt.
fi
fi
JAVA="$JAVA_HOME/bin/java"
export JAVA_HOME JAVA
case "$1" in
start)
# Cassandra startup
echo -n "Starting Cassandra: "
[ -d `dirname "$pid_file"` ] || \
install -m 755 -o $CASSANDRA_OWNR -g $CASSANDRA_OWNR -d `dirname $pid_file`
runuser -u $CASSANDRA_OWNR -- $CASSANDRA_PROG -p $pid_file > $log_file 2>&1
retval=$?
chown root.root $pid_file
[ $retval -eq 0 ] && touch $lock_file
echo "OK"
;;
stop)
# Cassandra shutdown
echo -n "Shutdown Cassandra: "
runuser -u $CASSANDRA_OWNR -- kill `cat $pid_file`
retval=$?
[ $retval -eq 0 ] && rm -f $lock_file
for t in `seq 40`; do
status -p $pid_file cassandra > /dev/null 2>&1
retval=$?
if [ $retval -eq 3 ]; then
echo "OK"
exit 0
else
sleep 0.5
fi;
done
# Adding a sleep here to give jmx time to wind down (CASSANDRA-4483). Not ideal...
# Adam Holmberg suggests this, but that would break if the jmx port is changed
# for t in `seq 40`; do netstat -tnlp | grep "0.0.0.0:7199" > /dev/null 2>&1 && sleep 0.1 || break; done
sleep 5
status -p $pid_file cassandra > /dev/null 2>&1
retval=$?
if [ $retval -eq 3 ]; then
echo "OK"
else
echo "ERROR: could not stop $NAME"
exit 1
fi
;;
reload|restart)
$0 stop
$0 start
;;
status)
status -p $pid_file cassandra
exit $?
;;
*)
echo "Usage: `basename $0` start|stop|status|restart|reload"
exit 1
esac
exit 0
# chmod 755 /etc/init.d/cassandra
# systemctl daemon-reload
Start Cassandra Service
- Start cassandra service using systemd
- Check Status of Cassandra Service
- Run systemd status command to check status of cassandra service
- Check status using nodetool command
- Cassandra install is complete. This is only a basic install.
# systemctl start cassandra
# /systemctl status cassandra
cassandra.service - LSB: distributed storage system for structured data
Loaded: loaded (/etc/rc.d/init.d/cassandra; bad; vendor preset: disabled)
Active: active (running) since Sun 2021-08-22 19:48:19 EDT; 26s ago
Docs: man:systemd-sysv-generator(8)
Process: 7040 ExecStop=/etc/rc.d/init.d/cassandra stop (code=exited, status=0/SUCCESS)
Process: 7127 ExecStart=/etc/rc.d/init.d/cassandra start (code=exited, status=0/SUCCESS)
Main PID: 7212 (java)
CGroup: /system.slice/cassandra.service
7212 /opt/jdk1.8.0_241/bin/java -ea -da:net.openhft... -XX:+UseThreadPriorities -XX:+HeapDumpOnOutOfMemoryError -Xss256k...
Aug 22 19:48:18 cassandra.linuxtechspace.com systemd[1]: Starting LSB: distributed storage system for structured data...
Aug 22 19:48:19 cassandra.linuxtechspace.com runuser[7135]: pam_unix(runuser:session): session opened for user cassandra by (uid=0)
Aug 22 19:48:19 cassandra.linuxtechspace.com cassandra[7127]: Starting Cassandra: OK
Aug 22 19:48:19 cassandra.linuxtechspace.com systemd[1]: Started LSB: distributed storage system for structured data.
# /opt/apache-cassandra-4.0.0/bin/nodetool status
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
-- Address Load Tokens Owns (effective) Host ID Rack
UN 127.0.0.1 133.03 KiB 16 100.0% 4c2e6eea-65b6-4baa-bf5f-7df7368a01fa rack1