If the Service is set as NodePort, Kubernetes will allocate a port within a certain range on each node. Any traffic going to the nodes on that port will be routed to the Service port. The port number may be user-specified. If not, Kubernetes will randomly choose a port between 30,000 and 32,767 that doesn't cause any collision. On the other hand, if it's specified, the user should be responsible for managing the collision by themselves. NodePort includes a ClusterIP feature. Kubernetes assigns an internal IP to the Service.
In the following example, we'll see how we can create a NodePort Service and use it:
// write a nodeport type service
# cat 3-2-3_nodeport.yaml
kind: Service
apiVersion: v1
metadata:
name: nginx-nodeport
spec:
type: NodePort
selector:
project: chapter3
service: web
ports:
- protocol: TCP
port: 80
targetPort: 80
// create a nodeport service
# kubectl create -f 3-2-3_nodeport.yaml
service "nginx-nodeport" created
You should then be able to access the Service via http://${NODE_IP}:80. The node could be any node. The kube-proxy watches for any updates by the Service and the endpoints, and updates the iptable rules accordingly (if using default iptables proxy-mode).