Laird Nelson
Laird Nelson

Reputation: 16248

On a Mac, what IP will represent my laptop from within a minikube cluster?

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.

Upvotes: 4

Views: 2019

Answers (1)

aaron-prindle
aaron-prindle

Reputation: 3317

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.

Upvotes: 4

Related Questions