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.
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.
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.
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.
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 ).
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.
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. |
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With