<p>All of the links of google are pink... I've read almost every answer on here and consulted several colleagues and they cannot work out what is wrong with my config.</p>
<h2>My WebSocket server</h2>
<pre><code>// additional fastify websocket set-up - key point, is its running on 5000
const start = async () =&gt; {
  try {
    await fastify.listen({ port: 5000 });
    console.log(
      `Market data websocket listening on ws://localhost:5000`
    );
  } catch (err) {
    fastify.log.error(err);
    process.exit(1);
  }
};
start();

</code></pre>
<h2>Confirmation that this server works locally</h2>
<p>I tested using <code>wscat</code> and it connects perfectly fine</p>
<pre><code>wscat -c &quot;ws://localhost:5000`
</code></pre>
<h2>Deployment file</h2>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: test
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: market-data-ws
    spec:
      containers:
        - name: market-data-ws
          image: eu.gcr.io/test-repo/market-data-ws
          envFrom:
            - secretRef:
                name: market-data-ws-env
          resources:
            limits:
              memory: '0.5'
              cpu: '0.25'
          ports:
            - containerPort: 5000
          livenessProbe:
            httpGet:
              path: /healthz
              port: 8080
            initialDelaySeconds: 5

</code></pre>
<h2>Service</h2>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: market-data-ws
spec:
  type: ClusterIP
  selector:
    app: market-data-ws
  ports:
    - port: 80
      targetPort: 5000

</code></pre>
<h2>My issue</h2>
<p>I'm trying to connect to this service <strong>from another pod server</strong>. It does not need to be exposed via ingress.</p>
<p>Note: the pod will fail liveness probe, even though if i exec in and check the liveness probe it works.</p>
<h3>The liveness endpoint works from inside the pod</h3>
<pre><code>kubectl exec -it $(kubectl get pods -o name | grep market-data) /bin/sh
curl http://localhost:5000/healthz
{&quot;hello&quot;:&quot;im alive&quot;}/
</code></pre>
<h3>❌ Accessing the pod by curling the hostname from inside another pod = Connection refused instantly</h3>
<pre><code>&gt; kubectl exec -it $(kubectl get pods -o name | grep ui) /bin/sh
# curl the health check
&gt; curl http://market-data-ws/healthz
&gt; curl: (7) Failed to connect to market-data-ws port 80 after 8 ms: Connection refused
</code></pre>
<h3>❌ Curl the IP instead of hostname = Connection refused instantly</h3>
<pre><code>curl 10.104.1.61:80/healthz
curl: (7) Failed to connect to 10.104.1.61 port 80 after 3 ms: Connection refused

</code></pre>
<h3>❓ Curl the IP (but the wrong exposed port) = Connection Timeout after a few minutes</h3>
<p>Because we get a time out here instead, this suggests to me that we were actually reaching our running container previously.</p>
<pre><code>curl 10.104.1.61:5000/healthz
curl: (28) Failed to connect to 10.104.1.61 port 5000 after 129952 ms: Operation timed out

</code></pre>
<h3>❓ When I port-forward my local port to this running service port it works</h3>
<pre><code>kub port-forward service/market-data-ws 5000:5000
Forwarding from 127.0.0.1:5000 -&gt; 5000
Forwarding from [::1]:5000 -&gt; 5000

wscat -c http://localhost:5000
&gt; Connected
</code></pre>
<p><strong>Other things I've tried</strong></p>
<ul>
<li>Execing into the <code>market-data-ws</code> pod and using <code>wscat -c ws://localhost:5000</code> to check if its actually running okay - it is.</li>
<li>Double, triple checked my containterPort / targetPort and colleagues have confirmed it looks 0okay.</li>
<li>Tried <code>NodePort</code> and <code>ClusterIp</code></li>
<li>tried running my websocket server on port 80 and exposing that</li>
</ul>
<h2>Other info</h2>
<ul>
<li>Im using GCP GKE</li>
<li>output of <code>kubectl describe service market-data-ws</code></li>
</ul>
<p><a href="https://i.stack.imgur.com/ZUHsy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZUHsy.png" alt="kub describe" /></a></p>

Daniel Cooke
<p>Okay i have finally found my issue, wow that was the hardest problem in a while.</p>
<p><a href="https://stackoverflow.com/questions/61822353/fastify-not-working-on-docker-kubernetes">Thanks to this answer</a> I was able to fix my issue.</p>
<h3>Fastify by default only listens on localhost</h3>
<p>I'm honestly not sure why this is not sufficient for use with docker + kubernetes, but I'm just glad its working now.</p>
<p>If someone is able to explain this in greater detail, I would be very grateful</p>
