Kubernetes NodePort on Minikube not accessible from outside

354 Views Asked by At

my current service file:

spec:
  selector:
    name: some-pod
  type: NodePort
  ports:
  - port: 8200
    targetPort: 8200
    nodePort: 30082
  externalIPs:
    - 192.168.183.128

current deployment of a pod within the deployment:

template:
metadata:
  name: some-pod
  labels:
    name: some-pod
    spec:
      containers:
      - name: container
        image: someimage
        imagePullPolicy: IfNotPresent
        securityContext:
          capabilities:
            add: ["CHOWN", "IPC_LOCK"]
        terminationMessagePath: "/tmp/pod-log"
        ports:
          - containerPort: 8200

when I get the minikube url I get the following:

[root@kind vault]# minikube service some-service --url
http://192.168.49.2:30082

after googling and reading bunch of article I still could not figure out what was going on. Then I realized that the IP that minikube showed above was the minikube container's IP. This means that the "nodePort" defined in the service-yaml file was to the container not to the node itself.

I verified it too by the following:

I listed all the interface by "ip a"
my node's interface ens160: 192.168.183.128
container "br-6981f04e576e": ip address - 192.168.49.2

from the node I ran the port connectivity to the minikube container -- works as expected:

[root@kind vault]# nc -zvw10 192.168.49.2 30082
Ncat: Version 7.92 ( https://nmap.org/ncat )
Ncat: Connected to 192.168.49.2:30082.
Ncat: 0 bytes sent, 0 bytes received in 0.02 seconds.

I also tried putting the externalIPs in the service yaml file and that still did not work.
then I looked into how to map the minikube "Running" container's port to the node's port and tired bunch such as "docker commit" and still nothing that helped! so please help!!

PS: I did allow the port 30082 port on the node itself and tested connectivity btw.

2

There are 2 best solutions below

1
On

Minikube supports either load balancer or node port

As mentioned in the documentation:

Services of type NodePort can be exposed via the minikube service --url command. It must be run in a separate terminal window to keep the tunnel open. Ctrl-C in the terminal can be used to terminate the process at which time the network routes will be cleaned up.

  1. Create a Kubernetes deployment

    kubectl create deployment hello-minikube1 --image=kicbase/echo-server:1.0
    
  2. Create a Kubernetes service type NodePort

    kubectl expose deployment hello-minikube1 --type=NodePort -- port=8080 
    
    
  3. Check Node Port

    $ kubectl get svc
     NAME              TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
     hello-minikube1   NodePort    10.100.238.34   <none>        8080:31389/TCP   3s
    
  4. Run service tunnel

    minikube service hello-minikube1 --url
    

    minikube service hello-minikube1 --url runs as a process, creating a tunnel to the cluster. The command exposes the service directly to
    any program running on the host operating system.

  5. Try in your browser

    Open in your browser (ensure there is no proxy set)

    http://127.0.0.1:TUNNEL_PORT
    
0
On

I figured out how to do it but not sure if this is the proper answer - which is by doing

kubectl port-forward svc/some-service 8200:8200

although this works, I still don't understand why the NodePort does not automatically do this by default as my understanding was the point of service NodePort.

Also another confusion:

  • per my service file, the NodePort that was exposed was 30082 however, when I do kubectl port-forward I have to use 8200, which is mapping my localhost directly to the pod so definitely don't see the point of NodePort service here.