Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I connect to a Cassandra VM

The following code works if Cassandra and the code are on the same machine:

using System;
using Cassandra;

namespace CassandraInsertTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var cluster = Cluster.Builder()
            .AddContactPoint("127.0.0.1")
            .Build();

            var session = cluster.Connect("test_keyspace");

            session.Execute("INSERT INTO test_table (id, col1, col2) VALUES (1, 'data1', 'data2')");

            Console.WriteLine($"Finished");
            Console.ReadKey();
        }
    }
}

Assuming a username and password is needed if the code is on one machine and cassandra is on a different machine (different ip address)? So I have tried:

        var cluster = Cluster.Builder()
        .AddContactPoint("192.168.0.18") <- the ip address for the cassandra node
        .WithPort(9042)
        .WithCredentials("username to log into the cassandra node","password to log into the cassandra node")
        .Build();

I get the following error message:

userone@desktop:~/Desktop/vsc$ dotnet run
Unhandled exception. Cassandra.NoHostAvailableException: All hosts tried for query failed (tried 192.168.0.18:9042: SocketException 'Connection refused')
   at Cassandra.Connections.ControlConnection.Connect(Boolean isInitializing)
   at Cassandra.Connections.ControlConnection.InitAsync()
   at Cassandra.Tasks.TaskHelper.WaitToCompleteAsync(Task task, Int32 timeout)
   at Cassandra.Cluster.Cassandra.SessionManagement.IInternalCluster.OnInitializeAsync()
   at Cassandra.ClusterLifecycleManager.InitializeAsync()
   at Cassandra.Cluster.Cassandra.SessionManagement.IInternalCluster.ConnectAsync[TSession](ISessionFactory`1 sessionFactory, String keyspace)
   at Cassandra.Cluster.ConnectAsync(String keyspace)
   at Cassandra.Tasks.TaskHelper.WaitToComplete(Task task, Int32 timeout)
   at Cassandra.Tasks.TaskHelper.WaitToComplete[T](Task`1 task, Int32 timeout)
   at Cassandra.Cluster.Connect(String keyspace)
   at HelloWorld.Program.Main(String[] args) in /home/userone/Desktop/vsc/Program.cs:line 17
userone@desktop:~/Desktop/vsc$ 

The iptables on the node (the cassandra server) is currently set as follows:

node1@node1:~$ sudo iptables -S
-P INPUT ACCEPT
-P FORWARD ACCEPT
-P OUTPUT ACCEPT
-A INPUT -i lo -j ACCEPT
-A IMPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -s 192.168.0.73/32 -p tcp -m multiport --dports 7000,7001,7199,9042,9160,9142 -m state --state NEW,ESTABLISHED -j ACCEPT
node1@node1:~$

Note 1: Both the machine with the app and the machine with cassandra installed can be pinged and tracerouted in both directions.

Note 2: I have tested the username and password and can log into the cassandra server without any issues when tried directly on the server.

Note 3: Cassandra is running in a VM which I just created today. The VM is a guest machine on the host machine which runs the code.

Note 4: Both the Host OS and Guest OS are Linux.

like image 446
oshirowanen Avatar asked Oct 19 '19 18:10

oshirowanen


People also ask

How do I connect to Cassandra from a different machine?

To connect to Cassandra from a different machine, you must open ports 9042, 9160, 7000, 7001 and 7199 for remote access. Refer to the FAQ for more information on this.

What ports do I need to connect to a Cassandra VM?

This is important to note because the same goes for the Cassandra ports 8888 (OpsCenter), and 9042 (native transport). Create endpoints for these ports and use them when connecting remotely. Create a public IP address that points to the VM itself rather than the cloud service.

What is Azure Managed instance for Cassandra?

Azure Managed Instance for Apache Cassandra If you're looking for a more automated service for running Apache Cassandra on Azure virtual machines, consider using Azure Managed Instance for Apache Cassandra. This service automates the deployment, management (patching and node health), and scaling of nodes within an Apache Cassandra cluster.

What size virtual machine do I need for Cassandra on azure?

Cassandra workloads on Azure commonly use either Standard_DS14_v2 or Standard_DS13_v2 virtual machines. Cassandra workloads benefit from having more memory in the VM, so consider memory optimized virtual machine sizes, such as Standard_DS14_v2, or local-storage optimized sizes such as Standard_L16s_v2.


2 Answers

.AddContactPoint("127.0.0.1")

If that works from the same machine, then you probably have Cassandra bound to that IP. If you need to connect to your node(s) remotely, then you need to bind a routeable IP to that node.

Run a nodetool status. If you see your cluster status showing your node with an IP of 127.0.0.1, then connecting to the local machine from the local machine is the only scenario that will ever work.

Try running the following command on your node:

grep _address cassandra.yaml

The IP address returned in the output is the only one that an application is allowed to connect to. If you want to be able to connect to 192.168.0.18, then the listen and rpc addresses should look something like this:

listen_address: 192.168.0.18
rpc_address: 192.168.0.18

Note that you'll need to change your seeds list, too.

Also, if you're on a VM/provider that has both internal and external IP addresses, then you'll also need to set your broadcast_ addresses to the external IP:

broadcast_address: 10.6.5.5
broadcast_rpc_address: 10.6.5.5
listen_address: 192.168.0.18
rpc_address: 192.168.0.18

But try setting just listen and rpc to 192.168.0.18 first.

Edit 20191022

Just wanted to double check, do I add 192.168.0.18 as the listen_address and rpc_address to the cassandra node where the cassandra node has the ip address 192.168.0.18?

Yes. Also make sure that your node's seed list is set like this:

- seeds: "192.168.0.18"

Before I did that, the value of the listen_address and rpc_address were set to localhost

I thought so.

However, after making the changes you suggested, nodetool status now gives me

Failed to connect to 127.0.0.1:7199 - connection refused

Ironically, that's the same message nodetool returns when Cassandra is not running. At this point I would check the system log and see if it is returning errors that may be preventing it from starting. I suspect that the seed list still reads "127.0.0.1".

tl;dr;

If you intend to connect to your cluster/node remotely, then you cannot use the default configurations which bind Cassandra to the home IP (127.0.0.1/localhost). And that includes all _address settings, as well as your seeds list.

like image 134
Aaron Avatar answered Oct 21 '22 02:10

Aaron


I suggest using something like telnet 192.168.0.18 9042 to test whether the network is properly configured for this scenario. The error that the driver is throwing is just a standard socket error that is returned when the driver can't connect to the cassandra host.

like image 1
João Reis Avatar answered Oct 21 '22 03:10

João Reis