Repository: kubernetes
Issue: Service with selector does not create all required endpoints, when a POD has multiple containers with a matching named port.
Labels: kind/bug,sig/network,needs-triage

### What happened?

I have a Deployment which creates POD's containing two containers, each container has a named port "grpc-service" on a different port to avoid conflicts.  I want a k8s service to connect to these grpc services in a round-robin fashion and also be able to use the k8s API to retrieve the list of endpoints.  I attempt to do this by creating a simple service, with a label selector that matches the container, and names the target-port.  However when I scale the  deployment only one of the containers is matched to the service,  I was expecting both containers to be matched resulting in two endpoints for each POD instance.

The generated endpoint looks like this, when the deployment is scaled to two replica's (I've trimmed out the targetRef's to keep it brief):

```
kind: Endpoints
apiVersion: v1
metadata:
  name: grpc-test-service
  annotations:
    endpoints.kubernetes.io/last-change-trigger-time: '2025-01-27T11:55:15Z'
subsets:
  - addresses:
      - ip: 10.245.0.71
      - ip: 10.245.2.91
    ports:
      - name: grpc
        port: 8080
        protocol: TCP
```

     


### What did you expect to happen?

I expected the resultant endpoint to look like this (i.e. giving 4 endpoints):

```
apiVersion: v1
kind: Endpoints
metadata:
  name: grpc-test-service
subsets:
- addresses:
  - ip: 10.245.0.124
  - ip: 10.245.2.121
  ports:
  - port: 8080
    name: grpc
    protocol: TCP
- addresses:
  - ip: 10.245.0.124
  - ip: 10.245.2.121
  ports:
  - port: 8081
    name: grpc
    protocol: TCP    
```

### How can we reproduce it (as minimally and precisely as possible)?

To create my test set-up the following yaml can be used (note that for the example I'm using nginx, however in practise I have different containers, which provide the same gRPC services).

```
apiVersion: v1
kind: Service
metadata:
  name: grpc-test-service
spec:
  type: LoadBalancer
  ports:
  - port: 5000
    name: grpc
    targetPort: grpc-server
  selector:
    app: grpc-test-server
    
---

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config-site1
data:
  nginx.conf: |
        events {
        }
        http {
           server {
               listen 8080;
               location / {
                   return 200 "Hello world! I'm site 1";
               }
           }
        }

---

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config-site2
data:
  nginx.conf: |
        events {
        }
        http {
           server {
               listen 8081;
               location / {
                   return 200 "Hello world! I'm site 2";
               }
           }
        }


---

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: grpc-test-server
    name: grpc-test-server
  name: grpc-test-server
spec:
  replicas: 2
  selector:
    matchLabels:
      app: grpc-test-server
      name: grpc-test-server
  template:
    metadata:
      labels:
        app: grpc-test-server
        name: grpc-test-server
      name: grpc-test-server
    spec:
      containers:
      - name: grpc-server-1
        image: nginx
        imagePullPolicy: Always
        ports:
          - name: grpc-server
            containerPort: 8080
            protocol: TCP
        volumeMounts:
          - name: config-site1
            mountPath: /etc/nginx/
               
      - name: grpc-server-2
        image: nginx
        imagePullPolicy: Always
        ports:
          - name: grpc-server
            containerPort: 8081
            protocol: TCP
        volumeMounts:
          - name: config-site2
            mountPath: /etc/nginx/
      volumes:
        - name: config-site1
          configMap:
            name: nginx-config-site1
            items:
              - key: nginx.conf
                path: nginx.conf
        - name: config-site2
          configMap:
            name: nginx-config-site2
            items:
              - key: nginx.conf
                path: nginx.conf
```

### Anything else we need to know?

_No response_

### Kubernetes version

<details>

```console
$ kubectl version
Warning: Use tokens from the TokenRequest API or manually created secret-based tokens instead of auto-generated secret-based tokens.
Client Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.5", GitCommit:"aea7bbadd2fc0cd689de94a54e5b7b758869d691", GitTreeState:"clean", BuildDate:"2021-09-15T21:10:45Z", GoVersion:"go1.16.8", Compiler:"gc
", Platform:"windows/amd64"}
Server Version: version.Info{Major:"1", Minor:"32", GitVersion:"v1.32.0", GitCommit:"70d3cc986aa8221cd1dfb1121852688902d3bf53", GitTreeState:"clean", BuildDate:"2024-12-11T17:59:15Z", GoVersion:"go1.23.3", Compiler:"gc
", Platform:"linux/amd64"}
WARNING: version difference between client (1.21) and server (1.32) exceeds the supported minor version skew of +/-1
```

</details>


### Cloud provider

<details>
None, an on Premise setup
</details>


### OS version

<details>

```console
# On Linux:
$ cat /etc/os-release
PRETTY_NAME="Ubuntu 24.04.1 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04.1 LTS (Noble Numbat)"
VERSION_CODENAME=noble
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=noble
LOGO=ubuntu-logo
$ uname -a
Linux bmk8s-hermes 6.8.0-51-generic #52-Ubuntu SMP PREEMPT_DYNAMIC Thu Dec 5 13:09:44 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

# On Windows:
C:\> wmic os get Caption, Version, BuildNumber, OSArchitecture
# paste output here
```

</details>


### Install tools

<details>

</details>


### Container runtime (CRI) and version (if applicable)

<details>

</details>


### Related plugins (CNI, CSI, ...) and versions (if applicable)

<details>

</details>