How to connect a google cloud function to cassandra database in kubernetes cluster

3/28/2019

I deployed a cassandra cluster in google cloud platform with kubernetes (with terraform).

The cassandra cluster is working well, I'm running some commands with kubectl exec -it cassandra-0 bash and the data is propagating to all other nodes, all is working well.

When I run host cassandra I see this:

cassandra.default.svc.cluster.local has address 10.32.1.4
cassandra.default.svc.cluster.local has address 10.32.1.5
cassandra.default.svc.cluster.local has address 10.32.3.12

Until here, all is right.

But now I have a question that I don't know how to solve. I have an Google Cloud Function that I want to connect to this database and insert some data.

What is the best way to do this? I guess that I shouldn't expose the database to the internet. But when I try to connect cloud function to cassandra.default.svc.cluster.local I can't see the server.

I use cluster_ip = "None" on the cassandra service

UPDATED: Added the following information:

My Google Cloud Function is this:

exports.helloGET = function helloGET (req, res) {
    const cassandra = require('cassandra-driver');
    const client = new cassandra.Client({ contactPoints: ['cassandra.default.svc.cluster.local'], localDataCenter: 'DC1', keyspace: 'my_bd_name' });

    const query = 'SELECT * FROM User';
    client.execute(query)
        .then(result => console.log('Users: ', dump(result.rows)));

    res.send('Cassandra loaded... :' + dump(result.rows));
};
-- Rui Martins
cassandra
google-cloud-functions
kubernetes

1 Answer

3/28/2019

A full SRV record is expected.

SRV records

SRV Records are created for named ports that are part of normal or Headless Services. For each named port, the SRV record would have the form _my-port-name._my-port-protocol.my-svc.my-namespace.svc.cluster.local. For a regular service, this resolves to the port number and the domain name: my-svc.my-namespace.svc.cluster.local. For a headless service, this resolves to multiple answers, one for each pod that is backing the service, and contains the port number and the domain name of the pod of the form auto-generated-name.my-svc.my-namespace.svc.cluster.local.

In your case you created a headless service (without a cluster IP as you used cluster_ip = "None") and need to find the auto-generated-name of auto-generated-name.cassandra.default.svc.cluster.local.

-- char
Source: StackOverflow