Skip to main content

Installing the Cassandra / Spark OSS Stack


As mentioned in my portacluster system imaging post, I am performing this install on 1 admin node (node0) and 6 worker nodes (node[1-6]) running 64-bit Arch Linux. Most of what I describe in this post should work on other Linux variants with minor adjustments.


When assembling an analytics stack, there are usually myriad choices to make. For this build, I decided to build the smallest stack possible that lets me run Spark queries on Cassandra data. As configured it is not highly available since the Spark master is standalone. (note: Datastax Enterprise Spark's master has HA based on Cassandra). It's a decent tradeoff for portacluster, since I can run the master on the admin node which doesn't get rebooted/reimaged constantly. I'm also going to skip HDFS or some kind of HDFS replacement for now. Options I plan to look at later are GlusterFS's HDFS adapter and Pithos as an S3 adapter. In the end, the stack is simply Cassandra and Spark with the spark-cassandra-connector.

Responsible Configuration

For this post I've used my perl-ssh-tools suite. The intent is to show what needs to be done and one way to do it. For production deployments, I recommend using your favorite configuration management tool.

perl-ssh-tools uses a configuration similar to dsh, which uses simple files with one host per line. I use two lists below. Most commands run on the fleet of workers. Because provides more than ssh commands, it's also used to run commands on node0 using its --incl flag e.g. --list all --incl node0.

cat .dsh/machines.workers

machines.all is the same with node0 added.

Install Cassandra

My first pass at this section involved setting up a package repo, but since I don't have time to package Spark properly right now, I'm going to use the tarball distros of Cassandra and Spark to keep it simple. joschi maintains a package on the AUR but I have chosen not to use it for this install. I'm also using the Arch packages of OpenJDK, which isn't supported by Datastax, but works fine for hacking. The JDK is pre-installed on my Arch image, it's as simple as sudo pacman -S extra/jdk7-openjdk.

First, I downloaded the Cassandra tarball from to node0 in /srv/public/tgz. Then on the worker nodes, it gets downloaded and expanded in /opt.

sudo curl -o /srv/public/tgz/$pkg \ --list workers -c "curl http://node0/tgz/$pkg |sudo tar -C /opt -xzf -" --list workers -c "sudo ln -s /opt/apache-cassandra-2.0.9 /opt/cassandra"

To make it easier to do upgrades without regenerating the configuration, I relocate the conf dir to /etc/cassandra to match what packages do. This assumes there is no existing /etc/cassandra. --list workers -c "sudo mv /opt/cassandra/conf /etc/cassandra" --list workers -c "sudo ln -s /etc/cassandra /opt/cassandra/conf"

I will start Cassandra with a systemd unit, so I push that out as well. This unit file runs Cassandra out of the tarball as the cassandra user with the stdout/stderr going to the systemd journal (view with journalctl -f). I also included some ulimit settings and bump the OOM score downwards to make it less likely that the kernel will kill Cassandra when out of memory. Since we're going to be running two large JVM apps on each worker node, this unit also enables cgroups so Cassandra can be given priority over Spark. Finally, since the target machines have 16GB of RAM, the heap needs to be set to 8GB ( calculates 3995M which is way too low).

cat > cassandra.service <<EOF
Description=Cassandra Tarball

ExecStart=/opt/cassandra/bin/cassandra -f -p /run/cassandra/

EOF --list workers -x -l cassandra.service -r /etc/systemd/system/ --list workers -c "sudo systemctl daemon-reload"

Since all Cassandra data is being redirected to /srv/cassandra and it's going to run as the cassandra user, those need to be created.

cat > <<EOF
mkdir -p /srv/cassandra/{log,data,commitlogs,saved_caches}
(grep -q '^cassandra:' /etc/group)  || groupadd -g 1234 cassandra
(grep -q '^cassandra:' /etc/passwd) || useradd -u 1234 -c "Apache Cassandra" -g cassandra -s /bin/bash -d /srv/cassandra cassandra
chown -R cassandra:cassandra /srv/cassandra
EOF --list workers -x -s

Configure Cassandra

Before starting Cassandra I want to make a few changes to the standard configurations. I'm not a big fan of LSB so I redirect all of the /var files to /srv/cassandra so they're all in one place. There's only one SSD in the target systems so the commit log goes on the same drive.

I configured portacluster nodes to have a bridge in front of the default interface, making br0 the default interface.

ip=$(ip addr show br0 |perl -ne 'if ($_ =~ /inet (\d+\.\d+\.\d+\.\d+)/) { print $1 }')

perl -i.bak -pe "
  s/^(cluster_name:).*/\$1 'Portable Cluster'/;
  s/^(listen|rpc)_address:.*/\${1}_address: $ip/;
  s/(\s+-\s+seeds:).*/\$1 ',,,,,'/
" /opt/cassandra/conf/cassandra.yaml
# EOF --list workers -x -s

The default log4-server.propterties has log4j printing to stdout. This is not desirable in a background service configuration, so I remove it. The logs are also now written to /srv/cassandra/log.

cat > <<EOF
log4j.appender.R.layout.ConversionPattern=%5p [%t] %d{ISO8601} %F (line %L) %m%n
EOF --list workers -x -l -r /opt/cassandra/conf/

And with that, Cassandra is ready to start. --list workers -c "sudo systemctl start cassandra.service"
ssh node3 tail -f /srv/cassandra/log/system.log

Installing Spark

The process for Spark is quite similar, except that unlike Cassandra, it has a master.

Since I'm not using any Hadoop components, any of the builds should be fine so I used the hadoop2 build.

sudo curl -o /srv/public/tgz/$pkg --list all -c "curl http://node0/tgz/$pkg |sudo tar -C /opt -xzf -" --list all -c "sudo ln -s /opt/spark-1.0.1-bin-hadoop2 /opt/spark" --list all -c "sudo mv /opt/spark/conf /etc/spark" --list all -c "sudo ln -s /etc/spark /opt/spark/conf"

Create /srv/spark and the spark user.

cat > <<EOF
mkdir -p /srv/spark/{logs,work,tmp,pids}
(grep -q '^spark:' /etc/group)  || groupadd -g 4321 spark
(grep -q '^spark:' /etc/passwd) || useradd -u 4321 -c "Apache Spark" -g spark -s /bin/bash -d /srv/spark spark
chown -R spark:spark /srv/spark
# make spark tmp world writable and sticky
chmod 4755 /srv/spark/tmp
EOF --list all -x -s

Configuring Spark

Many of Spark's settings are controlled by environment variables. Since I want all volatile data in /srv, many of these need to be changed. Spark will pick up automatically.

The Intel NUC systems I'm running this stack on have 4 cores and 16G of RAM, so I'll give Spark 2 cores and 4G of memory for now.

One line worth calling out is the SPARK_WORKER_PORT=9000. It can be any port. If you don't set it, every time a work is restarted the master will have a stale entry for a while. It's not a big deal but I like it better this way.

cat > <<EOF
export SPARK_REPL_MEM="4g"
export SPARK_CONF_DIR="/etc/spark"
export SPARK_TMP_DIR="/srv/spark/tmp"
export SPARK_PID_DIR="/srv/spark/pids"
export SPARK_LOG_DIR="/srv/spark/logs"
export SPARK_WORKER_DIR="/srv/spark/work"
export SPARK_LOCAL_DIRS="/srv/spark/tmp"
export SPARK_COMMON_OPTS="$SPARK_COMMON_OPTS -Dspark.kryoserializer.buffer.mb=32 "
export SPARK_MASTER_OPTS=" $LOG4J -Dspark.log.file=/srv/spark/logs/master.log "
export SPARK_WORKER_OPTS=" $LOG4J -Dspark.log.file=/srv/spark/logs/worker.log "
export PYSPARK_PYTHON="/bin/python2"

spark-submit and other tools may use spark-defaults.conf to find the master and other configuration items.

cat > spark-defaults.conf <<EOF
spark.master            spark://
spark.executor.memory   512m
spark.eventLog.enabled  true
spark.serializer        org.apache.spark.serializer.KryoSerializer

The systemd units are a little less complex than Cassandra's. The spark-master.service unit should only exist on node0, while every other node runs spark-worker. Spark workers are given a weight of 100 compared to Cassandra's weight of 1000 so that Cassandra is given priority over Spark without starving it entirely.

cat > spark-worker.service <<EOF
Description=Spark Worker

ExecStart=/opt/spark/sbin/ 1 spark://


The master unit is similar and only gets installed on node0. Since it is not competing for resources, there's no need to turn on cgroups for now.

cat > spark-master.service <<EOF
Description=Spark Master

ExecStart=/opt/spark/sbin/ 1


Now deploy all of these configs. Relocate the spark config into /etc/spark and copy a couple templates, then write all the files there. goes on all nodes. The unit files are described above. Finally, a command is run to instruct systemd to read the new unit files. --list all -c "sudo cp /opt/spark/conf/ /opt/spark/conf/" --list all -c "sudo cp /opt/spark/conf/fairscheduler.xml.template /opt/spark/conf/fairscheduler.xml" --list all -x -l -r /etc/spark/ --list all -x -l spark-defaults.conf -r /etc/spark/spark-defaults.conf --list workers -x -l spark-worker.service -r /etc/systemd/system/ --list all --incl node0 -x -l spark-master.service -r /etc/systemd/system/ --list all -c "sudo systemctl daemon-reload"

With all of that done, it's time to turn on Spark to see if it works. --list all --incl node0 -c "sudo systemctl start spark-master.service" --list workers -c "sudo systemctl start spark-worker.service"

Now I can browse to the Spark master webui.


Installing spark-cassandra-connector

The connector is now published in Maven and can be installed easiest using ivy on the command line. Ivy can pull all dependencies as well as the connector jar, saving a lot of fiddling around. In addition, while ivy can download the connector directly, it will end up pulling down all of Cassandra and Spark. The script fragment below pulls down only what is necessary to run the connector against a pre-built Spark.

This is only really needed for the spark-shell so it can access Cassandra. Most projects should include the necessary jars in a fat jar rather than pushing these packages to every node.

I run these commands on node0 since that's where I usually work with spark-shell. To run it on another machine, Spark will have to be present and match the version of the cluster, then this same process will get everything needed to use the connector.

cat > <<EOF
mkdir /opt/connector
cd /opt/connector

rm *.jar

curl -o ivy-2.3.0.jar \
curl -o spark-cassandra-connector_2.10-1.0.0-beta1.jar \

ivy () { java -jar ivy-2.3.0.jar -dependency \$* -retrieve "[artifact]-[revision](-[classifier]).[ext]"; }

ivy org.apache.cassandra cassandra-thrift 2.0.9
ivy com.datastax.cassandra cassandra-driver-core 2.0.3
ivy joda-time joda-time 2.3
ivy org.joda joda-convert 1.6

rm -f *-{sources,javadoc}.jar


sudo bash

Using spark-cassandra-connector With spark-shell

All that's left to get started with the connector now is to get spark-shell to pick it up. The easiest way I've found is to set the classpath with --driver-class-path then restart the context in the REPL with the necessary classes imported to make sc.cassandraTable() visible.

The newly loaded methods will not show up in tab completion. I don't know why.

/opt/spark/bin/spark-shell --driver-class-path $(echo /opt/connector/*.jar |sed 's/ /:/g')

It will print a bunch of log information then present scala> prompt.

scala> sc.stop

Now that the context is stopped, it's time to import the connector.

scala> import com.datastax.spark.connector._
scala> val conf = new SparkConf()
scala> conf.set("", "")
scala> val sc = new SparkContext("local[2]", "Cassandra Connector Test", conf)
scala> val table = sc.cassandraTable("keyspace", "table")
scala> table.count

To make sure everything is working, I ran some code I'm working on for my 2048 game analytics project. Each context gets an application webui that displays job status.



It was a lot of work getting here, but what we have at the end is a Spark shell that can access tables in Cassandra as RDDs with types pre-mapped and ready to go.

There are some things that can be improved upon. I will likely package all of this into a Docker image at some point. For now, I need it up and running for some demos that will be running on portacluster at OSCON 2014.