<p>I want to deploy an ASP.NET application with MongoDB on Kubernetes locally. Currently the application is working fine with the <code>docker-compose</code>.</p>
<p>In the case of Kubernetes YAML file, I have created a <code>StatefulSet</code> for the MongoDB and a Deployment for the ASP.NET app and also, I have created their respected Services and ConfigMap too. I have attached the complete code below.</p>
<p>Even I can see all the data are loaded into the Mongo database when I use the Mongo-Express Deployment. So, I am sure that the MongoDB StatefulSet is working fine. Now the only concern is the .NET App is throwing an exception called &quot;Resource temporarily unavailable&quot;.</p>
<p>About the issue: the build is working fine while performing docker-compose up. But in the case of Kubernetes cluster deployment its throwing this exception:</p>
<blockquote>
<p>fail:<br />
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]</p>
<p>An unhandled exception has occurred while executing the request.</p>
<p>System.TimeoutException: A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : &quot;1&quot;, ConnectionMode : &quot;Automatic&quot;, Type : &quot;Unknown&quot;, State : &quot;Disconnected&quot;, Servers : [{ ServerId: &quot;{ ClusterId : 1, EndPoint : &quot;Unspecified/mongo:27017&quot; }&quot;, EndPoint: &quot;Unspecified/mongo:27017&quot;, ReasonChanged: &quot;Heartbeat&quot;, State: &quot;Disconnected&quot;, ServerVersion: , TopologyVersion: , Type: &quot;Unknown&quot;, HeartbeatException: &quot;MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server.</p>
<p>System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (00000001, 11): Resource temporarily unavailable</p>
<p>at System.Net.Dns.InternalGetHostByName(String hostName)<br />
at System.Net.Dns.ResolveCallback(Object context)<br />
--- End of stack trace from previous location where exception was thrown ---<br />
at  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(Exception source)<br />
at System.Net.Dns.HostResolutionEndHelper(IAsyncResult asyncResult)<br />
at System.Net.Dns.EndGetHostAddresses(IAsyncResult asyncResult)<br />
at System.Net.Dns.&lt;&gt;c.b__25_1(IAsyncResult asyncResult)<br />
at System.Threading.Tasks.TaskFactory<code>1.FromAsyncCoreLogic(IAsyncResult iar, Func</code>2 endFunction, Action<code>1 endAction, Task</code>1 promise, Boolean requiresSynchronization)<br />
--- End of stack trace from previous location where exception was thrown ---<br />
at MongoDB.Driver.Core.Connections.TcpStreamFactory.ResolveEndPointsAsync(EndPoint initial)<br />
at MongoDB.Driver.Core.Connections.TcpStreamFactory.CreateStreamAsync(EndPoint endPoint, CancellationToken cancellationToken)<br />
at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)<br />
--- End of inner exception stack trace ---<br />
at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)<br />
at MongoDB.Driver.Core.Servers.ServerMonitor.InitializeConnectionAsync(CancellationToken cancellationToken)
at MongoDB.Driver.Core.Servers.ServerMonitor.HeartbeatAsync(CancellationToken cancellationToken)&quot;, LastHeartbeatTimestamp: &quot;2022-06-26T16:04:05.7393346Z&quot;, LastUpdateTimestamp: &quot;2022-06-26T16:04:05.7393356Z&quot; }] }.
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<code>1.UsingImplicitSession[TResult](Func</code>2 func, CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionImpl<code>1.FindSync[TProjection](FilterDefinition</code>1 filter, FindOptions<code>2 options, CancellationToken cancellationToken) at MongoDB.Driver.FindFluent</code>2.ToCursor(CancellationToken cancellationToken)
at MongoDB.Driver.IAsyncCursorSourceExtensions.FirstOrDefault[TDocument](IAsyncCursorSource`1 source, CancellationToken cancellationToken)</p>
</blockquote>


<p>Pretty sure you need the change the name of mongo db connectionstring. Check out the DNS for services section: <a href="https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/</a></p>
<p>In your case it would be <code>mongodb-service.default.svc.cluster.local</code> if you've deployed everything into default namespace.</p>
