Skip to content

[Enhancement]: Service definitions use named ports rather than port numbers #11393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jcpunk opened this issue Apr 30, 2025 · 6 comments
Open

Comments

@jcpunk
Copy link
Contributor

jcpunk commented Apr 30, 2025

Related problem

Current Kubernetes best practices suggest use of named ports for connecting services to containers rather than port numbers. Currently the created services use explicit port numbers when connecting to the generated containers.

Suggested solution

Use the assigned port names in the created service definitions.

Alternatives

It currently works.

Additional context

from the container:

      name: kafka
      ports:
      - containerPort: 9090
        name: tcp-ctrlplane
        protocol: TCP
      - containerPort: 9091
        name: tcp-replication
        protocol: TCP
      - containerPort: 9092
        name: tcp-clients
        protocol: TCP
      - containerPort: 9093
        name: tcp-clientstls
        protocol: TCP
      - containerPort: 9094
        name: tcp-external
        protocol: TCP
      - containerPort: 9404
        name: tcp-prometheus

from a service:

    ports:
    - name: tcp-ctrlplane
      port: 9090
      protocol: TCP
      targetPort: 9090   ### change this
    - name: tcp-replication
      port: 9091
      protocol: TCP
      targetPort: 9091   ### change this
    - name: tcp-kafkaagent
      port: 8443
      protocol: TCP
      targetPort: 8443   ### change this
    - name: tcp-clients
      port: 9092
      protocol: TCP
      targetPort: 9092   ### change this
    - name: tcp-clientstls
      port: 9093
      protocol: TCP
      targetPort: 9093   ### change this
@scholzj
Copy link
Member

scholzj commented Apr 30, 2025

@jcpunk Out of curiosity ... could please share where did you find this recommendation? What is the expected advantage? Thanks

@jcpunk
Copy link
Contributor Author

jcpunk commented Apr 30, 2025

Ideally, this would prevent hard coding port mappings. In practice the kafka ports are pretty static and well known, but use of names means, if at some point you need to fiddle with the mappings simply altering the pod's listed export port is all that is required.

Essentially, using the numbers means storing the numbers twice and needing to manually be sure they match up.

I'll need to retrace my steps to find where I found this recommendation. I'd swear it was under https://docs.kubelinter.io/#/generated/checks but I'm not seeing it now...

@scholzj
Copy link
Member

scholzj commented Apr 30, 2025

Interesting. Not sure this really matters that much when you have an operator generate Kubernetes resources. Also, in our case the names are basically meaning less for most people reading it, but so are the port numbers. Thanks for the explanation.

Hopefully, all the various tools out there expect this and are ready to handle it so that there are no ugly surprises if it is done 🤔.

@jcpunk
Copy link
Contributor Author

jcpunk commented Apr 30, 2025

Feature wise, use of port names has been stable for all the versions of kubernetes you support. portNames were added way back in the past.

@im-konge
Copy link
Member

Triaged on 29.5.2025: As you mentioned in the example, the names of the ports are also mentioned in the name field of the particular port of the Service. But anyway, we can keep it opened and implement it.

@KyriosGN0
Copy link
Contributor

I will try to implement it in the following days

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants