1

I installed minikube on my mac and created deployment and a service for my nodejs app. I tested that everything is working by getting the URL of my service using the following command:

minikube service my-nodejs-app --url

and then I run this URL in the browser and got results. The problem is when i tried to access the same URL from another machine inside the same network it didn't worked.

my service.yml file is:

apiVersion: v1
kind: Service
metadata:
  name: my-nodejs-app
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 1337 
    protocol: TCP
    name: app-server
  selector:
    app:  my-nodejs-app  

I tried to use port forwarding to forward my pod port to my localhost and it works only on the same machine who host the cluster and when I try to access from another machine on the same network (via the IP address of the machine where the cluster deployed) I still get page not found.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Ran Hassid
  • 2,788
  • 1
  • 12
  • 20

5 Answers5

5

You can use "port forward a service". Assuming:

  • Your local machine IP: 166.6.6.6 (which hold minikube)
  • Your minikube IP: 192.168.99.100 (check the real IP with command $minikube ip)
  • The nodePort of your service 'my-nodejs-app': 31000 (check the real nodePort with command: $kubectl get service)

In order to access your service from remote, you can forward a port (like 31000, recommend the same port with nodePort) to your service through the following command in your local machine:

ssh -i ~/.minikube/machines/minikube/id_rsa docker@$(minikube ip) -L \*:31000:0.0.0.0:31000

Then you can access your service through URL: http://166.6.6.6:31000, which will be forwarded to your service URL http://192.168.99.100:31000

Thx: https://github.com/kubernetes/minikube/issues/877

Jingchao Luan
  • 411
  • 4
  • 10
2

Probably a bit late, but if anyone still having this issue-

  1. Check the list of services and the one you want to expose if it is present
kubectl get svc -n {namespace_name}
  1. Change the type to NodePort if it is of cluster IP type.
kubectl patch svc {service_name} -n {namespace_name} --type='json' -p '[{"op":"replace","path":"/spec/type","value":"NodePort"}]'
  1. Expose the above Node Port available to your local machine now for other machines on same network:
service_port=$(minikube service {service_name} -n {namespace_name} --url | cut -d':' -f3)
ssh -i ~/.minikube/machines/minikube/id_rsa docker@$(minikube ip) -NL \*:${service_port}:0.0.0.0:${service_port}

Now you can access the above service from other machines on same network by just hitting the link-
{your_hostname}:{node_port}

0

Sounds like reaching it from another machine compares to exposing a ssevice to the web. In that case you need to look into spec/type:LoadBalancer (http://kubernetes.io/docs/user-guide/load-balancer/) That said, with minikube i'd stick to a single machine and development only tests

Ben
  • 5,030
  • 6
  • 53
  • 94
  • Thanks, but i am developing a mobile app and when i try to test the app on a real mobile device it is not possible because i cannot reach from my mobile device to the service. BTW - i find a way to do it by installing a reverse proxy (NGINX) that forward the request to my service but i wanted to know if there is a better way.. – Ran Hassid Dec 05 '16 at 08:02
  • Have you looked into setting a service with a loadbalancer type ? Because this is the better way ( the kubernetes way) and that's what i meant above – Ben Dec 05 '16 at 08:33
  • In minikube you cannot use LoadBalancer the type of the service must be NodePort – Ran Hassid Dec 05 '16 at 08:33
  • Allright my bad; i guess you're at it with the nginx proxy – Ben Dec 05 '16 at 08:35
0

If I understand your problem correctly:

Your machine's IP: 192.168.1.4 Your minikube IP: 192.168.99.100 Accessing your service from a browser on your machine: http://192.168.99.100:30080

Now, let's say you're on another machine, say192.168.1.5, and you want to access this service.

The problem is that you need to map your machine's port to minikube's 30080 because minikube is a VM running on your machine (which cannot be accessed from outside your machine).

So you can try: Virtualbox "port forward" from Guest to Host.

Another alternative is to forward a port from your localhost to a pod directly (not the k8s svc unfortunately) by using kubectl port-forward.

Community
  • 1
  • 1
iamnat
  • 4,056
  • 1
  • 23
  • 36
  • hi @iamnat, I tried kubectl port-forward (which seems to be the best solution) but its not working... I can try the "port forward" approach on the VirtualBox but don't you think it's better to install NGINX as reverse proxy? (this is my current working solution..) – Ran Hassid Dec 06 '16 at 12:48
0

You have not specified nodePort in ports. Add below configuration in port

nodePort: 30000

You can access your service at http://[IP address]:30000