druuu
druuu

Reputation: 1716

Cassandra client connection issue within Docker from an application container

batchWorker_1  | [DEBUG] 2017-10-30 12:42:10.035 [cluster1-nio-worker-0] Connection - Connection[/172.17.0.3:9042-1, inFlight=0, closed=false] Error connecting to /172.17.0.3:9042 (connection timed out: /172.17.0.3:9042)
batchWorker_1  | [DEBUG] 2017-10-30 12:42:10.037 [cluster1-nio-worker-0] STATES - Defuncting Connection[/172.17.0.3:9042-1, inFlight=0, closed=false] because: [/172.17.0.3:9042] Cannot connect
batchWorker_1  | [DEBUG] 2017-10-30 12:42:10.038 [cluster1-nio-worker-0] STATES - [/172.17.0.3:9042] preventing new connections for the next 1000 ms
batchWorker_1  | [DEBUG] 2017-10-30 12:42:10.038 [cluster1-nio-worker-0] STATES - [/172.17.0.3:9042] Connection[/172.17.0.3:9042-1, inFlight=0, closed=false] failed, remaining = 0
batchWorker_1  | [DEBUG] 2017-10-30 12:42:10.039 [cluster1-nio-worker-0] Connection - Connection[/172.17.0.3:9042-1, inFlight=0, closed=true] closing connection
batchWorker_1  | [DEBUG] 2017-10-30 12:42:10.042 [main] ControlConnection - [Control connection] error on /172.17.0.3:9042 connection, no more host to try
batchWorker_1  | com.datastax.driver.core.exceptions.TransportException: [/172.17.0.3:9042] Cannot connect
batchWorker_1  |    at com.datastax.driver.core.Connection$1.operationComplete(Connection.java:165) ~[batch_worker_server.jar:0.01]
batchWorker_1  |    at com.datastax.driver.core.Connection$1.operationComplete(Connection.java:148) ~[batch_worker_server.jar:0.01]
...

I am running my application and a Cassandra container, trying to establish connection from application container to Cassandra container.

  1. I tried with docker-compose. It throws the same error. It is able to resolve the right container IP (as you can see) but failing to connect.
  2. I tried to run by starting cassandra container separately and harddcode the IP in my application container, it still fails.
  3. The Cassandra container works fine, if i run the same app outside, it connect.

The issue is that, it is not able to resolve the Cassandra container IP from the application container. Not sure why.

I also enabled start_rpc and exposed all cassandra related ports. Still no luck.

Upvotes: 2

Views: 3885

Answers (2)

Jinna Baalu
Jinna Baalu

Reputation: 7809

Issue

[Control connection] error on /IP:9042 connection, no more host to try

Solution

Silly but important check "App tries to connect before cassandra is up"

RPC_ADDESS Default IP Issue

As containers have their own network. So every container will takes its own IP. As your RPC_ADDESS is set to container IP it will through this error.

In Cassandra configuration change the IP address of

rpc_address (Default: localhost) The listen address for client connections (Thrift RPC service and native transport). Valid values:

  1. unset: Resolves the address using the configured hostname

configuration of the node. If left unset, the hostname resolves to the IP address of this node using /etc/hostname, /etc/hosts, or DNS.

  1. 0.0.0.0: Listens on all configured interfaces. You must set the broadcast_rpc_address to a value other than 0.0.0.0.
  1. IP address 4. hostname

RPC_ADDRESS=0.0.0.0

Here is docker-compose.yml file

version: '2'
services:  
    cassandra:
        container_name: cassandra
        image: cassandra:3.9
        volumes:
            - /path/of/host/for/cassandra/:/var/lib/cassandra/
        ports:
            - 7000:7000
            - 7001:7001
            - 7199:7199
            - 9042:9042
            - 9160:9160
        environment:
            - CASSANDRA_CLUSTER_NAME='cassandra-cluster'
            - CASSANDRA_NUM_TOKENS=256
            - CASSANDRA_RPC_ADDRESS=0.0.0.0
        restart: always

Upvotes: 4

Alex Ott
Alex Ott

Reputation: 87099

You can create Docker network as described in documentation, and connect Cassandra & your application to same network.

You also need to check on what interfaces Cassandra is listening - is it single interface, or all?

Upvotes: 1

Related Questions