<p>I'm trying to connect to a postgres container running in docker on my mac, from my minikube setup in virtualbox. But I'm running into dns resolve issues.</p>

<p>I'm running postgres as a container on docker</p>

<pre><code>&gt; docker ps                                                                                                                                                                                                                                                                                   
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
a794aca3a6dc        postgres            "docker-entrypoint.s…"   3 days ago          Up 3 days           0.0.0.0:5432-&gt;5432/tcp   postgres
</code></pre>

<p>On my Mac / VirtualBox / Minikube setup I create a service</p>

<pre><code>kind: Service
apiVersion: v1
metadata:
  name: postgres-svc
spec:
  type: ExternalName
  externalName: 10.0.2.2
  ports:
    - port: 5432
</code></pre>

<p><code>10.0.2.2</code> is alias to host interface (found this information <a href="https://stackoverflow.com/questions/9808560/why-do-we-use-10-0-2-2-to-connect-to-local-web-server-instead-of-using-computer/34732276#34732276">here</a>)</p>

<pre><code>&gt; kubectl get service --all-namespaces
NAMESPACE     NAME                                             TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)                  AGE
default       kubernetes                                       ClusterIP      10.96.0.1        &lt;none&gt;        443/TCP                  21d
hazelnut      postgres-svc                                     ExternalName   &lt;none&gt;           10.0.2.2      5432/TCP                 27m
kube-system   kube-dns                                         ClusterIP      10.96.0.10       &lt;none&gt;        53/UDP,53/TCP,9153/TCP   21d
kube-system   kubernetes-dashboard                             ClusterIP      10.108.181.235   &lt;none&gt;        80/TCP                   19d
kube-system   tiller-deploy                                    ClusterIP      10.101.218.56    &lt;none&gt;        44134/TCP                20d
</code></pre>

<p>(our namespace is <code>hazelnut</code>, don't ask:-)</p>

<p>In my deployment, if I connect to 10.0.2.2 directly, it connects to the postgres without issue, but if I try to resolve the hostname of the kubernetes service it doesnt' work.
So it's not a firewall or routing issue, pure dns.</p>

<p>I've tried <code>postgres-svc.hazelnut.cluster.local</code>,
<code>postgres-svc</code>, <code>postgres-svc.hazelnut.svc.cluster.local</code>, <code>postgres-svc.hazelnut</code> all resulting in NXDOMAIN</p>

<p><code>kubernetes.default</code> works though.</p>

<pre><code>&gt; nslookup kubernetes.default
Server:     10.96.0.10
Address:    10.96.0.10#53

Name:   kubernetes.default.svc.cluster.local
Address: 10.96.0.1
</code></pre>

<p>In this <a href="https://stackoverflow.com/questions/52356455/kubernetes-externalname-service-not-visible-in-dns">post</a> they mention that using kube-dns should solve it, but I'm using it and to no avail</p>

<pre><code>&gt; kubectl get svc --namespace=kube-system
NAME                   TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                  AGE
kube-dns               ClusterIP   10.96.0.10       &lt;none&gt;        53/UDP,53/TCP,9153/TCP   21d
...
</code></pre>

<p>Any idea how I can get this to work properly?</p>

<p>For the <a href="https://kubernetes.io/docs/concepts/services-networking/service/#externalname" rel="nofollow noreferrer">ExternalName service type</a> the <code>externalName</code> should be FQDN, not an IP address, e.g. </p>

<pre><code>kind: Service
...
metadata:
  name: postgres-svc
spec:
  type: ExternalName
  externalName: mydb.mytestdomain
</code></pre>

<p>The host machine should be able to resolve the name of that FQDN. You might add a record into the <code>/etc/hosts</code> at the Mac host to achieve that: </p>

<pre><code>10.0.0.2    mydb.mytestdomain
</code></pre>

<p>Actually, coredns uses name resolver configured in the <code>/etc/resolv.conf</code> in the Minikube VM. It points to the name resolver in the VirtualBox NAT Network (10.0.2.3). In turn, VirtualBox relies on the host name resolving mechanism that looks through the local <code>/etc/hosts</code> file. </p>

<p>Tested for: 
MacOS 10.14.3, 
VBox 6.0.10, 
kubernetes 1.15.0, 
minikube 1.2.0, 
coredns</p>
