<p>I am trying to run the kubernetes service via NodePort.</p>
<p><a href="https://i.stack.imgur.com/TRzpu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TRzpu.png" alt="This is my node.js running" /></a></p>
<p><a href="https://i.stack.imgur.com/6QZtb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6QZtb.png" alt="This is my deployment file" /></a></p>
<p><a href="https://i.stack.imgur.com/Y9OBO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Y9OBO.png" alt="This is my service file" /></a></p>
<p><a href="https://i.stack.imgur.com/5p4E1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5p4E1.png" alt="Pods" /></a></p>
<p><a href="https://i.stack.imgur.com/fZxHJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fZxHJ.png" alt="Deployment running" /></a></p>
<p><a href="https://i.stack.imgur.com/XA7EB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XA7EB.png" alt="Services running" /></a></p>
<p><a href="https://i.stack.imgur.com/dWwkC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dWwkC.png" alt="Description of service running" /></a></p>
<p><a href="https://i.stack.imgur.com/BZ4v5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BZ4v5.png" alt="enter image description here" /></a></p>
<p>I can't figure out what the problem is when i try to hit <code>localhost:31553</code> on my device it is not listening there. Moreover I also tried with <code>10.101.184.102:31553</code> it also doesn't work.
I want to run it on localhost.
Please Help.</p>

Anurag Gupta
<p>When you are using <code>NodePort</code> service you need to access the service via node's ip address. Note that this Service is visible as <code>&lt;NodeIP&gt;:spec.ports[*].nodePort</code>. So, the template is like <code>&lt;node-ip&gt;:&lt;node-port&gt;</code> . for getting the node ip you can run the command .</p>
<pre><code>$ kubectl get nodes -o wide
NAME                 STATUS   ROLES    AGE   VERSION   INTERNAL-IP   EXTERNAL-IP   OS-IMAGE       KERNEL-VERSION     CONTAINER-RUNTIME

kind-control-plane   Ready    master   32d   v1.18.2   172.18.0.2    &lt;none&gt;        Ubuntu 19.10   5.8.0-38-generic   containerd://1.3.3-14-g449e9269
</code></pre>
<p>here your  nodeport is <code>31553</code>.</p>
<p>now try curl : <code>curl -k 172.18.0.2:31553</code></p>
<p>and it's always better if you set the <code>spec.ports[*].nodePort</code> value, otherwise it will be a random port from range of <code>30000-32767</code>.</p>
<p><strong>service.yaml</strong></p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: posts-srv
  labels:
    app: posts
spec:
  type: NodePort
  ports:
  - port: 4000
    protocol: TCP
    name: posts
    targetPort: 4000
    nodePort: 31553
  selector:
    app: posts
</code></pre>
