Setting up a Cassandra Cluster on your laptop using Docker

Jayaraj Jayaraman
2 min readAug 7, 2018

--

Setting up an Apache Cassandra cluster in your laptop using Docker will be useful if you want to use this NoSql database as part of your development environment.

If you do not have Docker in your laptop, first install Docker following the instruction provided here for Mac, or here for Windows.

Make sure your laptop has at least 8GB RAM available for running at least 2 Cassandra Containers.

Let us start setting up the Cassandra Cluster.

Single instance

Let’s run single Cassandra instance.

$ docker run --name my-cassandra-1 -m 2g -d cassandra:3.11.2

Now, check whether the Cassandra Container is up and running using docker pscommand:

$ docker ps

You should see output similar to this.

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c8d489f00f17 cassandra:3.11.2 "/docker-entrypoint.s" 10 seconds ago Up 10 seconds 7000-7001/tcp, 7199/tcp, 9042/tcp, 9160/tcp my-cassandra-1

Now, find out the IP address of the container and connect to the Cassandra with cqlsh.

$ docker inspect --format='{{ .NetworkSettings.IPAddress }}' my-cassandra-1
172.17.0.2

$ docker run -it --link my-cassandra-1 --rm cassandra:3.11.2 \
bash -c 'exec cqlsh 172.17.0.2'
Connected to Test Cluster at 172.17.0.2:9042.
[cqlsh 5.0.1 | Cassandra 3.11.2 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
cqlsh>

Cluster

To form the cluster we need to run second Cassandra container my-cassandra-2 and connect it to my-cassandra-1.

$ docker run --name my-cassandra-2 -m 2g -d \
-e CASSANDRA_SEEDS="$(docker inspect --format='{{ .NetworkSettings.IPAddress }}' my-cassandra-1)" \
cassandra:3.11.2

Now we have a cluster with two instances. Let’s verify the cluster status by running nodetool status command, which needs to be executed from within the container.

$ docker exec -i -t my-cassandra-1 bash -c 'nodetool status'
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
-- Address Load Tokens Owns (effective) Host ID Rack
UN 172.17.0.3 101.84 KB 256 100.0% fb3d0aef-d70e-4637-8bdf-7e861acfcea6 rack1
UN 172.17.0.2 107.36 KB 256 100.0% ccd1000e-1b9a-422a-a2b4-d291da9c0585 rack1

Make sure that “nodetool” returns “UN” as the status, i.e., Up and Normal, for all the cluster nodes. More instances can be added to this cluster using the same approach.

Client Tools

Now that you have your Cassandra Cluster up and running. To be able to quickly run nodetool commands and/or to use cqlsh, these shell scripts will come handy.

cqlsh.sh

docker run -it — link my-cassandra-1 — rm cassandra:3.11.2 bash -c ‘exec cqlsh 172.17.0.2’

nodetool.sh

docker exec -it my-cassandra-1 bash -c “nodetool “$@””

Hope this helps in setting up your own Cassandra environment for your development purpose as well as for learning to use Cassandra. Enjoy!

--

--