<p>There's a working k8s configuration which uses the same port name in a pod and in a service. Here's a config part of the pod:</p>
<pre><code>ports:
- containerPort: 8000
  name: app-port
  protocol: TCP
</code></pre>
<p>Here's a config part of the service:</p>
<pre><code>  ports:
  - name: app-port
    nodePort: 32000
    port: 8000
    protocol: TCP
    targetPort: app-port
  type: NodePort
</code></pre>
<p>How is the name <code>app-port</code> resolved?</p>

<p>Basically you will refer to the target port with the <strong>port number</strong>. But, you can give a name to each port mentioned in the pod configuration and use this name in the <strong>service</strong> to refer to the specified port. By this your <strong>service</strong> will be aware of which port it needs to consider from the pod.</p>
<p>To answer your question, the <strong>port name</strong> which you created in the <strong>pod belongs only to the pod configuration</strong>. It can be used as a reference to get the portnumber of a pod. But in your service you are creating a new <strong>portname</strong> attribute which only belongs to the service. The <code>targetPort</code> in service will only consider the port number from the <code>portname</code> attribute created in the service and not from the pod.</p>
<p>Likewise the kubernetes knows that it needs to consider the <code>target port number</code> for the <strong>service</strong> by the <strong>port name</strong> created in the <strong>service</strong> and not from the <strong>pod</strong>.</p>
<p>Basically k8's uses DNS to determine all this stuff. For more information you can refer to this <a href="https://kubernetes.io/docs/concepts/services-networking/service/#defining-a-service" rel="nofollow noreferrer">link1</a> <a href="https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/" rel="nofollow noreferrer">link2</a></p>
