Accessing app on kubernetes using nodeport

9/7/2018

Dear all I have deployed a sample service such as this:

 kubectl get svc
NAME          TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
kubernetes    ClusterIP   10.233.0.1     <none>        443/TCP          1d
mynodejsapp   NodePort    10.233.2.225   <none>        3000:31209/TCP   43s

may I ask how do I access the app mynodejsapp on the cluster ip?

When I did a get nodes -o wide this is what I have seen as below,

$ kubectl get nodes -o wide
NAME                    STATUS    ROLES     AGE       VERSION            EXTERNAL-IP   OS-IMAGE                  KERNEL-VERSION                   CONTAINER-RUNTIME
controlplane-node-001   Ready     master    2d        v1.9.1+2.1.8.el7   <none>        Oracle Linux Server 7.2   4.1.12-112.14.13.el7uek.x86_64   docker://17.3.1
controlplane-node-002   Ready     master    2d        v1.9.1+2.1.8.el7   <none>        Oracle Linux Server 7.2   4.1.12-112.14.13.el7uek.x86_64   docker://17.3.1
controlplane-node-003   Ready     master    2d        v1.9.1+2.1.8.el7   <none>        Oracle Linux Server 7.2   4.1.12-112.14.13.el7uek.x86_64   docker://17.3.1
default-node-001        Ready     node      2d        v1.9.1+2.1.8.el7   <none>        Oracle Linux Server 7.2   4.1.12-112.14.13.el7uek.x86_64   docker://17.3.1
default-node-002        Ready     node      2d        v1.9.1+2.1.8.el7   <none>        Oracle Linux Server 7.2   4.1.12-112.14.13.el7uek.x86_64   docker://17.3.1

Any help. Thanks.

-- Adam
kubernetes
kubernetes-service

1 Answer

9/7/2018

may i ask how do I access the app mynodejsapp on the cluster ip?

Now, for direct answer to your question in regards to your service overview:

  • To access mynodejsapp service from outside of the cluster you need to target IP of any of the nodes on port 31209 (and kube-proxy will route it to mynodejsapp service for you)
  • To access mynodejsapp service from within the cluster, meaning from another pod running on that same cluster you need to target clusterIP 10.233.2.225:3000 (or alternatively with running kube-dns you can use service name directly mynodejsapp:3000)

As detailed in the official documentation clusterIP is tied to service, and in turn it is resolved through kube-dns from service name to clusterIP. In a nutshell you can use clusterIP only from within pods running on said cluster (same as service).

As for exposing services externally through NodePort you can find more info also in the official documentation

-- Const
Source: StackOverflow