<p>Following a tutorial on Kubernetes and got stuck after the logs look fine, but the port exposed doesn't work : &quot;Connection Refused&quot; using Chrome / curl.</p>
<p>Used a yaml file to power up the service via NodePort / ClusterIP.</p>
<p>posts-srv.yaml - Updated</p>
<pre class="lang-js prettyprint-override"><code>apiVersion: v1
kind: Service
metadata:
  name: posts-srv
spec:
  type: NodePort
  selector:
    app: posts
  ports:
    - name: posts
      protocol: TCP
      port: 4000
      targetPort: 4000
      nodePort: 32140
</code></pre>
<p>posts-depl.yaml - Updated</p>
<pre class="lang-js prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: posts-depl
spec:
  replicas: 1
  selector:
    matchLabels:
      app: posts
  template:
    metadata:
      labels:
        app: posts
    spec:
      containers:
        - name: posts
          image: suraniadi/posts
          ports:
            - containerPort: 4000
</code></pre>
<pre><code>$ kubectl get deployments
NAME         READY   UP-TO-DATE   AVAILABLE   AGE
posts-depl   1/1     1            1           27m
$ kubectl get services
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP   10.96.0.1       &lt;none&gt;        443/TCP          27h
posts-srv    NodePort    10.111.64.122   &lt;none&gt;        4000:32140/TCP   21m
$ kubectl get pods
NAME                          READY   STATUS    RESTARTS   AGE
posts-depl-79b6889f89-rxdv2   1/1     Running   0          26m
$ kubectl version
Client Version: version.Info{Major:&quot;1&quot;, Minor:&quot;19&quot;, GitVersion:&quot;v1.19.7&quot;, GitCommit:&quot;1dd5338295409edcfff11505e7bb246f0d325d15&quot;, GitTreeState:&quot;clean&quot;, BuildDate:&quot;2021-01-13T13:23:52Z&quot;, GoVersion:&quot;go1.15.5&quot;, Compiler:&quot;gc&quot;, Platform:&quot;darwin/amd64&quot;}
Server Version: version.Info{Major:&quot;1&quot;, Minor:&quot;19&quot;, GitVersion:&quot;v1.19.7&quot;, GitCommit:&quot;1dd5338295409edcfff11505e7bb246f0d325d15&quot;, GitTreeState:&quot;clean&quot;, BuildDate:&quot;2021-01-13T13:15:20Z&quot;, GoVersion:&quot;go1.15.5&quot;, Compiler:&quot;gc&quot;, Platform:&quot;linux/amd64&quot;}
</code></pre>

<p>For structural reasons, it's better to specify the nodePort in your service yaml configuration file or kubernetes will allocate it  randomly from the k8s port range (30000-32767).
In the ports section it's a list of ports no need, in your case, to specify a name check the <a href="https://kubernetes.io/docs/concepts/services-networking/service/#nodeport" rel="nofollow noreferrer">nodePort_docs</a> for more infos.
This should work for you :</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: posts-srv
spec:
  type: NodePort
  selector:
    app: posts
  ports:
    - port: 4000
      targetPort: 4000
      nodePort: 32140
      protocol: TCP
</code></pre>
<p>To connect to the nodePort service verify if any firewall service is up then  verify that this port is enabled in your VMs : (centos example)</p>
<pre><code>sudo firewall-cmd  --permanent --add-port=32140/tcp
</code></pre>
<p>Finally connect to this service using any <strong>node IP</strong> address (not the CLusterIP, this IP is an INTERNAL-IP not accessible outside the cluster) and the nodePort : &lt;node_pubilc_IP&gt;:&lt;nodePort:32140&gt;</p>
