How to make use of Kubernetes port names? How to make use of Kubernetes port names? kubernetes kubernetes

How to make use of Kubernetes port names?


No. You can't use port name instead of port number. Name field in ServicePort has different purpose.

All ports within a ServiceSpec must have unique names. This name maps to the 'Name' field in EndpointPort objects.

For each Service, one Endpoint object is generated. Every port of that Endpoint corresponds to a Service port. Name field in both ServicePort and EndpointPort is used for this matching.


Usually, you refer to a target port by its number. But you can give a specific name to each pod`s port and refer to this name in your service specification.

This will make your service clearer. Here a small example:

apiVersion: v1kind: Podmetadata:  name: named-port-pod  labels:    app: named-port-podspec:  containers:    - name: echoserver      image: gcr.io/google_containers/echoserver:1.4      ports:      - name: pod-custom-port        containerPort: 8080---apiVersion: v1kind: Servicemetadata:  name: named-port-svcspec:  ports:    - port: 80      targetPort: pod-custom-port  selector:    app: named-port-pod


kube-dns is a DNS service that uses the same protocol as all the regular DNS servers. In this sense, DNS was not designed to resolve "port names", but domain names (which map to an IP address).

What several people do is to have a reverse proxy that would ProxyPass traffic from one port to another (provided that we are talking about HTTP/HTTPS traffic) https://serverfault.com/questions/85078/how-to-forward-dns-alias-to-hostnameport or use iptables rules https://www.digitalocean.com/community/tutorials/how-to-forward-ports-through-a-linux-gateway-with-iptables. However, these assume that you would forward traffic to a specific port in the first place (for example, 80)

As you wrote in the comment, using environment variables would be the option that fits your case.