Apache Camel used on a Kubernetes Cassandra cluster


Introduction

Eclipse JKube 1.0.0 GA will be released on September 9, 2020. As I mentioned in previous posts, JKube is the successor of the deprecated Fabric8 Maven Plugin (FMP). As such, our main goal right now is to migrate the current user-base to the new project. Aligned with this strategy, we started to create Pull Requests on those repositories that are currently using FMP in order to replace the deprecated dependency.

In this article, I’ll describe how to run the updated Apache Camel example (now using JKube) for Cassandra running on Kubernetes, and its expected behavior. This blog post is strongly based on the original article written by the great Andrea Cosentino back in 2016.

Spinning up the Apache Cassandra cluster on Kubernetes

The first step is to clone the Apache Camel examples project and navigate to the example folder:

For the purpose of this blog, I’m using Minikube, but you could run the example on any other Kubernetes cluster.

In order to spin up the Apache Cassandra cluster we need to apply the two YAML files provided in the src/resources/jkube directory:

After a few minutes, the Pods defined in the cluster StatefulSet should have started, we can check their status by running:

When both Pods are with running status, the Apache Cassandra cluster will be ready to use.

Apache Camel example

The example is a very simple Apache Camel application that contains a single route that will log some information every 5 seconds.

Java classes

The project contains 2 Java classes. CqlPopulateBean will create a Cassandra Keyspace and Table and add 2 entries to that table in the Cassandra cluster we deployed in the previous step whenever the populate() method is invoked.

The RowProcessor is an Apache Camel Processor implementation that will transform the List of Rows retrieved from the CQL query into a more friendly String that we can log.

Camel Context

Following you can see an excerpt of the camel-context.xml file:

The file provides a definition for 2 beans and a single route.

The first bean (populate) will be instantiated during the application startup and the populate method will be called. As I explained before, this will add 2 rows to the users table.

The second bean (rowProcessor) is an instance of the RowProcessor class defined earlier.

Finally, we can see the route definition (cassandra-route). The route starts from a timer that runs every 5 seconds. It then performs a CQL query that will fetch all users from the test Keyspace in the DC1-K8Demo datacenter. The resulting list of rows will be processed by the processor bean and the resulting string will be logged.

Running the example

Now that we’ve seen all of the components for our example we can run it and take it for a spin.

If you are running this on Minikube, you will first need to share your Docker Daemon with your host system by running: eval $(minikube docker-env)

Note: In case you are running the example in an external cluster you will need to push the built Docker image to a registry accessible to the K8s cluster so that the image can be pulled.

We are now ready to deploy the application, we can do so by running:

The previous command will package the application, build a Docker Image, create cluster resource manifests, and apply them into the Minikube cluster. As you can see, the use of Eclipse JKube really simplifies the development effort by leveraging these tasks from the developer. Following is an excerpt of the pom.xml containing the JKube’s Kubernetes Maven Plugin configuration:

As you can see, the plugin doesn’t require much configuration. The only specific configuration is the one for the execution that will simply bind the k8s:resource and k8s:build goals to the Maven install phase.

After a few moments, the Pod defined in the Deployments will start.

We can now retrieve the logs by running the following Maven command:

If everything went well you should see an output similar to this:

As you can see in the last entries of the log, the rows inserted with the CqlPopulateBean class are now being retrieved using CQL and transformed using the RowProcessor.

Conclusion

In this article, I described to you the behavior of the Apache Camel Cassandra Kubernetes example. This is a really simple example but highlights the power of Apache Camel, Apache Cassandra, and Eclipse JKube used together. More complex architectures to solve real-world scenarios can be easily built on top of this example.

Leave a comment

Your email address will not be published. Required fields are marked *