<p>I do not understand how to configure ports correctly for a k8s deployment.</p>
<p>Assume there is a nextJS application which listens to port 3003 (default is 3000). I build the docker image:</p>
<pre><code>FROM node:16.14.0
RUN apk add dumb-init

# ...

EXPOSE 3003

ENTRYPOINT [&quot;/usr/bin/dumb-init&quot;, &quot;--&quot;]
CMD npx next start -p 3003
</code></pre>
<p>So in this Dockerfile there are two places defining the port value <code>3003</code>. Is this needed?</p>
<p>Then I define this k8s manifest:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: example
spec:
    spec:
      containers:
        - name: example
          image: &quot;hub.domain.com/example:1.0.0&quot;
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 3003
---
apiVersion: v1
kind: Service
metadata:
  name: example
spec:
  ports:
    - protocol: TCP
      port: 80
      targetPort: 3003
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example
  namespace: default
  annotations:
    cert-manager.io/cluster-issuer: letsencrypt-prod
    kubernetes.io/ingress.class: nginx
    kubernetes.io/tls-acme: &quot;true&quot;
    nginx.ingress.kubernetes.io/ssl-passthrough: &quot;true&quot;
spec:
  tls:
    - hosts:
        - domain.com
      secretName: tls-key
  rules:
    - host: domain.com
      http:
        paths:
          - pathType: Prefix
            path: &quot;/&quot;
            backend:
              service:
                name: example
                port:
                  number: 80
</code></pre>
<p>The deployment is not working correctly. Calling <em>domain.com</em> shows me a <code>503 Service Temporarily Unavailable</code> error.</p>
<p>If I do a port forward on the pod, I can see the working app at <em>localhost:3003</em>. I cannot create a port forward on the service.</p>
<p>So obviously I'm doing something wrong with the ports. Can someone explain which value has to be set and why?</p>

user3142695
<p>You are missing labels from the deployment and the selector from the service. Try this:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: example
  labels:
    app: example
spec:
  selector:
    matchLabels:
      app: example
  template:
    metadata:
      labels:
        app: example
    spec:
      containers:
        - name: example
          image: &quot;hub.domain.com/example:1.0.0&quot;
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 3003
---
apiVersion: v1
kind: Service
metadata:
  name: example
spec:
  selector:
    app: example
  ports:
    - protocol: TCP
      port: 80
      targetPort: 3003
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example
  namespace: default
  annotations:
    cert-manager.io/cluster-issuer: letsencrypt-prod
    kubernetes.io/ingress.class: nginx
    kubernetes.io/tls-acme: &quot;true&quot;
    nginx.ingress.kubernetes.io/ssl-passthrough: &quot;true&quot;
spec:
  tls:
    - hosts:
        - domain.com
      secretName: tls-key
  rules:
    - host: domain.com
      http:
        paths:
          - pathType: Prefix
            path: &quot;/&quot;
            backend:
              service:
                name: example
                port:
                  number: 80
</code></pre>
<p>Deployment: <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/workloads/controllers/deployment/</a></p>
<p>Service: <a href="https://kubernetes.io/docs/concepts/services-networking/service/" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/services-networking/service/</a></p>
<p>Labels and selectors: <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/</a></p>
<p>You can name your label keys and values anything you like, you could even have a label as <code>whatever: something</code> instead of <code>app: example</code> but these are some recommended labels: <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/</a>
<a href="https://kubernetes.io/docs/reference/labels-annotations-taints/" rel="nofollow noreferrer">https://kubernetes.io/docs/reference/labels-annotations-taints/</a></p>
