<p>Hopefully some k8s genius can point out what I'm doing wrong with port mappings. We have a NextJS FE app, which calls: <code>http://gateway-api:8000/...</code> - The dns resolves to the correct service IP, but it returns connection refused. Our YAML are below, and the port is exposed at the docker level. Calling the API works flawlessly from the likes of postman et al, but the FE consistently gets refused. One can only presume that the ports aren't mapped correctly, or the Service isn't 'linking' to the Pod correctly in some other fashion.</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: gateway-api
spec:
  selector:
    name: gateway-api
  template:
    metadata:
      labels:
        run: gateway-api
  containers:
    - name: gateway-api
      image: something/gateway-api:latest
      ports:
        - containerPort: 8000
</code></pre>
<p>and the service</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: gateway-api
  labels:
    run: gateway-api
spec:
  ports:
    - port: 8000
      protocol: &quot;TCP&quot;
      targetPort: 8000
  selector:
    app: gateway-api
</code></pre>
<p>Various info:</p>
<pre><code>kubectl describe service gateway-api
Name:              gateway-api
Namespace:         default
Labels:            app.kubernetes.io/managed-by=tilt
                   run=gateway-api
Annotations:       &lt;none&gt;
Selector:          app=gateway-api
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.103.9.152
IPs:               10.103.9.152
Port:              &lt;unset&gt;  8000/TCP
TargetPort:        8000/TCP
Endpoints:         &lt;none&gt;
Session Affinity:  None
Events:            &lt;none&gt;
</code></pre>
<pre><code>get pod -o wide             
NAME          READY   STATUS    RESTARTS      AGE   IP           NODE             NOMINATED NODE   READINESS GATES
gateway-api   1/1     Running   0             12m   10.1.1.161   docker-desktop   &lt;none&gt;           &lt;none&gt;
gateway-web   1/1     Running   0             12m   10.1.1.160   docker-desktop   &lt;none&gt;           &lt;none&gt;
mysql-0       1/1     Running   7 (19h ago)   75d   10.1.1.140   docker-desktop   &lt;none&gt;           &lt;none&gt;
</code></pre>
<pre><code>kubectl get service -o wide
NAME             TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE   SELECTOR
gateway-api      ClusterIP   10.103.9.152    &lt;none&gt;        8000/TCP   11m   app=gateway-api
</code></pre>
<p>Should also state, that we're unable to use <code>localhost:xxxx</code> owing to the fact we utilise NextJS server. Hence the named pod/service route.</p>

laminatefish
<p>The Service's <code>selector:</code> matches the Pod's <code>labels:</code>.  Your Pod has no <code>metadata: { labels: }</code> and so no Service can ever match it.</p>
<p>In practice, it's unusual to directly create Pods; you'd almost always use a higher-level object like a Deployment.  The labels need to be in the Pod template in the Deployment spec:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: gateway-api
  labels: { ... } # good practice, not matched by the Service
spec:
  template:
    metadata:
      labels: # &lt;-- matched by the Service
        app: gateway-api
</code></pre>
