1

I am running my minikube as docker image. I am trying to expose my service to outside world using Nodeport.

This is my yaml file.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: docker-hello-world
  labels:
    app: docker-hello-world
spec:
  selector:
    matchLabels:
      app: docker-hello-world
  replicas: 3
  template:
    metadata:
      labels:
        app: docker-hello-world
    spec:
      containers:
      - name: docker-hello-world
        image: scottsbaldwin/docker-hello-world:latest
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: docker-hello-world-svc
spec:
  selector:
    app: docker-hello-world
  ports:
    - port: 8088
      targetPort: 80
  type: NodePort

Searched a lot about nodePort that we require node ip to access the service. I am able to access my service using minikube service docker-hello-world-svc --url which gives me url to access the service http://127.0.0.1:52526 but here port number is different then the nodePort.

My service is successful running .

NAME                     TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
docker-hello-world-svc   NodePort    10.109.146.181   <none>        8088:30934/TCP   65m

i want to access my service from outside the cluster using Nodeport but my nodes does not have any external ip

kubectl get nodes -o wide                                                                                                                                             
NAME       STATUS   ROLES           AGE    VERSION   INTERNAL-IP    EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION      CONTAINER-RUNTIME
minikube   Ready    control-plane   5h9m   v1.24.3   192.168.49.2   <none>        Ubuntu 20.04.4 LTS   5.10.104-linuxkit   docker://20.10.17

already read out that i need ingress controller to access service but i wanna test it using nodePort .

Any work around so i can access my service using only nodePort running inside the minikube which is running as docker image?

Status of minikube does not show kubectl

>>minikube status                                                                                                                                                                             
minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured
Amit
  • 53
  • 13
  • Does this help? https://stackoverflow.com/questions/40767164/expose-port-in-minikube – Sakib Md Al Amin Aug 13 '22 at 16:49
  • saw that post, there most of the comments were related to either getting the minikube node ip and using nodeid:nodeport, but here i am using minikube docker image so i am getting external ip of running node.. i used. minikube service - - url which gives localhost:someport number.. also there were few comments that minikube does not provide to the External ip to Mac platform, – Amit Aug 13 '22 at 16:57
  • i had a few issues between mac and linux, but i think logically the minikube ip should route it to your nodeport ? can you telnet to the pod using cmd ? – invertedOwlCoding Aug 14 '22 at 19:53

2 Answers2

3

You don't need an external IP when using NodePort, you can use minikube ip to get the minikube node address, and then connect to the respective nodePort:

$ kubectl svc
NAME                     TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
docker-hello-world-svc   NodePort    10.108.66.235   <none>        8088:31550/TCP   2m15s

$ minikube ip
192.168.49.2

$ curl 192.168.49.2:31550
<h1>Hello webhook world from: docker-hello-world-cc79bf486-k4lm8</h1>


Another alternative is to use a LoadBalancer service, and use minikube tunnel to connect to it using the internal port:

$ kubectl  svc
NAME                     TYPE           CLUSTER-IP     EXTERNAL-IP    PORT(S)          AGE
docker-hello-world-svc   LoadBalancer   172.16.58.95   172.16.58.95   8088:32624/TCP   10s

$ curl 172.16.58.95:8088
<h1>Hello webhook world from: docker-hello-world-cc79bf486-dg5s9</h1>

Notes:

  • You will only get an EXTERNAL-IP after running minikube tunnel in a separate terminal (it's a foreground process)
  • I recommend that you run Minikube using a less common IP range for the services, preventing conflicts with other network routes:
minikube start --service-cluster-ip-range=172.16.0.0/16
Eduardo Baitello
  • 10,469
  • 7
  • 46
  • 74
  • I tried using minikube ip but it does not connect. curl: (28) Failed to connect to 192.168.49.2 port 30934 after 75015 ms: Operation timed out. minikube get its internal ip which is usable within the cluster node but to connect it to outside the cluster we need external ip – Amit Aug 14 '22 at 05:35
  • Kudos for you, this saves me today, couple of days fighting against minikube. Thanks! – MegaDrive68k Dec 13 '22 at 01:02
0

You can use port forwarding with kubeclt. kubectl port-forward allows using resource name, such as a pod name, to select a matching pod to port forward to.

kubectl port-forward svc/resource-name 4000:5000
Joel Wembo
  • 814
  • 6
  • 10