<p>I have a k8s cluster running locally on an arm Mac. I have client and server pods. The client is a React frontend. The server is an express server connecting to a mongodb Atlas cluster.</p>
<p>So far the images build fine and all pods are running.</p>
<p>The problem is the internal port routing is not working. All I see is</p>
<pre><code>GET http://localhost:5000/ net::ERR_CONNECTION_REFUSED
</code></pre>
<p>And the referrer policy in the networking tab suggests a CORS error:</p>
<pre><code>Referrer Policy: strict-origin-when-cross-origin
</code></pre>
<p>I'm not sure what I need to do to get my client to fetch on a certain port? So far I have this, which works outside of k8s:</p>
<pre><code>const getAllUsers = () =&gt; {
    fetch(&quot;http://localhost:5000/&quot;)
      .then((res) =&gt; res.text())
      .then((res) =&gt; {
        return setUsers(JSON.parse(res));
      });
  };
</code></pre>
<p>The server has this code to handle the request:</p>
<pre><code>  app.get(&quot;/&quot;, (req, res) =&gt; {
  usersCollection
    .find()
    .toArray()
    .then((results) =&gt; {
      res.json(results);
    })
    .catch((error) =&gt; console.error(error));
});
</code></pre>
<p>My server cluster ip service is like this:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: server-cluster-ip-service
spec:
  type: ClusterIP
  selector:
    component: server
  ports:
    - port: 5000
      targetPort: 5000
</code></pre>
<p>And my server deployment is like this:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: server-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      component: server
  template:
    metadata:
      labels:
        component: server
    spec:
      containers:
        - name: server
          image: mydocker/k8s-server
          ports:
            - containerPort: 5000
          env:
            - name: REDIS_HOST
              value: redis-cluster-ip-service
            - name: REDIS_PORT
              value: &quot;6379&quot;
</code></pre>
<p>When I log out the server deployment logs I get:</p>
<pre><code>&gt; express_mongodb@1.0.0 dev /app
&gt; nodemon server.js

[nodemon] 2.0.12
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
Connected to Database
listening on 5000
</code></pre>
<p>Which is great - it's listening on the port I specified with</p>
<pre><code> app.listen(PORT, function () {
  console.log(&quot;listening on 5000&quot;);
});
</code></pre>
<p>There's something I'm not getting here. The first thing I want to do is make sure my server is connected to my client on port 5000 - what am I doing wrong?</p>
<p>EDIT: After a long time looking at CORS error fixes, maybe it isn't a CORS error? I curl the service ip with:</p>
<pre><code>curl my.ip.##.##
</code></pre>
<p>Then try:</p>
<pre><code>curl localhost:5000
</code></pre>
<p>But the requests time out.</p>
<p>I use <code>kubectl describe service server-cluster-ip-service</code></p>
<p>and get back</p>
<pre><code>Name:              server-cluster-ip-service
Namespace:         default
Labels:            &lt;none&gt;
Annotations:       &lt;none&gt;
Selector:          component=server
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.###.##.##
IPs:               10.###.##.##
Port:              &lt;unset&gt;  5000/TCP
TargetPort:        5000/TCP
Endpoints:         172.##.#.##:5000,172.##.#.##,172.##.#.##
Session Affinity:  None
Events:            &lt;none&gt;
</code></pre>

<p>Ideally, you should be using the service name to communicate with different services inside the cluster.</p>
<p>If you react server want to talk with the express server, you have to the <code>express-service-name</code> as host into the <strong>react</strong> service. Kubernetes will auto manage the resolution.</p>
<p>so for you, it will be <code>server-cluster-ip-service</code></p>
<pre><code>const getAllUsers = () =&gt; {
    fetch(&quot;http://server-cluster-ip-service:5000/&quot;)
      .then((res) =&gt; res.text())
      .then((res) =&gt; {
        return setUsers(JSON.parse(res));
      });
  };
</code></pre>
<p>As in a single machine or in a host all services can talk to each other on <strong>localhost</strong> the same way in Kubernetes cluster services uses the service name to solve each other.</p>
<p>Reference document DNS for services &amp; POD: <a href="https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/</a></p>
<p>You express cluster POD or application will run on <code>0.0.0.0/0</code> and port <code>5000</code> and there will be one <strong>Kubernetes service</strong> as your created now with <code>target port 5000</code>.</p>
<p>Your client or other application internally will call this service by <strong>service name</strong> and request redirected to container (POD) of the express server.</p>
