Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between NodePort and LoadBalancer?

Tags:

kubernetes

I have just started with Kubernetes and I am confused about the difference between NodePort and LoadBalancer type of service.

The difference I understand is that LoadBalancer does not support UDP but apart from that whenever we create a service either Nodeport or Loadbalancer we get a service IP and port, a NodePort, and endpoints.

From Kubernetes docs:

NodePort: on top of having a cluster-internal IP, expose the service on a port on each node of the cluster (the same port on each node). You'll be able to contact the service on any NodeIP:NodePort address.

LoadBalancer: on top of having a cluster-internal IP and exposing service on a NodePort also, ask the cloud provider for a load balancer which forwards to the Service exposed as a NodeIP:NodePort for each Node.

So, I will always access service on NodeIP:NodePort. My understanding is, whenever we access the node:NodePort, the kubeproxy will intercept the request and forward it to the respective pod.

The other thing mentioned about LoadBalancer is that we can have an external LB which will LB between the Nodes. What prevents us to put a LB for services created as nodeport?

I am really confused. Most of the docs or tutorials talk only about LoadBalancer service therefore I couldn't find much on internet.

like image 209
Jardanian Avatar asked Dec 23 '15 20:12

Jardanian


People also ask

What is difference between LoadBalancer and ingress?

The main difference is ingresses are native objects inside the cluster that can route to multiple services, while load balancers are external to the cluster and only route to a single service.

What is the difference between NodePort and cluster IP?

NodePort definitions have the same mandatory properties as ClusterIP services. The only difference is the change to type: NodePort . The targetPort field is still required, as NodePorts are backed by a ClusterIP service. This will route traffic on port 32000 to port 80 in your Pods.

What is NodePort used for?

Overview. Use NodePorts to expose the service nodePort on all nodes in the cluster. Using NodePorts requires additional port resources. A node port exposes the service on a static port on the node IP address.

What is the difference between NodePort and ClusterIP in Kubernetes?

ClusterIP : Exposes the Service on a cluster-internal IP. Choosing this value makes the Service only reachable from within the cluster. This is the default ServiceType . NodePort : Exposes the Service on each Node's IP at a static port (the NodePort ).


2 Answers

Nothing prevents you from placing an external load balancer in front of your nodes and use the NodePort option.

The LoadBalancer option is only used to additionally ask your cloud provider for a new software LB instance, automatically in the background.

I'm not up to date which cloud providers are supported yet, but i saw it working for Compute Engine and OpenStack already.

like image 80
Zahlex Avatar answered Oct 20 '22 00:10

Zahlex


Difference between Node port and Load Balancer services.

Node Port Load balancer
By creating a NodePort service, you are saying to Kubernetes reserve a port on all its nodes and forwards incoming connections to the pods that are part of the service. There is no such port reserve with Load balancer on each node in the cluster.
NodePort service can be accessed not only through the service’s internal cluster IP, but also through any node’s IP and the reserved node port. Only accessible by Load balancer public IP
Specifying the port isn’t mandatory. Kubernetes will choose a random port if you omit it( default range 30000 - 32767). Load balancer will have its own unique, publicly accessible IP address and will redirect all connections to your service
If you only point your clients to the first node, when that node fails, your clients can’t access the service anymore With Load balancer in front of the nodes to make sure you’re spreading requests across all healthy nodes and never sending them to a node that’s offline at that moment.
like image 35
Gupta Avatar answered Oct 20 '22 00:10

Gupta