Integrate Kong with a Scalable Apache Cassandra Deployment on Kubernetes

Introduction

Kong is an API development platform that makes it easy to build and deploy sophisticated, cloud-native APIs with analytics, traffic control and authentication features. It is based on NGINX (for request processing) and Apache Cassandra or PostgreSQL (for data storage).

If you're an API developer, you already know that your APIs need to be both resilient and scalable to changes in traffic. One way to ensure this is by adding Kubernetes to the mix. Kubernetes provides built-in capabilities to monitor health, recover from failure and scale up (or down) depending on demand.

This guide walks you through the process of deploying Apache Cassandra on Kubernetes and then configuring Kong to use this Kubernetes deployment as its datastore. By transferring data storage responsibilities to Kubernetes, this approach avoids a single point of failure, increases the resilience of your Kong deployment and makes it easier to scale up in future.

Assumptions and prerequisites

This guide makes the following assumptions:

Step 1: Deploy Apache Cassandra on Kubernetes

The first step is to deploy Apache Cassandra on your Kubernetes cluster. The easiest way to do this is with Bitnami's Apache Cassandra Helm chart, which gives you a ready-to-use deployment with minimal effort and within a few minutes.

Use the commands below to deploy Apache Cassandra on your Kubernetes cluster, remembering to replace the CASSANDRA-PASSWORD placeholder with a unique password for the Cassandra administrator account (by default, the administrator account is configured with the username cassandra):

helm repo add bitnami https://charts.bitnami.com/bitnami
helm install mydeployment bitnami/cassandra  --set cluster.replicaCount=2 --set service.type=LoadBalancer --set dbUser.password=CASSANDRA-PASSWORD
Warning

Using a LoadBalancer service type will typically assign a static IP address for the Cassandra deployment. Depending on your cloud provider's policies, you may incur additional charges for this static IP address.

Wait for the deployment to complete and then run the command below to obtain the IP address for use with Cassandra:

kubectl get svc --namespace default mydeployment-cassandra --template "{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"

Step 2: Create an Apache Cassandra keyspace for Kong

The next step is to confirm that you are able to connect to the new Apache Cassandra deployment from your Bitnami Kong server and create a keyspace for Kong.

  • Log in to the Bitnami Kong cloud server using SSH.

  • At the server console, use the command below to connect to the Apache Cassandra deployment. Replace the CASSANDRA-IP-ADDRESS placeholder with the IP address obtained in the previous step.

    cqlsh -u cassandra CASSANDRA-IP-ADDRESS
    

    Enter the password supplied at deployment-time when prompted to do so.

  • At the Apache Cassandra prompt, use the command below to create a new keyspace for Kong:

    CREATE KEYSPACE bitnami_kong WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}  AND durable_writes = true;
    
  • Confirm that the keyspace has been created by checking the output of the following command:

    DESCRIBE KEYSPACES;
    
  • Log out from Apache Cassandra:

    exit
    

Step 3: Reconfigure Kong to use the new Apache Cassandra deployment

The next step is to update the Kong configuration file with the credentials and IP address for the new Apache Cassandra deployment.

  • Log in to the Bitnami Kong cloud server using SSH (if you're not already logged in).

  • Stop all services:

    sudo /opt/bitnami/ctlscript.sh stop
    
  • Using a text editor, edit the Kong configuration file at /opt/bitnami/kong/conf/kong.conf and update the following values in it. Replace the CASSANDRA-IP-ADDRESS placeholder with the IP address obtained in the first step and the CASSANDRA-PASSWORD placeholder with the password supplied at deployment-time.

    cassandra_contact_points = CASSANDRA-IP-ADDRESS
    cassandra_username = cassandra
    cassandra_password = CASSANDRA-PASSWORD
    

    Save the changes.

  • Run the commands below to bootstrap the Apache Cassandra database:

    cd /opt/bitnami/kong/bin
    sudo kong migrations bootstrap
    
  • Once the database is prepared, stop Kong and restart all services:

    cd /opt/bitnami/kong/bin
    sudo kong stop
    sudo /opt/bitnami/ctlscript.sh start kong
    
  • Confirm that Kong is operating normally:

    sudo /opt/bitnami/ctlscript.sh status kong
    

At this point, Kong is configured to work with your Apache Cassandra deployment.

Step 4: Test and confirm the integration

Test the integration between Kong and Apache Cassandra as follows:

  • Create an example service.

  • Connect to the Apache Cassandra deployment on Kubernetes and check if the service configuration has been saved to the datastore by running the command below:

    cqlsh -e "SELECT * from bitnami_kong.services" -u cassandra CASSANDRA-IP-ADDRESS
    

    Enter your password when prompted.

  • Confirm that you see a database record corresponding to the service you created, as shown below:

Query output

This means that your Kong API gateway has been successfully configured to work with Apache Cassandra deployed on Kubernetes. You can now proceed to auto-scale your deployment in line with demand requirements.

Useful links

To learn more about the topics discussed in this guide, use the links below: