Updated April 1, 2023
Introduction to Kubernetes NodePort
The Kubernetes nodeport is defined as; it is the simple process to acquire the external traffic on our service, in which we can say that it is an open port that can be used on every node of our cluster in Kubernetes if the application is running on other nodes then also the Kubernetes can forward the incoming traffic on the nodeport of our service, the name nodeport has been inferred as it opens a particular nodeport on all the nodes and it can able to send the port for forwarding the service, and the node port can reveal the service on a cluster of internal IP.
Kubernetes NodePort overview
The nodeport is an open port that can be available on every node of our cluster in which the Kubernetes can undoubtedly convert the traffic on the nodeport even if our application may run on a different node; the nodeport has been assisted by every cluster of the Kubernetes but if we are running in a cloud provider then and then only we need to edit our firewall rules to avail the service, the by default range of the nodeport is in between 30000-32767 in which the nodeport can vulnerably match a service intended port to the given service, the administrator makes sure that the external IP can be converted to the node of and there were some local firewalls which can allow the nodeport to get access into the open port, in which we can say that the node port and external IPs are self-supporting and they both can be used simultaneously.
Using nodeport
It has been used to reveal the service node port on all nodes in the cluster; when we try to generate a service, then we can describe the port number to the port number; if we do not want to define that manually, then the system will assign one service to us,
- To make use of the nodeport, we need to have some added port assets.
- The nodeport has been used to get the traffic into the cluster of Kubernetes in our service, in which we can say that it takes traffic requests and that is transferred to the node port in our working node.
- The administrator assured us that the external IP had been converted to the node, and to make use of it, we need to change the setting of local firewalls that allow us to get access to the open port.
- The nodeport has been allowing us to access the application.
Configuring the service
We can use the service by specifying the port number while trying to generate the service; there are some configurations we need to follow for configuration, let’s see it,
- For the configuration, we have to log in to the master node.
- If we want to create a new project, then we need to run the command as given below,
'$ oc new-project <project_name>'
- For using the service, we also need to specify the ‘spec’ type to the nodeport.
- We have to run the command for creating the service,
'$ oc new-app <file-name>'
- If we want to see the services which are created then we have to run the command as given below,
'oc get svc'
- We have to keep in mind that the external IP we will get will be the nodeport; we can access the service using a node address.
Type nodeport
The ‘type’ is an extension that can be used before nodeport of any cluster type, and the service of it has the clusterIP address; the load balancer type has been there as an extension type for the nodeport type.
Suppose we set a ‘type’ field to nodeport. In that case, it means that the Kubernetes can able to manage a port from a range which has been provided by its port range flag which we have seen that it is by default 30000-32767, every node has the same port number on our service, if we use the nodeport which means that it provides the privilege due to that, we can able to load the balancing solution, and if we want a particular port number, then we have to specify the value in the field.
Rules
There are some rules which have been given below,
- The Kubernetes can manage a level of port in the range starting from 30000 to 32767 when we set a ‘type’ field to the nodeport.
- Each node can substitute the same port number on every node of our service.
- If any process runs on the port as a server, then the node port will do not stop its work; it means the work will continue, not only using that as a client.
- If any process started by using the nodeport, then because of the pre-routing rule, it ensures that the traffic transferred to the node gets converted to the pods.
Examples of Kubernetes NodePort
Different examples are mentioned below:
Example #1
apiVersion: v2
kind: service
metadata:
name:
labels:
:
spec:
selector:
:
type: NodePort
ports:
-port: <8080>
#nodePort: <31615>
In this example, we have to use the name ‘our-nodeport-service,’ which can be replaced with service; the labels have been replaced with ‘our-label-value’ so that can be used in our service, the selector has been used to associate with the service development.
Example #2
kind: Service
metadata:
name: my-service
spec:
type: nodePort
selector:
app: MyApp
ports:
- port: 81
targetPort: 81
nodePort: 30008
In this example, we have set the type in which we have given the ‘MyApp’ name to the ‘app,’ and the nodeport is 30008.
Conclusion
In this article, we conclude that the node port is an open port that can allow converting the traffic from one node to another node of clusters in Kubernetes; we have also seen the use of it, types of it, rules for it, so this article will help us to understand the concept in details.
Recommended Articles
This is a guide to Kubernetes NodePort. Here we discuss the node port as an open port that can allow converting the traffic from one node to another node of clusters in Kubernetes. You may also have a look at the following articles to learn more –