[CSHARP-3943] Problem connect AKS to mongodb Created: 01/Nov/21  Updated: 27/Oct/23  Resolved: 17/Nov/21

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Pavel Shapurau Assignee: James Kovacs
Resolution: Gone away Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

We have application on .Net in Azure AKS. We have connect from CLI with pods, node, VM in this virtual network to private and to public link in Mongodb Atlas, but we can't connect from application in this pods. But if we expand the second node, and transfer this pod to the second node, the application works. We turn off the first node and the application does not work. We raise node 1 and transfer it to applications and the application works and connects to the Mongo database.
If it is not possible to connect to monga, the following messages appear in the logs:
System.TimeoutException: A timeout occurred after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector

{ AllowedLatencyRange = 00:00:00.0150000 }

, OperationsCountServerSelector }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "ReplicaSet", Type : "ReplicaSet", State : "Disconnected", Servers : [] }.
at MongoDB.Driver.Core.Clusters.Cluster.ThrowTimeoutException(IServerSelector selector, ClusterDescription description)
at MongoDB.Driver.Core.Clusters.Cluster.WaitForDescriptionChangedHelper.HandleCompletedTask(Task completedTask)
at MongoDB.Driver.Core.Clusters.Cluster.WaitForDescriptionChanged(IServerSelector selector, ClusterDescription description, Task descriptionChangedTask, TimeSpan timeout, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Clusters.Cluster.SelectServer(IServerSelector selector, CancellationToken cancellationToken)
at MongoDB.Driver.MongoClient.AreSessionsSupportedAfterServerSelection(CancellationToken cancellationToken)
at MongoDB.Driver.MongoClient.AreSessionsSupported(CancellationToken cancellationToken)
at MongoDB.Driver.MongoClient.StartImplicitSession(CancellationToken cancellationToken)
at MongoDB.Driver.OperationExecutor.StartImplicitSession(CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSession[TResult](Func`2 func, CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionImpl`1.Distinct[TField](FieldDefinition`2 field, FilterDefinition`1 filter, DistinctOptions options, CancellationToken cancellationToken)
at MongoDB.Driver.IMongoCollectionExtensions.Distinct[TDocument,TField](IMongoCollection`1 collection, Expression`1 field, FilterDefinition`1 filter, DistinctOptions options, CancellationToken cancellationToken)



 Comments   
Comment by PM Bot [ 17/Nov/21 ]

There hasn't been any recent activity on this ticket, so we're resolving it. Thanks for reaching out! Please feel free to comment on this if you're able to provide more information.

Comment by James Kovacs [ 02/Nov/21 ]

Hi, Pavel,

Thank you for explaining the issue in more detail. Based on your description this is an issue with connecting from AKS to MongoDB Atlas and is not an issue specific to the .NET/C# driver.

If you are able to connect from other drivers such as our pymongo or Node.js drivers, but not from .NET/C#, that would be an indication of a .NET/C#-specific problem. Note that the legacy mongo shell does not behave exactly as our drivers do in all situations - especially more complex situations such as AKS to Atlas. Our new mongosh shell is built on top of our Node.js driver and is much more representative of how drivers should connect to MongoDB.

I recommend working with our Atlas Support Team and/or community resources to troubleshoot further. If it is determined that it is a problem specific to the .NET/C# driver, then the .NET/C# team would be able to investigate further with you.

Sincerely,
James

Comment by Pavel Shapurau [ 02/Nov/21 ]

Hi, James

Thanks for your reply.
I have an AKS node based on the ubuntu 18 image, pods is also based on the ubuntu 18 image. I tried to connect from the command line using the mongo command line client.
From my application to the .Net, I can connect to any mongo database at any time. But the Mongo Atlas is not always connected to Mongo database. I conducted an experiment like this:
I create a new node pool in the AKS cluster and run a container in this node pool - there was a connection to mongo atlas from the application. After that, I changed the node resources in the node pool (I added RAM and CPU) and the connection to the mongo atlas stopped working from my application, in the meantime the application was connected to any other mongo databases at that moment and there is always a connection from the command line, like to mongo atlas and to any other mongo databases.
I also tried the following actions: I changed the size of my node back to the original resources in the AKS cluster, but the connection from the application to the mongo atlas did not work either. Then I tried the "re-image" option for my node - and it worked! The connection began to work for Mongo Atlas from the application. Thus, by changing the size of the node in the node pool and then clicking the "re-image" option each time, the application connects from this node to the monga atlas. It's good! But, what happens when the image node is resized and how does this affect the connection of the application from the pod??? And why exactly does not a connection occur to the Mongo atlas after that, while the connection to any other Mongo works correctly? I would like to know the answers to the questions. Because it is not an option every time after changing the size of a node in the AKS cluster, to press the option with hands to "recreate the image" only so that the applications from the pods are connected to the Mongo Atlas. Also, you can do this experiment at home, or we can make a call and I will demonstrate this problem online!

Comment by James Kovacs [ 01/Nov/21 ]

Hi, Pavel,

Thank you for reaching out to us about your connection issue between AKS pods and MongoDB Atlas.

Based on the stack traces provided, this appears to be an issue with connecting to your MongoDB Atlas replica set from AKS rather than an issue specifically with the MongoDB .NET/C# driver. You mention that you can connect from AKS pods to your MongoDB Atlas replica set using the CLI. Do you mean the old mongo command line client or the newer mongosh command line client? The new mongosh client is built on top of our Node.js driver and is a more accurate reflection of how drivers connect to the cluster.

Next steps:
1. Attempt to connect from AKS pod to MongoDB Atlas using mongosh using the mongodb+srv:// connection string provided in the Atlas UI.
2. Attempt to connect from AKS pod to MongoDB Atlas using another driver such as pymongo using the mongodb+srv:// connection string provided in the Atlas UI.

If those fail to connect as well, we suggest troubleshooting further with our MongoDB Atlas Support Team available via "Get Help" in the Atlas UI. If you can connect via both of those methods, then this is more indicative of a problem specific to the .NET/C# driver and we will provide additional troubleshooting steps.

Sincerely,
James

Comment by Pavel Shapurau [ 01/Nov/21 ]

Also, the following situation may appear in the log:
Connection id "0HMCT3BEQJ1BS", Request id "0HMCT3BEQJ1BS:00000002": An unhandled exception was thrown by the application.
System.TimeoutException: A timeout occurred after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector

{ AllowedLatencyRange = 00:00:00.0150000 }

, OperationsCountServerSelector }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "ReplicaSet", Type : "ReplicaSet", State : "Disconnected", Servers : [] }.
at MongoDB.Driver.Core.Clusters.Cluster.ThrowTimeoutException(IServerSelector selector, ClusterDescription description)
at MongoDB.Driver.Core.Clusters.Cluster.WaitForDescriptionChangedHelper.HandleCompletedTask(Task completedTask)
at MongoDB.Driver.Core.Clusters.Cluster.WaitForDescriptionChangedAsync(IServerSelector selector, ClusterDescription description, Task descriptionChangedTask, TimeSpan timeout, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Clusters.Cluster.SelectServerAsync(IServerSelector selector, CancellationToken cancellationToken)
at MongoDB.Driver.MongoClient.AreSessionsSupportedAfterServerSelectionAsync(CancellationToken cancellationToken)
at MongoDB.Driver.MongoClient.AreSessionsSupportedAsync(CancellationToken cancellationToken)
at MongoDB.Driver.MongoClient.StartImplicitSessionAsync(CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSessionAsync[TResult](Func`2 funcAsync, CancellationToken cancellationToken)

Generated at Wed Feb 07 21:46:44 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.