DNS resolution of ExternalName in Minikube on Mac

久未见 提交于 2019-12-08 04:32:35

问题


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.

I'm running postgres as a container on docker

> 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->5432/tcp   postgres

On my Mac / VirtualBox / Minikube setup I create a service

kind: Service
apiVersion: v1
metadata:
  name: postgres-svc
spec:
  type: ExternalName
  externalName: 10.0.2.2
  ports:
    - port: 5432

10.0.2.2 is alias to host interface (found this information here)

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

(our namespace is hazelnut, don't ask:-)

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.

I've tried postgres-svc.hazelnut.cluster.local, postgres-svc, postgres-svc.hazelnut.svc.cluster.local, postgres-svc.hazelnut all resulting in NXDOMAIN

kubernetes.default works though.

> 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

In this post they mention that using kube-dns should solve it, but I'm using it and to no avail

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

Any idea how I can get this to work properly?


回答1:


For the ExternalName service type the externalName should be FQDN, not an IP address, e.g.

kind: Service
...
metadata:
  name: postgres-svc
spec:
  type: ExternalName
  externalName: mydb.mytestdomain

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

10.0.0.2    mydb.mytestdomain

Actually, coredns uses name resolver configured in the /etc/resolv.conf 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 /etc/hosts file.

Tested for: MacOS 10.14.3, VBox 6.0.10, kubernetes 1.15.0, minikube 1.2.0, coredns



来源:https://stackoverflow.com/questions/57250240/dns-resolution-of-externalname-in-minikube-on-mac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!