Kubernetes NodePort not working correctly

10/13/2019

I have a cluster Kubernetes cluster on a local machine and one raspberry pi. In order to test the cluster I created a nginx deployment and a service that I want to access as a NodePort. But for God know's why, I can't access said service. Bellow are my deployment and service files.

kubectl get nodes

NAME      STATUS                        ROLES    AGE    VERSION
anima     Ready                         master   7d5h   v1.16.1
bahamut   Ready                         <none>   7d4h   v1.16.1

My service and deployment files:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        resources:
          limits:
            memory: "128Mi"
            cpu: "500m"
        ports:
        - containerPort: 80
      nodeSelector:
        kubernetes.io/hostname: bahamut
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  selector:
    app: nginx
  ports:
  - port: 3030
    targetPort: 80
  type: NodePort

After kubectl get pods -o wide:

NAME                     READY   STATUS    RESTARTS   AGE     IP            NODE      NOMINATED NODE   READINESS GATES
nginx-67c8c4b564-6x7g5   1/1     Running   0          6m21s   10.244.1.13   bahamut   <none>           <none>

My Deployments, kubectl get deployments -o wide:

NAME    READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES         SELECTOR
nginx   1/1     1            1           7m55s   nginx        nginx:latest   app=nginx

My Services, kubectl get svc -o wide:

NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE     SELECTOR
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP          7d5h    <none>
nginx        NodePort    10.102.203.77   <none>        3030:30508/TCP   8m54s   app=nginx

And finally, kubectl get endpoints -o wide:

NAME         ENDPOINTS           AGE
kubernetes   192.168.25.4:6443   7d5h
nginx        10.244.1.13:80      9m41s

My Kubernetes master local IP is 192.168.25.4 and my raspberry ip is 192.168.25.6. After deploying the service I tried:

curl 192.168.25.6:3030
curl: (7) Failed to connect to 192.168.25.6 port 3030: Connection refused

curl 192.168.25.6:80
curl: (7) Failed to connect to 192.168.25.6 port 80: Connection refused

curl 192.168.25.6:30508 (hangs)

Also tried using the master node IP, the Service IP and the listed Cluster IP, but nothing works.

EDIT

It works if I use hostNetwork=true on the deployment and access it using the node local IP on the container port, but obviously that's not what I want. I want to understand why Kubernetes isn't let me access the container through the service.

-- alessandrocb
curl
docker
kubernetes
nginx

3 Answers

10/14/2019

The problem was with the flannel interface. After a ip link delete flannel.1 and restarting flannel pods, the service started to work flawlessly.

-- alessandrocb
Source: StackOverflow

10/14/2019

NodePort exposes the Service on each Node’s IP at a static port (the NodePort) in your case it is 30508. Please see more details here.

And this will share more details on the bare-metal clusters.

-- Bimal
Source: StackOverflow

10/14/2019

The port should be the value of the port exposed by the service, 80.

If you want to keep the node port fixed to an specific value, change your service to something like:

  - port: 80
    nodePort: 30030

please note that the node port range is 30000 - 32767 after that you should be able to query:

curl <node>:30030
-- Pedreiro
Source: StackOverflow