Mastodon

Port, TargetPort, and NodePort in Kubernetes

Pod ports list

The pod.spec.containers[].ports list provides, unsurprisingly, a list of ports that are exposed by the container. As long as the container itself is listening on the given ports, this list mostly serves to provide additional information to Kubernetes itself.

Service ports list

service.spec.ports configures which requests to a service port get forwarded to which ports on its pods. A request can be made from outside of the cluster to the node’s IP address and the service’s nodePort and forwarded to the service’s port.

kind: Service
apiVersion: v1
metadata:
name: port-example-svc
spec:
# Make the service externally visible via the node
type: NodePort
ports:
# Which port on the node is the service available through?
- nodePort: 31234
# Inside the cluster, what port does the service expose?
- port: 8080
selector:
# ...

nodePort Specifies the port which exposes the service outside of the the cluster. The service also has to be of type NodePort (if this field isn’t specified, Kubernetes will allocate a node port automatically). port Specifies the port which exposes the service inside of the cluster. Requests made to the service will be sent to pods selected by the service on this port.

References

Port, TargetPort, and NodePort - Kubernetes Book #kubernetes

Tags: kubernetes