What does the `port` property of a ServicePort object _mean_?

10/20/2017

According to https://kubernetes.io/docs/api-reference/v1.8/#serviceport-v1-core , a ServicePort's port (integer) is "The port that will be exposed by this service.", but given that it doesn't specify the port on which a backend pod will listen (those are targetPort and can even be names to be defined by each backend pod) and it doesn't really specify the port to which a frontend pod should connect (those are nodePort or auto-assigned), I'm confused as to what it does specify.

Are load balancers (ingresses?) required to expose the port as identified in a service, or can they map it as well?

The examples at https://kubernetes.io/docs/concepts/services-networking/service/ don't always include names for the ports even though the spec says that "All ports within a ServiceSpec must have unique names." -- this leads me to assume that a stringified port is also the default name. Is that right?

The docs for ServiceSpec mention that port is the "patch merge key" for its ports array. Are this and the previous observation about port being the default name just forensic evidence that port existed first but we wish we'd started with name? :)

-- Rob Starling
kubernetes

1 Answer

10/23/2017

The numeric port of a Service's ServicePort (in its spec) is used in the following places:

  • The Service is exposed at spec.clusterIP:spec.ports[*].port (unless clusterIP is set to "None")
  • The Service is exposed at spec.loadBalancerIP:spec.ports[*].port (if you specify type: LoadBalancer)
  • It is the default value for targetPort of that ServicePort
-- Rob Starling
Source: StackOverflow