<p>A Kubernetes <a href="https://kubernetes.io/docs/concepts/services-networking/service/" rel="noreferrer"><code>Service</code></a> can have a <code>targetPort</code> and <code>port</code> in the service definition:</p>

<pre class="lang-yaml prettyprint-override"><code>kind: Service
apiVersion: v1
metadata:
  name: my-service
spec:
  selector:
    app: MyApp
  ports:
  - protocol: TCP
    port: 80
    targetPort: 9376
</code></pre>

<p>What is the difference between the <code>port</code> and <code>targetPort</code>?</p>

Mr.DevEng
<p><strong>Service:</strong> This directs the traffic to a pod.</p>

<p><strong>TargetPort:</strong> This is the actual port on which your application is running inside the container. </p>

<p><strong>Port:</strong> Some times your application inside container serves different services on a different port. </p>

<p><strong>Example:</strong> The actual application can run <code>8080</code> and health checks for this application can run on <code>8089</code> port of the container.
So if you hit the service without port it doesn't know to which port of the container it should redirect the request. Service needs to have a mapping so that it can hit the specific port of the container.</p>

<pre><code>kind: Service
apiVersion: v1
metadata:
  name: my-service
spec:
  selector:
    app: MyApp
  ports:
    - name: http
      nodePort: 30475
      port: 8089
      protocol: TCP
      targetPort: 8080
    - name: metrics
      nodePort: 31261
      port: 5555
      protocol: TCP
      targetPort: 5555
    - name: health
      nodePort: 30013
      port: 8443
      protocol: TCP
      targetPort: 8085 
</code></pre>

<p>if you hit the <code>my-service:8089</code> the traffic is routed to <code>8080</code> of the container(targetPort). Similarly, if you hit <code>my-service:8443</code> then it is redirected to <code>8085</code> of the container(targetPort). But this <code>myservice:8089</code> is internal to the kubernetes cluster and can be used when one application wants to communicate with another application. So to hit the service from outside the cluster someone needs to expose the port on the host machine on which kubernetes is running
so that the traffic is redirected to a port of the container. This is <code>node port</code>(port exposed on the host machine).
From the above example, you can hit the service from outside the cluster(Postman or any rest-client) by <code>host_ip:nodePort</code></p>

<p>Say your host machine ip is <code>10.10.20.20</code> you can hit the http, metrics, health services by <code>10.10.20.20:30475</code>, <code>10.10.20.20:31261</code>, <code>10.10.20.20:30013</code>.</p>

<p>Edits: Edited as per <a href="https://stackoverflow.com/users/545127/raedwald">Raedwald</a> comment.</p>
