Unable to connect to Scylladb in Docker using C# driver - c#

I have created a single node ScyllaDB in docker, which is up and running, and below is my docker-compose commands:
version: "3"
services:
scylla-node1:
container_name: scylla-node1
image: scylladb/scylla
restart: always
command: --smp 2 --memory 1500M --broadcast-rpc-address 127.0.0.1 --listen-address 0.0.0.0
ports:
- 9042:9050
networks:
web:
networks:
web:
driver: bridge
Reading the documentation for Scylla it recommends using the DataStax C# Driver for Apache Cassandra. So, I have used this in my solution. Following the basic examples, I am struggling to get it work. Thus,
var cluster = Cluster.Builder()
.AddContactPoints("0.0.0.0")
.Build();
var session = cluster.Connect("sample_keyspace");
When the code reaches the Connect command it throws the following error
Cassandra.NoHostAvailableException: 'All hosts tried for query failed (tried 0.0.0.0:9042: SocketException 'The requested address is not valid in its context.')'
Firstly, I can connect to Scylla through the CSQL Utility and can create a Keyspace and then run a query to confirm that the Keyspace has been created.
Is this a problem with C# Driver or am I doing something wrong?

This looks like a networking issue to me because your Docker container doesn't appear to be configured correctly.
The problem is that the C# driver (your application) is not able to connect to the container because there is no network connectivity. That is what this exception means:
Cassandra.NoHostAvailableException: 'All hosts tried for query failed (tried 0.0.0.0:9042: \
SocketException 'The requested address is not valid in its context.')'
I assume "CSQL utility" is a typo. The reason you are able to connect using cqlsh is because you are most likely connecting with:
$ docker exec -it scylla-node1 cqlsh
which isn't the same as connecting remotely. To be fair I'm making an assumption as you didn't provide details.
You've configured the container with --broadcast-rpc-address 127.0.0.1 so it will only listen for client connections on localhost. This means that you can't use 0.0.0.0 as the contact point.
I've also noted that you've mapped the container port 9050 to host port 9042:
ports:
- 9042:9050
By default, Cassandra listens for client connections on port 9042, not 9050. If nothing is listening on port 9050, there's nothing for the driver to connect to and will also lead to NoHostAvailableException. Cheers!

Related

Docker SQL-Server login problem: AuthenticationException: The remote certificate was rejected by the provided RemoteCertificateValidationCallback

