What is the principle of the nodeport service in k8s?

In Kubernetes, a Service is an abstraction layer that decouples the logical network endpoints of an application from the underlying infrastructure. Service NodePort is a type of Service that allows external networks to access the service through a specified port on any node in the cluster.

The principle of Service NodePort is as follows:

  1. When you create a Service in a Kubernetes cluster and set its type to NodePort, Kubernetes will assign a random port (NodePort) to that Service, typically within the range of 30000-32767.
  2. Kubernetes will automatically configure a network rule for each node in the cluster to listen to that NodePort.
  3. When external traffic accesses a Service through any node’s NodePort, that node forwards the traffic to the backend Pod of the Service. This is achieved through iptables rules or IPVS.
  4. The Service will create a Cluster IP, which acts as a virtual IP address for the Service to be used internally within the cluster. When a node receives traffic and forwards it to the backend pods of the Service, it will use this virtual IP address.

In summary, the principle behind Service NodePort is to configure network rules to listen for NodePort on each node, redirecting external traffic to the backend pods of the Service. This allows external networks to access the Service through a specified port on any node in the cluster.

bannerAds