1

I've created a service inside minikube (expressjs API) running on my local machine, so when I launch the service using minikube service wedeliverapi --url i can access it from my browser with localhost:port/api

minikube IP : 192.168.49.2

enter image description here

enter image description here

But I also want to access that service from another device so I can use my API from a flutter mobile application, how can I achieve this goal?

2
  • Could you please replace the screenshots with text? Oct 30 at 7:48
  • which one? minikube service wedeliverapi --url output ?
    – Wrak Ali
    Nov 1 at 12:06
0

TLDR;

Look at NodePort or Ingress to expose a Kubernetes minikube service externally.

For minikube specifically, there is one last networking layer between the minikube VM and the host. You need to enable port forwarding from the Host into the VM. To forward the entire default minikube NodePort range:

for port in {30000..32767}; do VBoxManage controlvm minikube natpf1 "NodePort$port,tcp,,$port,,$port"; done

To disable the port forwarding when you're done:

for port in {30000..32767}; do VBoxManage controlvm minikube natpf1 delete "NodePort$port"; done

Long Answer

The default Service type is ClusterIP, which only exposes the Service inside the cluster via a cluster-internal IP.

To expose a Service outside the minikube cluster, there are a few options provided by Kubernetes.

First, we can look at ServiceTypes:

  1. NodePort - Exposes a service on each cluster node at a specific port. Cluster networking routes the packets to the target Service's Pods. This is the most basic way to expose a service externally. It has existed for many years in Kubernetes. This is probably the simplest way to expose a Service for testing in minikube. However, if you want to do anything fancy such as TLS/SSL termination, load balacning, virtual hosts, or routing rules... use an Ingress.
  2. LoadBalancer - Exposes a service using a cloud-native Load Balancer. This is for production clusters, and probably not useful for your minikube use case. It usually creates a dynamic NodePort for use as the cloud Load Balancer's backend target port.
  3. ExternalName - A way to use externally hosted services through a cluster DNS CNAME record. Maps the service name directly to an outside DNS address, and bypasses the need for endpoint records. Although it's good to know this exists, this is not very useful for your use case of exposing a cluster service externally.

Next, we can look at port forwarding:

  • kubectl port-forward command is a way to forward local traffic into a Pod within a Kubernetes cluster. This feature is built into kubectl, and is mostly useful for local development and one-off tasks where you need to access a Pod directly. This works with Pod names, Deployments, and ReplicaSets.

     kubectl port-forward podname-nnnnn-xxxx $LOCALPORT:$CONTAINERPORT
    

This is probably the simplest method for accessing a Pod providing a Service quickly in order to test that it's working properly and accepting traffic.

Finally, we can look at Ingress and Ingress Controllers (default: nginx):

  • Ingress: An Ingress may be configured to give Services externally-reachable URLs, load balance traffic, terminate SSL / TLS, and offer name-based virtual hosting. An Ingress Controller usually runs inside a Pod in the kube-system namespace and provides these features which are configurable through the Ingress Kubernetes API object.

For minikube, you need to enable it as an addon:

minikube addons enable ingress

Follow the minikube Ingress tutorial here to get started using ingress-nginx.

2
  • I really appreciate your answer but can ingress-nginx help me to expose the service outside my machine for all devices connected to the network?
    – Wrak Ali
    Nov 2 at 9:12
  • Specifically for minikube LAN access, there is one more networking layer to be aware of: LAN --> Host --> minikube VM. For this, you're probably using VirtualBox and can enable the port forwarding NAT feature to route the LAN traffic to the minikube VM. (See edited answer above for commands)
    – TrinitronX
    Nov 3 at 11:04
0

One easy way is to have proxy for outside your machine, I can not comment due to low reputation. You can use apache or any other like HA, nginx

3
  • sorry, brother for the late reply but can you give me more details on how to setup a reverse proxy using Nginx ? like should I have to install it on my windows machine or in minikube ?
    – Wrak Ali
    Nov 1 at 10:52
  • on your Windows machine Nov 1 at 19:54
  • i alrady installed nginx and used this =====> ` server { listen 80; server_name localhost; location / { proxy_pass localhost:3000/api; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }` in my nginx.conf but it not redirect !!
    – Wrak Ali
    Nov 2 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.