Home > Back-end >  Connect to Cassandra on Kubernetes using java-driver
Connect to Cassandra on Kubernetes using java-driver

Time:11-30

We are bringing up a Cassandra cluster, using k8ssandra helm chart, it exposes several services, our client applications are using the datastax Java-Driver and running at the same k8s cluster as the Cassandra cluster (this is testing phase)

CqlSessionBuilder builder = CqlSession.builder();

What is the recommended way to connect the application (via the Driver) to Cassandra?

Adding all nodes?

for (String node :nodes) {
   builder.addContactPoint(new InetSocketAddress(node, 9042));
}

Adding just the service address?

 builder.addContactPoint(new InetSocketAddress(service-dns-name , 9042))

Adding the service address as unresolved? (would that even work?)

 builder.addContactPoint(InetSocketAddress.createUnresolved(service-dns-name , 9042))

CodePudding user response:

The k8ssandra Helm chart deploys a CassandraDatacenter object and cass-operator in addition to a number of other resources. cass-operator is responsible for managing the CassandraDatacenter. It creates the StatefulSet(s) and creates several headless services including:

  • datacenter service
  • seeds service
  • all pods service

The seeds service only resolves to pods that are seeds. Its name is of the form <cluster-name>-seed-service. Because of the ephemeral nature of pods cass-operator may designate different C* nodes as seed nodes. Do not use the seed service for connecting client applications.

The all pods service resolves to all Cassandra pods, regardless of whether they are readiness. Its name is of the form <cluster-name>-<dc-name>-all-pods-service. This service is intended to facilitate with monitoring. Do not use the all pods service for connecting client applications.

The datacenter service resolves to ready pods. Its name is of the form <cluster-name>-<dc-name>-service This is the service that you should use for connecting client applications. Do not directly use pod IPs as they will change over time.

CodePudding user response:

Adding all nodes?

You definitely do not need to add all of the nodes as contact points. Even in vanilla Cassandra, only adding a few is fine as the driver will gossip and find the rest.

Adding just the service address?

Your second option of binding on the service address is all you should need to do. The nice thing about the service address, is that it will account for changing/removing of IPs in the cluster.

  • Related