<p>I am trying to learn Kubernetes.</p>
<p>I am trying to do this using Minikube and here is what I did:</p>
<p>1.) Write a simple server using Node</p>
<p>2.) Write a <code>Dockerfile</code> for that particular Node server</p>
<p>3.) Create a Kubernetes <code>deployment</code></p>
<p>4.) Create a service (of type ClusterIP)</p>
<p>5.) Create a service (of type NodePort) to expose the container so I can access from outside (browser, curl)</p>
<p>But when I try to connect to the <code>NodePort</code> with the format of <code>&lt;NodePort&gt;:&lt;port&gt;</code> and `:, it gives an error:</p>
<blockquote>
<p>Failed to connect to 192.168.39.192 port 80: Connection refused</p>
</blockquote>
<p>These are the files I created as steps mentioned above (1-5).</p>
<p><strong>1.) server.js</strong> - Here only I have mentioned <code>server.js</code>, relevent <code>package.json</code> exists and they work as expected when I run the server locally (without deploying it in Docker), I mention this in case readers might ask whether my server works correctly.</p>
<pre><code>'use strict';

const express = require('express');

// Constants
const PORT = 8080;
const HOST = '0.0.0.0';

// App
const app = express();
app.get('/', (req, res) =&gt; {
  res.send('Hello world\n');
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
</code></pre>
<p><strong>2.) Dockerfile</strong></p>
<pre><code>FROM node:10

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 8080
CMD [ &quot;npm&quot;, &quot;start&quot; ]
</code></pre>
<p><strong>3.) deployment.yaml</strong></p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: node-web-app
spec:
  replicas: 2
  selector:
    matchLabels:
      name: node-web-app
  template:
    metadata:
      labels:
        # you can specify any labels you want here
        name: node-web-app
    spec:
      containers:
      - name: node-web-app
        # image must be the same as you built before (name:tag)
        image: banuka/node-web-app
        ports:
        - name: http
          containerPort: 8080
          protocol: TCP
        imagePullPolicy: Never
      terminationGracePeriodSeconds: 60
</code></pre>
<p><strong>4.) clusterip.yaml</strong></p>
<pre><code>kind: Service
apiVersion: v1
metadata:
  labels:
    # these labels can be anything
    name: node-web-app-clusterip
  name: node-web-app-clusterip
spec:
  selector:
    app: node-web-app
  ports:
    - protocol: TCP
      port: 80
      # target is the port exposed by your containers (in our example 8080)
      targetPort: 8080
</code></pre>
<p><strong>5.) NodePort.yaml</strong></p>
<pre><code>kind: Service
apiVersion: v1
metadata:
  labels:
    name: node-server-nodeport
  name: node-server-nodeport
spec:
  # this will make the service a NodePort service
  type: NodePort
  selector:
    app: node-app-web
  ports:
    - protocol: TCP
      # new -&gt; this will be the port used to reach it from outside
      # if not specified, a random port will be used from a specific range (default: 30000-32767)
      nodePort: 32555
      port: 80
      targetPort: 8080
</code></pre>
<p>And when I try to curl from outside or use my web browser it gives following error:</p>
<blockquote>
<p>curl: (7) Failed to connect to 192.168.39.192 port 32555: Connection refused</p>
</blockquote>
<p>PS: pods and containers are also working as expected.</p>

BhagyaKolitha Jayalath
<p>There are several possible reasons for this.
First: Are you using your local IP or the IP where the minikube VM is running? To verify use <code>minikube ip</code>.
Second: The <code>NodePort</code> service wants to use pods with label <code>app: node-app-web</code>, but your pods only have the label <code>name: node-web-app</code>
Just to make sure the port that you assume is used, check with <code>minikube service list</code> that the requested port was allocated. Check your firewall settings as well.</p>
