<p>Let's say that we have a Node app running inside a Docker container and listening on port 3000. We want to expose this app to be accessible in the browser, also in port 3000. So we do port forwarding like this:</p>
<pre><code>docker run -p &lt;port&gt;:&lt;targetPort&gt; my-image // for example: 3000:3000
</code></pre>
<p>So docker knows on which port to listen and to which process inside the container to forward the network.</p>
<p>But in k8s NodePort service, its enough to provide the NodePort service port and the target port of the Pod, but not the target port inside the Pod, so we have random outside port, 30,000-32,000 (approximately) that listens to outside traffic, forward this to the port of the NodePort service and then forward it to the target port of a Pod, but.. hey, we didn't mention the target port <strong>inside the Pod</strong>.</p>
<p>So how the Pod object knows to which process inside it to forward the traffic? We usually assume we have only single container in a Pod, but if we have multiple?</p>
<p>Also note that in the example of exposing image in Docker - if we don't mention port forwarding the whole exposition won't work so it won't be accessible via browser (outside the container)..</p>

Raz Buchnik
<p><code>containerPort</code> in pod spec is just informational and non mandatory. Mentioning that does not make the application inside the pod listen on that port. Normally you will specify which port the application should listen on in the source code or in dockerfile.The <code>targetPort</code> in the service need to match with that port. For example the <code>targetPort</code> for a service which forwards traffic to nginx container running inside a pod should be <code>80</code> because nginx process listens on port 80.</p>
<p><strong>targetPort` in the service informs kubernetes service which port of the container running inside the pod to forward the traffic to.</strong></p>
<p>In case of a pod which has got multiple containers listening on different ports you can specify multiple <code>targetPort</code> in service as below.</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: service-name
spec:
  ports:
  - name: http
    port: 80
    targetPort: 80
  - name: https
    port: 443
    targetPort: 443
  - name: something
    port: 6001
    targetPort: 6001
  selector:
    app: app-label
  type: NodePort
</code></pre>
