<p>I am new to kubernetes and I am trying to learn it by deploying a simple node server using AWS EKS. (kubernetes is alreay setup to talk to the created AWS EKS cluster)</p>
<p>Here is code for my simple node file (<code>server.js</code>)</p>
<pre><code>const express = require('express')
const app = express()
const port = 8080

app.get('/', (req, res) =&gt; {
 res.send('Hello World!')
})

app.listen(port, () =&gt; {
console.log(`Example app listening at http://localhost:${port}`)
})
</code></pre>
<p>Here is how the <code>Dockerfile</code> looks like:</p>
<pre><code>FROM node:12
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
COPY package*.json ./
RUN npm ci
# Bundle app source
COPY . .
EXPOSE 8080
CMD [ &quot;node&quot;, &quot;server.js&quot; ]
</code></pre>
<p>I am able to run the above server in my local by creating a docker image.</p>
<p>Now, in order to deploy this server here are the steps that I followed: <br />
First, I pushed the above image to doceker hub (aroraankit7/simple-server)
Second, I created a deployment.yaml file which looks like this:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: simple-server-app
  labels:
    app: simple-server-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: simple-server-app
   template:
     metadata:
       labels:
         app: simple-server-app
     spec:
       containers:
       - name: simple-server
         image: aroraankit7/simple-server:v1
         ports:
         - containerPort: 8080
</code></pre>
<p>Third, I deployed this using kubectl apply command. Here is the output for <code>kubectl get pods</code>
<a href="https://i.stack.imgur.com/4wqpx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4wqpx.png" alt="enter image description here" /></a></p>
<p>Then, I created the service.yaml file. Here is how it looks:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
name: simple-server-svc
labels:
  run: simple-server
spec:
  ports:
  - port: 8080
    targetPort: 8080
    protocol: TCP
  selector:
    run: simple-server
</code></pre>
<p>I then deployed this using the kubectl apply command. Output for <code>kubectl describe services</code>:
<a href="https://i.stack.imgur.com/nh96D.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nh96D.png" alt="enter image description here" /></a></p>
<p>Next, I logged into on of my pods by using the command: <code>kubectl -it exec simple-server-app-758dfb88f4-4cfmp bash</code></p>
<p>While inside this pod, I ran the following the command: <code>curl http://simple-server-svc:8080</code> and this was the output that I got: <code>curl: (7) Failed to connect to simple-server-svc port 8080: Connection refused</code></p>
<p>Why is the connection getting refused?</p>
<p>When I am running <code>curl http:localhost://8080</code>, I am getting the right output (Hello World! in this case)</p>

Ankit Arora
<p>Your service is not bound to the deployment. You need to modify the selector in your service.yaml to the following:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
 name: simple-server-svc
 labels:
  run: simple-server
spec:
  ports:
  - port: 8080
    targetPort: 8080
    protocol: TCP
  selector:
    app: simple-server-app
</code></pre>
<p>You can use kubectl expose command to avoid mistakes like this.</p>
