<p>I have a Spring Boot application that I'm trying to test in Minkube, which should connect to MongoDb Atlas. </p>

<p>It connects to my database when running the application on my pc, it also connects after creating and running as a Docker image, running on a Ubuntu VM on my PC. It does not connect to Mongodb Atlas when running as a Kubernetes deployment on Minikube. </p>

<p>There must be a configuration step im missing, but i just dont know what. I think my question is "Do i need to do anything special to allow Minikube/Kubernetes to find external URLs"?</p>

<p>I will post my errors and current routes of investigation bellow, if anyone has any suggestions they would be greatly appreciated.</p>

<hr>

<p>Nested error (its a long error message so just the relevant bit - i think) when running in Kubernetes, as you can see its unable to find my database URL, and complains of "DNS name not found". Can replicate this by running the program locally in Intelij and mangling the URL a bit</p>

<pre><code>Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mongodb.client.MongoClient]: Factory method 'mongo' threw exception; nested exception is com.mongodb.MongoConfigurationException: Unable to look up TXT record for host xxxxxx-erble.gcp.mongodb.net
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
        at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
        ... 128 common frames omitted
Caused by: com.mongodb.MongoConfigurationException: Unable to look up TXT record for host xxxxxx-erble.gcp.mongodb.net
        at com.mongodb.internal.dns.DefaultDnsResolver.resolveAdditionalQueryParametersFromTxtRecords(DefaultDnsResolver.java:131) ~[mongodb-driver-core-4.0.1.jar:na]
        at com.mongodb.ConnectionString.&lt;init&gt;(ConnectionString.java:378) ~[mongodb-driver-core-4.0.1.jar:na]
        at org.springframework.boot.autoconfigure.mongo.MongoClientFactorySupport.applyHostAndPort(MongoClientFactorySupport.java:101) ~[spring-boot-autoconfigure-2.3.0.M4.jar:2.3.0.M4]
        at org.springframework.boot.autoconfigure.mongo.MongoClientFactorySupport.computeClientSettings(MongoClientFactorySupport.java:69) ~[spring-boot-autoconfigure-2.3.0.M4.jar:2.3.0.M4]
        at org.springframework.boot.autoconfigure.mongo.MongoClientFactorySupport.createMongoClient(MongoClientFactorySupport.java:60) ~[spring-boot-autoconfigure-2.3.0.M4.jar:2.3.0.M4]
        at org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration.mongo(MongoAutoConfiguration.java:57) ~[spring-boot-autoconfigure-2.3.0.M4.jar:2.3.0.M4]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
        at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
        ... 129 common frames omitted
Caused by: javax.naming.NameNotFoundException: DNS name not found [response code 3]
        at jdk.naming.dns/com.sun.jndi.dns.DnsClient.checkResponseCode(DnsClient.java:664) ~[jdk.naming.dns:na]
        at jdk.naming.dns/com.sun.jndi.dns.DnsClient.isMatchResponse(DnsClient.java:582) ~[jdk.naming.dns:na]
        at jdk.naming.dns/com.sun.jndi.dns.DnsClient.doUdpQuery(DnsClient.java:430) ~[jdk.naming.dns:na]
        at jdk.naming.dns/com.sun.jndi.dns.DnsClient.query(DnsClient.java:214) ~[jdk.naming.dns:na]
        at jdk.naming.dns/com.sun.jndi.dns.Resolver.query(Resolver.java:81) ~[jdk.naming.dns:na]
        at jdk.naming.dns/com.sun.jndi.dns.DnsContext.c_getAttributes(DnsContext.java:434) ~[jdk.naming.dns:na]
        at java.naming/com.sun.jndi.toolkit.ctx.ComponentDirContext.p_getAttributes(ComponentDirContext.java:235) ~[na:na]
        at java.naming/com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:141) ~[na:na]
        at java.naming/com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:129) ~[na:na]
        at java.naming/javax.naming.directory.InitialDirContext.getAttributes(InitialDirContext.java:171) ~[na:na]
        at com.mongodb.internal.dns.DefaultDnsResolver.resolveAdditionalQueryParametersFromTxtRecords(DefaultDnsResolver.java:114) ~[mongodb-driver-core-4.0.1.jar:na]
        ... 139 common frames omitted
</code></pre>

<hr>

<p>Things i have tried:</p>

<ul>
<li>In my deployment.yaml: setting dnsPolicy to Default (plus all other possible values)</li>
<li>Creating an external service, and using the externalName in place of my URL</li>
</ul>

<pre><code>kind: Service
apiVersion: v1
metadata:
  name: mongodbatlas
spec:
  type: ExternalName
  externalName: xxxxx-erble.gcp.mongodb.net
</code></pre>

<ul>
<li>Creating an allow-all egress Network Policy:</li>
</ul>

<pre><code>apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-all-egress
spec:
  podSelector: {}
  egress:
    - {}
  policyTypes:
    - Egress
</code></pre>

<hr>

<p>Below is my deployment and service YAML, with personal username/password/repository details removed:</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend
  labels:
    app: frontend
spec:
  replicas: 1
  selector:
    matchLabels:
      app: frontend
  template:
    metadata:
      labels:
        app: frontend
    spec:
      dnsPolicy: Default
      containers:
        - name: frontend
          image: &lt;DOCKERRPO&gt;/&lt;REPO&gt;:front
          imagePullPolicy: Always
          env:
            - name: MONGODB_ATLAS
              value: mongodb+srv://&lt;USERNAME&gt;:&lt;PASSWORD&gt;@xxxxxx-erble.gcp.mongodb.net/test?retryWrites=true&amp;w=majority
            - name: FRONT_SERVER_PORT
              value: "8080"
            - name: spring_profiles_active
              value: dev
          ports:
            - containerPort: 8080
          resources:
            limits:
              memory: 512Mi
      imagePullSecrets:
        - name: &lt;SECRET&gt;
---
apiVersion: v1
kind: Service
metadata:
  name: frontend
spec:
  ports:
    - nodePort: 32000
      port: 80
      targetPort: 8080
  selector:
    app: frontend
  type: NodePort
</code></pre>

Alexander Lock-Achilleos
<p>Have found a solution:</p>

<p>For some reason, one of the components of my setup (spring boot/kubernetes/minikube/virtualbox etc.) has an issue resolving DNS SRV URIs. The MongoDB Atlas URI provided for connecting to my cluster fails, changing this to the depreciated style of URI seems to allow my service to connect. </p>

<p><a href="https://stackoverflow.com/questions/52728251/gae-unable-to-look-up-srv-record-for-mongodb-atlas-instance">GAE unable to look up SRV record for mongodb atlas instance</a></p>
