Getting Exception in thread "main" com.datastax.driver.core.exce

ghz 8months ago ⋅ 115 views

Getting Exception in thread "main" com.datastax.driver.core.exceptions.NoHostAvailableException

I'm trying to deploy scyllaDB on kubernetes, so Have done the following steps:

here is deployment.yml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: scylla-db
spec:
  replicas: 1
  selector:
    matchLabels:
      app: scylla-label
  template:
    metadata:
      labels:
        app: scylla-label
    spec:
      containers:
        - name: scylla
          image: scylladb/scylla:4.5.0
          ports:
            - containerPort: 9042

Here is my service.yml:

apiVersion: v1
kind: Service
metadata:
  name: scylla-db-service
spec:
  type: NodePort
  
  selector:
    app: scylla-label
  ports:
    - protocol: TCP
      port: 9042
      targetPort: 9042

after deploying pod and service I got:

$ kubectl get po

NAME                                  READY   STATUS              RESTARTS   AGE
scylla-db-59bff57bbd-pjwm7            1/1     Running             0          57m

$ kubectl get svc

NAME                             TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
scylla-db-service                NodePort       10.106.91.33    <none>        9042:31486/TCP   71m

Here is my scala code that simply build a connection with scylladb and insert data into database.

DataSchema.scala:

import com.outworkers.phantom.dsl._

abstract class DataSchema extends Table[DataSchema, DataModelClass] {

  override def tableName: String = "dataTable"

  object id extends StringColumn with PartitionKey {
    override def name: String = "id"
  }

  object name extends StringColumn {
    override def name: String = "name"
  }

}

DB.scala:

import com.outworkers.phantom.dsl._

class DB(override val connector: CassandraConnection)
  extends Database[DB](connector) {
  object Data extends DataSchema with Connector
}

ScyllaDBConnection.scala:

import com.outworkers.phantom.connectors._
import com.outworkers.phantom.dsl.CassandraConnection
import com.typesafe.config.{Config, ConfigFactory}

class ScyllaDBConnection {

  def connectionProvider: CassandraConnection = {

    val singleNodeIP = "10.106.91.33"

    val connection: CassandraConnection =
      ContactPoints(Seq(singleNodeIP))
        .keySpace("ks")
    connection
  }
}

Repository.scala:

import com.outworkers.phantom.ResultSet
import com.outworkers.phantom.dsl._
import scala.concurrent.Future

object Repository {

  val scyllaDBConnection:CassandraConnection = new ScyllaDBConnection().connectionProvider
  val db = new DB(scyllaDBConnection)

  import db.{session, space}

  def insertData(modelClass: DataModelClass): Future[ResultSet] = {
    db.Data.store(modelClass)
      .future()
  }
}

Unfortunately I'm getting NoHostAvailableException.

complete error is:

15:15:21.643 [main] DEBUG com.datastax.driver.core.ControlConnection -- [Control connection] error on /10.106.91.33:9042 connection, no more host to try
com.datastax.driver.core.exceptions.TransportException: [/10.106.91.33:9042] Cannot connect

What I tried

added service (scylla-db-service) name in ContactPoints instead of IP, but still getting same error.

Answers

It seems like you're encountering connectivity issues between your Scala code and the ScyllaDB instance running on Kubernetes. Let's troubleshoot this step by step:

  1. Check ScyllaDB Pod Logs: First, let's ensure that the ScyllaDB pod is running correctly and there are no errors in its logs. You can retrieve the logs using the kubectl logs command:

    kubectl logs scylla-db-59bff57bbd-pjwm7
    

    This command will show you the logs of the ScyllaDB pod. Check for any errors or warnings that might indicate why the pod is not reachable.

  2. Check Kubernetes Networking: Ensure that there are no network policies or firewalls blocking traffic between your Scala application and the ScyllaDB service. Also, verify that the ScyllaDB service is reachable from within the Kubernetes cluster.

  3. Use Service DNS Name: Instead of using the IP address directly, try using the DNS name of the service (scylla-db-service) in your Scala code. Kubernetes provides DNS resolution for service names, so it should resolve to the correct IP address.

    Modify your ScyllaDBConnection.scala to use the service DNS name:

    val singleNodeService = "scylla-db-service"
    
    val connection: CassandraConnection =
       ContactPoints(Seq(singleNodeService))
         .keySpace("ks")
    
  4. Check Cassandra Connection Configuration: Ensure that your Scala application's Cassandra connection configuration matches the configuration of your ScyllaDB instance, including the keyspace name.

  5. Check ScyllaDB Pod Readiness: Verify that the ScyllaDB pod is in the Running state and ready to accept connections. You can check this using the kubectl get pods command.

  6. Check Service Configuration: Ensure that the service type (NodePort) and ports match between your service YAML (service.yml) and your Scala code.

  7. Check ScyllaDB Image Version: Make sure that the version of the ScyllaDB Docker image you're using (scylladb/scylla:4.5.0) is compatible with your Kubernetes environment and configuration.

By following these steps and troubleshooting each component, you should be able to identify and resolve the issue with connecting to ScyllaDB from your Scala application running on Kubernetes. If you continue to experience issues, please provide any additional error messages or logs for further assistance.