kuberneteskubernetes-service

Why does a Nodeport need a "port" in Kubernetes?


enter image description here

The nodeport takes in 3 parameters in the service yaml.

Since all you wanted was to map a port on the node(nodeport) to the port on the container(targetPort), why do we need to provide the port of the service?

Is this because Nodeport is internally implemented on top of ClusterIP?


Solution

  • "Nodeport is internally implemented on top of ClusterIP" - correct.

    The port in the Kubernetes Service definition is used to specify the port on which the service will listen for traffic within the Kubernetes cluster. This is the port that will be exposed to other pods in the cluster as an endpoint for the service. When a request is made to this port by a client within the cluster, the traffic will be routed to one of the pods selected by the Service based on its load balancing algorithm.

    The nodePort is used to expose the service on a port on the node itself, which allows the service to be accessed from outside the cluster.