How to access a service deployed as NodePort in Minikube in EC2 from outside

9/28/2021

I have installed minikube in EC2 Linux Instance.

Minikube IP : 192.168.49.2, 
EC2 Instance IP: 10.X.X.X

I've deployed an application as NodePort service type where

port: 80
targetPort: 80
nodePort: 32768
and able to access it with curl http://192.168.49.2:32768 within the cluster.

How should I expose this application to the outside world?
If port forwarding is the option then I tried the below ways:

kubectl port-forward --address <InstanceIP> svc/my-service 8888:80

This always gives error:

Unable to listen on port 5000: 
Listeners failed to create with the following errors: 
[unable to create listener] error: 
unable to listen on any of the requested ports: [{8888 80}]

I made sure there is no service running on port 8888.

kubectl port-forward --address <InstanceIP> svc/my-service 8888:32768

This says, error:

Service my-service does not have a service port 32768.

Any help is appreciated

-- Sri Durga
kubernetes
kubernetes-helm
kubernetes-ingress
minikube

1 Answer

9/28/2021

How should I expose this application to the outside world?

You should use a Service with a type: LoadBalancer

LoadBalancer will give you an External Ip that will allow you to connect to it from outside the cluster.

If you only need it for testing and this is why you are using minikube then port forward should cover the requirements.

Using LoadBalancer with minikube

In order to use LoadBalanacer with minikube you should open a new terminal and execute minikube tunnel

# Port forward to the desired service, 
# dont forget to add the namespace if any

#
# You should forward to port 80 which is the port defined in your service
#     port : 80
#     targetPort : 80
kubectl port-forward svc/my-service 8888:80 -n <namespace>

# As you mentioned you tried it already and it's not working
# so follow below and try to expose your service with

kubectl expose

Now you should be able to connect to your service.


Here is a full example which generates Service

https://github.com/nirgeier/KubernetesLabs/tree/master/Labs/02-Deployments-Imperative

The sample generates the Service on the fly with

kubectl expose deployment -n codewizard multitool --port 80 --type NodePort
-- CodeWizard
Source: StackOverflow