Is it possible to select the port on a multiport service in kubernetes using the port name?

4/1/2021

I have two pods running in the same namespace.

One of then can be accessed trough the following multi-port service:

kind: Service
apiVersion: v1
metadata:
  name: bacon-service
spec:
  selector:
    app: bacon-app
    restype: pod
  ports:
  - name: http
    port: 80
    targetPort: 8001
    protocol: TCP
  - name: http2
    port: 8787
    targetPort: 8787
    protocol: TCP
  type: ClusterIP

And the second needs to access the http2 port of the first pod to make gRPC transactions.

I've been using the port number to access it, which has been working just fine.

http://bacon-app:8787

Yet I think a clever implementation would be to use the port name. So if by any change someday someone decides to change the service port, the client pod would not be affected.

I've tried to achieve so with the following:

http://bacon-app:http2

Which simply does not work, would it be possible to do it somehow?

-- Rogerio Schmitt
grpc
kubernetes

1 Answer

4/1/2021

No, sorry, but it's not possible, as this is pure http routing and has nothing to do with Kubernetes. Browsers / http clients only understand <scheme>://<host>[:port]

-- DevLounge
Source: StackOverflow