I’m trying to follow this guide: https://minikube.sigs.k8s.io/docs/handbook/host-access/
But host.minikube.internal
from my pod solve to 127.0.0.1
that is obviously not the ip that I need to reach a service hosted on host. My minikube version is 1.32.0
I need this for development only. I’m also using Tilt.
You are right, in minikube versions above 1.10, host.minikube.internal within a pod resolves to 127.0.0.1 (local host) and won’t reach your host machine. Here are alternative approaches for development purposes:
Nodeport Service is simple but Might require Firewall Rules:
Use kubectl expose deploy < your -deployment-name> - - -type=Nodeport- - -port=3333- - - targetport=80
(replace with your ports)- Use
kubectl get svc
to find the allocated Node port - Access the service from your pod using the minikube VMs IP address and the Nodeport.
<VM_IP> : 3000
(replace with the actual VM Ip address)
NOTE:
you might need to adjust firewall rules on the Minikube VM to allow traffic on the Nodeport.
Service Account with Host Network Access more secure but require pod spec modification:
- Create a Service Account with host network access:
- Kubectl creates a service account host-access
- Kubectl create cluster role binding host-access-binding–cluster role system:host—service account=default:host -access
Modify your pod spec to use the service Account:
YAML:
Apiversion:v1
Kind:pod
metadata:
name: my-pod
Spec:
Service AccountName:
host-access
- Deploy the pod with the modified spec.
NOTE:
This approach avoids exposing ports to the wider network like with Nodeport and is considered more secure for development environments.
Minikube tunnel simplest but limited scope:
Run minikube tunnel This creates a tunnel between your local machine and the Minikube VM,allowing you to access services running on the VM using localhost:
NOTE:
This approach only works on your local development machine and won’t be accessible from other machines on your network.
Choose the approach that best suits your needs based on simplicity, security, and accessibility requirements
Consider using Tilt’s environment variables feature to inject the VM IP address into your pods if using the Nodeport service.
Refer to the Minikube documentation for more details on service types and host network access
By following these approaches, you should access your host machine’s service from your pod in Minikube for development purposes.
EDIT-1
Why host.minikube.internal points to local host
The document for Minikube versions above 1.10 states that host.minikube internally should resolve to the minikube VM’s IP address within a pod, allowing you to reach services running on the host machine.
However, in your case version 1.32.0, it’s resolving to 127.0.0.1 local host within the pod, which won’t work for accessing the host.There might be a bug or configuration issue specific to Minikube version 1.32.0
Alternative solutions:
Minikube dns –proxy flag : (Recommended )
Starting Minikube with the – – – dns -proxy flag can potentially fix the issue with host.minikube.internal in your version
Run minikube start- -dns-proxy or add this flag to your existing minikube configuration
If successful, host.minikube.internal should resolve to the Minikube VM’s IP address within your pod, allowing you to access services running on the host machine.
Minikube VM IP Address:
If the – – – dns -proxy flag doesn’t work you can directly use the Minikube VM’s IP address to access services on the host machine from the pod
Use a Minikube VM’s IP address to access services on the host machine from your pod
To find VM IP Address run Minikube ip
to get the IP Address of the Minikube VM.
Access the service using the Ip address and port <VM_IP>:<service port>
(replace with actual values)
In case you are using Tilt, there is a possibility of injecting an environment variable such as Minikube VM IP address into pods via Tilt’s environment variables feature. This makes it easy for users since no amendment on pod specs is necessary if they want to reach their local services.
Through either –dns-proxy flag, or directly applying IP address of the Kubernetes node, accessing all these resources is allowed.
10