4

I am running minikube on my Mac laptop. I am using VirtualBox to host the minikube virtual machine, following the official instructions.

I would like a pod that I am going to deploy into the cluster to be able to ping a server I will be running on my laptop. Assuming (for now) that I am not defining a Kubernetes Service of type ExternalName to represent that server, what IP or hostname should I use from within the program running in my pod?

EDIT: From my pod I can ping 10.0.2.2 and get answers back. However, trying to telnet to 10.0.2.2 on port 9092, where I happen to have an H2 database running, just hangs.

Finally, minikube ssh, which apparently logs me into the VirtualBox VM, maybe? running as docker? results in all the same behaviors above, in case this matters, which suggests this is fundamentally a question, I suppose, about VirtualBox.

EDIT #2: A restart of VirtualBox solved the connection issue (!). Nevertheless, 10.0.2.2 still seems like magic to me; I'd like to know where that IP comes from.

Laird Nelson
  • 15,321
  • 19
  • 73
  • 127

1 Answers1

4

You should be able to use the ipv4 address listed under vboxnet0. Look for the vboxnet0 interface in the list of outputs for ifconfig. Alternatively the address 10.0.2.2 will also map back to the host from the guest.

This IP address will be accessible from within the guest but not directly from within a pod. To make it accessible from within a pod you will need to create a headless service that exposes this IP address.
See this answer for how to create a headless service: Minikube expose MySQL running on localhost as service

So for example I ran a server at port :8000 on my host and did the following to access it in a pod:

$ kubectl create -f service.yaml

----service.yaml----
apiVersion: v1 kind: Service metadata:
    name: my-service spec:
    ports:
        - protocol: TCP
          port: 1443
          targetPort: 8000

$ kubectl create -f endpoint.yaml

----endpoint.yaml----
apiVersion: v1
kind: Endpoints
metadata:
    name: my-service
subsets:
    - addresses:
        - ip: 192.168.99.1 #ipv4 address from vboxnet0
      ports:
        - port: 8000

$ kubectl get svc
NAME         CLUSTER-IP   EXTERNAL-IP   PORT(S)    AGE
kubernetes   10.0.0.1     <none>        443/TCP    2h
my-service   10.0.0.83    <none>        1443/TCP   17m

Then you can access the host service by using 10.0.0.83:1443 within a pod.

Community
  • 1
  • 1
aaron-prindle
  • 3,077
  • 1
  • 17
  • 15
  • Thank you for your answer. Would I create a headless Service, which requires me to create an Endpoint, or a Service of type ExternalName, which I don't think does require me to create an Endpoint? – Laird Nelson Apr 20 '17 at 21:41
  • 1
    I just got this to work accessing a server on my host using a headless service in minikube w/virtualbox using yaml files nearly identical to those linked and the ip @ vboxnet0. I think you could also use ExternalName but I think you will need to add a DNS entry into your cluster to map to the IP. I am less familliar with ExternalName unfortunately. – aaron-prindle Apr 20 '17 at 22:15
  • Interestingly, with no other magic on my part, `ping 10.0.2.2` from within a shell prompt set up by `kubectl run -i -t busybox --image=busybox --restart=Never` gets an answer. Where can I read more about this magic (to me!) `10.0.2.2` address? – Laird Nelson Apr 20 '17 at 22:19
  • I don't really know too much more about it other than virtualbox always maps this back to the host – aaron-prindle Apr 20 '17 at 22:23