Skip to content
Related Articles
Open in App
Not now

Related Articles

Kubernetes – NodePort Service

Improve Article
Save Article
  • Last Updated : 10 Nov, 2022
Improve Article
Save Article

Pre-requisite: Kubernetes

In Kubernetes, there are 3 types of services to provide discovery and routing between pods.

1. ClusterIP: It is the default service and its visibility is cluster internal which means it’s not possible to use clusterIP service to reach a micro-service from the internet from outside the cluster.

2. NodePort:  NodePort extends the ClusterIP service and its visibility is internal and external to the cluster. You can set a NodePort using the NodePort property, this is the port that the service will listen on from outside the cluster. There is one requirement of using NodePort which is nodes must have public IP addresses. The port must be in the range between 30000 and 32767 and if you don’t specify the NodePort value Kubernetes will assign it randomly.

3. LoadBalancer: It operates at the transport level (TCP) that is at level 4. It means that is unable to make decisions based on content and it uses a simple algorithm such as a round-robin across the selected paths. Whereas Ingress operates at the application level which is at level 7. It is able to make decisions based on the actual content of each message. More intelligent load-balancing decisions and content optimizations. In other words, Ingress is like a LoadBalancer but more intelligent.

For our article, we’ll use NodePort service for service discovery.

Implementation:

Step 1. Create a deployment. For our demo purpose, I choose Nginx as our application with 2 replicas, you can change it as per your requirement.

$ kubectl create deploy 
--image=nginx:1.23 nginx 
--replicas=2
create deployment

 

Step 2. Expose service with NodePort

kubectl expose deploy nginx 
--type=NodePort --port=8080 
-target-port=80

we exposed our deployment as a NodePort service with the name nginx-service

expose deployment

 

Check your service (Optional):

$ kubectl get svc nginx
-service -o wide
service check

 

You can see we’ve successfully exposed our service with NodePort

Step 3. Access your application

$ minikube service 
nginx-service --url
accessing application

 

On invoking this, you can see it in the browser:

output

 

As you can see in the above image we can access our application on our machine which is running on our cluster, this is only possible because of the NodePort service.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!