<p>I am working on a microservice web application (React and Node) using NATS and running it on GCP Kubernetes cluster.</p>
<p>I am unable to connect to the NAT streaming server pod. The ports 4222 &amp; 8222 are open.I am getting <strong>connection refused</strong> error.</p>
<blockquote>
<p>Error:</p>
</blockquote>
<pre><code>Using ts-node version 8.10.2, typescript version 3.9.7
NatsError: Could not connect to server: Error: connect ECONNREFUSED 10.32.13.69:4222
    at Socket.&lt;anonymous&gt; (/app/node_modules/nats/lib/nats.js:801:26)
    at Socket.emit (events.js:314:20)
    at emitErrorNT (internal/streams/destroy.js:100:8)
    at emitErrorCloseNT (internal/streams/destroy.js:68:3)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  code: 'CONN_ERR',
  chainedError: Error: connect ECONNREFUSED 10.32.13.69:4222
      at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16) {
    errno: -111,
    code: 'ECONNREFUSED',
    syscall: 'connect',
    address: '10.32.13.69',
    port: 4222
  }
}
</code></pre>
<p>Pod + Service file for NAT Streaming Server</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment 
metadata: 
    name: nats-depl
spec:
    replicas: 1
    selector: 
        matchLabels: 
            app: nats
    template:
        metadata: 
            labels: 
                app: nats
        spec:
            containers: 
                - name: nats 
                  image: nats-streaming:0.17.0 
                  ## args provides arguments to the primary
                  ## cmd executed when container starts 
                  args: [
                    '--port', '4222',
                    '--http_port', '8222',
                    '--hb_interval', '5s',      
                    '--hb_timeout', '5s',       
                    '--hb_fail_count', '2',     
                    '--stan_debug', 
                    '--cluster_id', 'ticketing'
                  ]
</code></pre>
<p>Service config :</p>
<pre class="lang-yaml prettyprint-override"><code>---
apiVersion: v1
kind: Service 
metadata:
    name: nats-srv 
spec: 
    type: ClusterIP
    selector: 
        apps: nats 
    ports: 
        - name: client 
          protocol: TCP 
          port: 4222 
          targetPort: 4222 
        - name: monitoring 
          protocol: TCP 
          port: 8222 
          targetPort: 8222 

</code></pre>
<blockquote>
<p>$ kubectl describe service nats-srv</p>
</blockquote>
<pre><code>$ kubectl describe service nats-srv
Name:              nats-srv
Namespace:         default
Labels:            app.kubernetes.io/managed-by=skaffold-v1.11.0
                   skaffold.dev/builder=google-cloud-build
                   skaffold.dev/cleanup=true
                   skaffold.dev/deployer=kubectl
                   skaffold.dev/run-id=59070209-9ab7-47cd-ab0f-cdd91797b1a7
                   skaffold.dev/tag-policy=git-commit
                   skaffold.dev/tail=true
Annotations:       kubectl.kubernetes.io/last-applied-configuration:
                     {&quot;apiVersion&quot;:&quot;v1&quot;,&quot;kind&quot;:&quot;Service&quot;,&quot;metadata&quot;:{&quot;annotations&quot;:{},&quot;labels&quot;:{&quot;app.kubernetes.io/managed-by&quot;:&quot;skaffold-v1.11.0&quot;,&quot;skaffold.dev...
Selector:          apps=nats
Type:              ClusterIP
IP:                10.32.13.69
Port:              client  4222/TCP
TargetPort:        4222/TCP
Endpoints:         &lt;none&gt;
Port:              monitoring  8222/TCP
TargetPort:        8222/TCP
Endpoints:         &lt;none&gt;
Session Affinity:  None
Events:            &lt;none&gt;

</code></pre>

Karan Kumar
<p>The service has selector <code>apps: nats</code> but deployment has label <code>app: nats</code>. They need to be same.This is why <code>Endpoints</code> in service is not having Pod IPs and hence you get <code>connection refused</code>. Modify the service as below</p>
<pre><code>---
apiVersion: v1
kind: Service 
metadata:
    name: nats-srv 
spec: 
    type: ClusterIP
    selector: 
        app: nats 
    ports: 
        - name: client 
          protocol: TCP 
          port: 4222 
          targetPort: 4222 
        - name: monitoring 
          protocol: TCP 
          port: 8222 
          targetPort: 8222
</code></pre>
