Accessing kubernetes service with nodeport returns with connection refused

5/22/2020

I have installed my on-permise kubernetes on 3 VMs (1 master and 2 worker nodes) with 3 public ip. I try to verify by deploying a nginx instance with nodeport like this:

kubectl create deployment nginx --image=nginx
kubectl create service nodeport nginx --tcp=80:80

The nodeport assigned some port range to me like 30269:

NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        5d21h
nginx        NodePort    10.107.68.135   <none>        80:30269/TCP   91m

When I try to curl my IP address on this port, it returns Connection refused, no matter on my master or on worker nodes. I am sure firewall do allows the ports, as I have tried to use docker to run nginx on this port by:

docker run -p 30269:80 -d nginx

and I can reach the nginx default page by running curl on my public ip on port 30269

If I run netstat, the output is like this

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:30269           0.0.0.0:*               LISTEN      3396/kube-proxy   
tcp        0      0 127.0.0.1:6784          0.0.0.0:*               LISTEN      3741/weaver 
tcp        0      0 127.0.0.1:10248         0.0.0.0:*               LISTEN      2726/kubelet
...

How should I proceed to troubleshoot the problem?

Thanks you very much in advance for your help.

-- Kenneth Tang
kubernetes
nginx

1 Answer

5/23/2020

You are missing the specification of how the service connects to your pods.

This is done by specifying two things:

  1. In the pod spec you need to specify a label (let's say app:nginx)
  2. In the service spec, you need to specify to which pod labels this service applies which in your case is the label in section 1 above - app:nginx

Then, once you apply these resources the node port service will map the nginx pod to the node port.

I would strongly advise you to read this article to get a better sense of how services operate: https://kubernetes.io/docs/tasks/access-application-cluster/service-access-application-cluster/

-- omricoco
Source: StackOverflow