<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>

StuffHappens
<p>When you create the Service, it is associated with Pods selected by the label selector defined in the Service <code>spec.selector</code>.</p>
<p>When a request is made to the Service, the Control Plane retrieves its <code>spec.ports[*].targetPort</code> value:</p>
<ul>
<li><p>If it does not exist, the value of <code>spec.ports[*].port</code> field is used instead to forward traffic to the Pods.</p>
</li>
<li><p>If it does exist and it is a number, the <code>targetPort</code> value is used to forward traffic to the Pods.</p>
</li>
<li><p>If it is a string, the Control Plane looks up the port by name in <code>spec.ports[*].name</code> of the Pod, and uses the port with the matching name as the target port.</p>
</li>
</ul>
<p>Here is the relevant piece of code in Kubernetes:</p>
<pre><code>// FindPort locates the container port for the given pod and portName.  If the
// targetPort is a number, use that.  If the targetPort is a string, look that
// string up in all named ports in all containers in the target pod.  If no
// match is found, fail.
func FindPort(pod *v1.Pod, svcPort *v1.ServicePort) (int, error) {
    portName := svcPort.TargetPort
    switch portName.Type {
    case intstr.String:
        name := portName.StrVal
        for _, container := range pod.Spec.Containers {
            for _, port := range container.Ports {
                if port.Name == name &amp;&amp; port.Protocol == svcPort.Protocol {
                    return int(port.ContainerPort), nil
                }
            }
        }
    case intstr.Int:
        return portName.IntValue(), nil
    }

    return 0, fmt.Errorf(&quot;no suitable port for manifest: %s&quot;, pod.UID)
}
</code></pre>
<p><a href="https://github.com/kubernetes/kubernetes/blob/v1.26.0/pkg/api/v1/pod/util.go#L32" rel="nofollow noreferrer">source</a></p>
