Difficulty: Intermediate
Estimated Time: 20 minutes

Let's create a Cassandra Docker micro-service!

In this scenario, we'll learn how to:

  • Spin up Cassandra in a container
  • Set up a Django REST project
  • Create a Django/REST microservice and connect it to Cassandra
  • Double the Cassandra cluster by adding another Cassandra container

You gotta love microservices! We'll get you started!

ver 0.006

In this scenario, we learned how to:

  • Spin up Cassandra in a container
  • Set up a Django REST project
  • Create a Django/REST microservice and connect it to Cassandra
  • Double the Cassandra cluster by adding another Cassandra container

This is a simple service, but it illustrates the important broad brush strokes.

Build a Containerized Cassandra Python REST Microservice

Step 1 of 4

Run Cassandra in a container

The first step to setting up a Cassandra/REST microservice is to get a Cassandra cluster. In this step we'll build a small cluster of one node in its own container.

Start by grabbing the Cassandra image.

docker pull cassandra

Before we can run the Cassandra container, let's create a network that we can use for our microservices. We'll name the network service-net.

docker network create --driver bridge service-net

Now, we are ready to run the Cassandra container. We'll call this container cassandra-1 (Spoiler Alert: The -1 must imply there will be more Cassandra containers later...). The -d option runs the container disconnected, or in other words, in the background. Note that we connect it to the service-net network we just created.


ProTip: When you create a user-defined network in Docker, you can access the containers in the network using the container name.


docker run -d --name cassandra-1 --network service-net --rm -t cassandra

When we want to check the status of our Cassandra cluster, we can use the nodetool status command.

docker exec -it cassandra-1 nodetool status

The -it option means we want to interact with the container.

The UN in the output means the node is Up and Normal, or in other words, ready to go.

Let's create a keyspace and table in our new cluster. We'll call the keyspace user_management and the table user_credentials. We'll use the CQL shell, cqlsh, and send it the commands using the -e option.

docker exec -it cassandra-1 cqlsh -e "
CREATE KEYSPACE user_management
  WITH REPLICATION = {
    'class' : 'SimpleStrategy',
    'replication_factor' : 1
  };
CREATE TABLE user_management.user_credentials(
  username        text,
  password        text,
  PRIMARY KEY(username));"

Let's insert a row into the table.

docker exec -it cassandra-1 cqlsh -e "
INSERT INTO user_management.user_credentials
  (username, password)
  VALUES ('User1', 'YouWontGuessThis');"

Note: We would never store passwords in plain-text in a production system, but we will for this example to keep things simple.


Finally, we'll query the table for the row we just inserted.

docker exec -it cassandra-1 cqlsh -e "
SELECT * from user_management.user_credentials WHERE username = 'User1';"

Woot! You now know how to set up Cassandra in a container!