Kubernetes ExternalName, exposing 10.0.2.2

12/23/2018

I have created a mongo Service as follows:

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

When starting the cluster with the following command:

minikube start --vm-driver=virtualbox

I would expect the Virtualbox loopback address (10.0.2.2) to be mapped to the local Mongo DB instance that runs on my localhost machine.

However when logging in into a pod and trying to ping 10.0.2.2, I experience a 100% package loss.

Is there something I'm missing here?

-- Trace
kubernetes
minikube

1 Answer

12/23/2018

So if you are trying to expose MongoDB as an external service and basically map mongo-svc to 10.0.2.2, then

the first issue is that ExtrnalName must the fully qualified domain name of the actual service, as per Kubernetes doc: “ExternalName accepts an IPv4 address string, but as a DNS name comprised of digits, not as an IP address. ExternalNames that resemble IPv4 addresses are not resolved by CoreDNS or ingress-nginx..”

The second issue is that the whole point of external service is to abstract external services by creating ExternalName service so that pods can connect to the external service through mongo-svc.default.svc.cluster.local (use the default if your mongo-svc in a default namespace, which seems to be the case based on your YAML) and not use external service name and its location so that you can modify the service definition and point to another service if needed.

-- Alexz
Source: StackOverflow