I'm working on a Docker related application, written in C#, based on Entity Framework.
One of the Docker containers, is the ms-sql-server, which is used for database access.
In order to access the SQL-server, following connectionString is created:
Server=ms-sql-server;Initial Catalog=Ownobjects;User ID=SA;Password=some_password
While performing following source code:
public void InitialiseDatabase(IApplicationBuilder app)
{
using (var context = app.ApplicationServices.GetRequiredService<ApplicationDbContext>())
{
context.Database.EnsureCreated();
...
... I get following Exception:
Microsoft.Data.SqlClient.SqlException: 'A connection was successfully established with the server,
but then an error occurred during the pre-login handshake.
(provider: TCP Provider, error: 35 - An internal exception was caught)'
Inner Exception
AuthenticationException: The remote certificate was rejected
by the provided RemoteCertificateValidationCallback.
According to this similar StackOverflow post, two things might not match: the instance name in the connection string and the expected name from the instance.
Is this true? In that case, where or how can I find both names?
If this is not true, then what might be causing this issue?
Edit:
Some further examination:
In the Logs of the ms-sql-server Docker container, I've found following line:
Server name is 'e614890825ac'.
This is an informational message only.
No user action is required
I've used this entry in the connectionString but then the same Exception is raised, but this time with following Inner Exception::
ExtendedSocketException: Resource temporarily unavailable
Edit2:
Again some further examination:
In the meantime I've discovered that my original servername in the connectionString is correct.
Thanks in advance
I've spent some time solving this message SQL Server Pre-Login Handshake
Solution 1 (Encryption in client container):
Dockerfile client (github issue)
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
# ...
# SQL Server trusted connection problem
RUN sed -i 's/CipherString = DEFAULT#SECLEVEL=2/CipherString = DEFAULT#SECLEVEL=1/g' /etc/ssl/openssl.cnf
# ...
Note: There's no need to change your connection string adding Encrypt=false or anything else with this setting
Note 2: I'have tried to use focal and other linux images and only aspnet:6.0 works with this setting.
Solution 2 (Image version or storage problems):
Change the runtime version of the client container (see github comments)
Replace the binding storage or volume in sql server container. This could be a user access problem.
Note: There are some comments about this github issue

ASP.NET Core disable HTTPS

I have a dockerized ASP.NET Core app that I'm trying to run locally under Linux (Fedora 33). I'd like to disable HTTPS so I don't have to deal with constant certificate errors.
To do this I simply disabled the HTTPS redirect feature which is included by default, however the app then wasn't responding to any HTTP requests at all.
I managed to get the dev version of the app to run on HTTP by explicitly configuring Kestrel in Startup.cs:
webBuilder.UseKestrel(options => {
options.ListenAnyIP(5000);
});
However, it isn't working for the Production version of the app. Whenever I send a request to http://0.0.0.0:5000, I just get a curl: (56) Recv failure: Connection reset by peer error.
Running netstat -ap in the container doesn't show the app as being bound to the port:
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 localhost:46825 0.0.0.0:* LISTEN -
udp 0 0 localhost:36022 0.0.0.0:* -
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags Type State I-Node PID/Program name Path
unix 2 [ ACC ] STREAM LISTENING 27739639 1/dotnet /tmp/dotnet-diagnostic-1-3889474-socket
During startup the app also doesn't display the same information that I would normally expect, e.g. the following from another of my dockerized ASP.NET Core apps that does use HTTPS:
web-prod_1 | Hosting environment: Production
web-prod_1 | Content root path: /app
web-prod_1 | Now listening on: https://[::]:443
web-prod_1 | Now listening on: http://[::]:80
web-prod_1 | Application started. Press Ctrl+C to shut down.
The app doesn't print any of this on start, which is a little concerning.
I'd really appreciate any help with this, I've tried just about everything I can think of and I'm pulling my hair out over it.
EDIT
The cause of this was unrelated to HTTPS - a HostedService with a StartAsync that didn't terminate was being added in Startup.cs. Somehow this was stopping the rest of the app from starting, but only in production builds. Moving the AddHostedService call to Program.cs resolved the issue.
But is the container engine actually starting with the flags to bind the container and hosts ports? Conection reset means that the port is closed and actively rejecting connection.
You mention you did a netstat from inside the container, can you run another one from the host while the container is running? I like to use netstat -plnt
If your container is exposing the correct ports to the OS you should see it and be able to reach it at localhost:port, here I have a mysql proxy exposing 3306
❯ sudo netstat -plnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
1/init
tcp6 0 0 :::3306 :::* LISTEN 300988/containers-r
Which I had to specify the port binding when launching it (depending on your container engine this is done in different flags/options)
docker run -ti -p 3306:3306 eu.gcr.io/xxx-xx/svc-xx-xx-tests
1st cheek by other browser
or
cheek blocking antivirus or firewall of your PC may interrupted it.
then
change the port number before by cheeking the ip and port number by ip scanner.
as you now when you need change http to https need SSL certificate , so set disable option Tools->option->environment->webbrowsers

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.
.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.
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.
I do not have any experience with Cassandra, but almost all of my connectivity problems were related to firewalls' configuration, OS distributed and network services, etc.
So, if everything works well locally, do not change anything in source code before you are entirely sure about it.
I suggest to check the following list first:
Check all servers' firewall configuration
Check if all needed services are run in all servers (search if Cassandra is depended on any services).
Check the network firewall and subnet port and protocol access limitation if exist.
Check if Cassandra is fully and functionally up and works on port 9042.

Cannot connect to Mysql service from c# front-end in kubernetes

I have a kubernetes cluster with front end as C# console application written in .NET Core and a backend Mysql db. Both of these applications are deployed as deployments in kubernetes in different pods. I have also created a mysql service to be able to connect with Mysql db. But, I can't seem to connect to mysql server from .NET core console app to kubernetes Mysql service. However, I can pretty much connect with Mysql pod using the IP address from the console app.
I will try my best to describe the situation here.
$ kubectl -n radiomicsapp get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
anonymizer-pod-586548ddd9-kv8cj 2/2 Running 0 21s 10.244.0.187 aks-agentpool-35971152-1
mysql-744bfb878c-scwz9 1/1 Running 0 19h 10.244.1.244 aks-agentpool-35971152-2
$ kubectl -n abcapp get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysql ClusterIP 10.0.54.120 <none> 3306/TCP 104d
If I use IP address from the pod (10.244.1.244) to connect with db, it works. However, if I use IP address (10.0.54.120) from mysql service, it throws
Error: Unable to connect to any of the specified MySQL hosts.
Since, the IP address of the mysql pod changes whenever the pod is restarted/recreated, I will have to change the db connection string in my anonymizer console app, I created a mysql service with type ClusterIP as given below:
apiVersion: v1
kind: Service
metadata:
name: mysql
namespace: abcapp
labels:
app: abc
spec:
type: ClusterIP
ports:
- name: mysql
port: 3306
protocol: TCP
targetPort: 3306
selector:
app: abc
Also, my anonymizer pod, mysql pod as well as mysql service are all in the same namespace.
Any help would be much appreciated. Thanks for taking your time.
You have to use the service name on your code and leave kube-dns to handle the IP resolution for you to be sure your code works no matter if the pod is recreated or moved and it takes a different IP address during the process.
From another answer is easy to extrapolate an approximate example
How to connect to MySQL Database?
string connstring = string.Format("Server=mysql; database={0}; UID=UserName; password=your password", databaseName);
connection = new MySqlConnection(connstring);
Notice that only need to put on the Server mysql

Telnet to the RabbitMQ Stomp adapter just hung

I am using RabbitMQ server 3.5.6 on windows 8.1 and Windows 7 x64.
I have to access the Stomp Adapter embedded with the RabbitMQ Server.
So i have configured it from https://www.rabbitmq.com/stomp.html
it shows that it is successfully enabled and configured the rabbitmq_stomp.
After doing this i have restarted my computer and try to telnet to the localhost 61613 (the default socket of the rabbitmq_stomp) and it just hung in there saying connecting.
The RabbitMQ server log says:
=INFO REPORT==== 4-Dec-2015::17:53:29 ===
accepting STOMP connection <0.327.0> ([::1]:49397 -> [::1]:61613)
what did i do wrong? any help would be appreciated.
I find out a way to test the STOMP with RabbitMQ, you can issue the following command to test it. The solution here is by using nc, I didn't find a way using telnet under Windows though.
echo -e 'CONNECT\naccept-version:1.0,1.1,1.2\n\n\0' | nc -q10 127.0.0.1 61613
Normally the client should send the first byte in that protocal, usually something like CONNECT\naccept-version:1.0,1.1,1.2\nhost:yourstomp, needs a null byte , in this case is Ctrl-#, at the end to terminate, so that would be easier to send via echo -e or similar.

Categories

Resources