I am learning about Kubernetes am trying move from docker-compose to use Kubernetes but having problem with connecting to MongoDB running on localhost.
I don\'t have an
How to connect from Host to Pod in Minikube
If the issue is that you can't access your Pods (via the service
) on Minikube.
Let's check your Service
first.
$ kubectl get service eureka
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
eureka LoadBalancer 10.104.196.32 <pending> 8080:30167/TCP 25m
Note that, when running Minikube, a LoadBalancer
service won’t acquire an external IP address.
Locally, Minikube doesn’t integrate with another service to run external load balancers.
But in most other environments, when you use a Kubernetes LoadBalancer
, it will provision a load balancer external to your cluster, for example an Elastic Load Balancer (ELB) in AWS, or a Cloud Load Balancer in GKE.
Because Minikube doesn’t integrate with a separate local load balancer, you have to ask Minikube to simulate the connection using minikube service:
$ minikube service eureka
That will provide you with the Minikube’s IP address and the port number Minikube routes to 'eureka', so and you can copy and paste that into a browser/other app to test the app.
Additional info can be found in Minikube's documentation:
To access the hello-minikube Deployment, expose it as a Service:
kubectl expose deployment hello-minikube --type=NodePort --port=8080
Get the URL of the exposed Service to view the Service details:
minikube service hello-minikube --url
EDIT
How to connect from Pod to localhost
on Minikube.Host
don't have any problems connecting using following docker-compose.yaml by using 'network_mode: "host"'
True.
If you use the host
network mode for a container, that container’s network stack is not isolated from the Docker host (the container shares the host’s networking namespace), and the container does not get its own IP-address allocated. So if you run a container which binds to port 80 and you use host
networking, the container’s application is available on port 80 on the host’s IP address.
Since minikube runs everything from within a VM, networking can get fairly complicated
Once Minikube is running and it’s network has been established, you should be able to visit your localhost server on both 127.0.0.1
and 192.168.99.1
(If not, subsequent steps will not work). If successful, this means that the Minikube VM can access your host machine’s localhost on 192.168.99.1
(127.0.0.1
from Minikube would still be a Minicube's localhost
)
That is why in this very case instead of localhost
your app shall connect to 192.168.99.1
.
kind: Service apiVersion: v1 metadata: name: mongo spec: type: ExternalName externalName: host.docker.internal
Please make sure that host.docker.internal
is resolved to the IP address of Host machine.
Additionally you can edit /etc/hosts
of Minikube and add any domain name for that IP, so you'll be able connecting to it.
Hope that explains how to access your app from outside and access specifically "localhost
" when using Minikube.