[Kubernetes] NodePort and LoadBalancer Service

1 minute read


NodePort and LoadBalancer Service

  • Relation between the Services
  • NodePort
  • LoadBalancer

Relation between the Services

  • The below three service types are additive, meaning that each one createes the one above it
    • ClusterIP
    • NodePort
    • LoadBalancer
    • i.e. When a NodePort is created a ClusterIP is created as well

NodePort

  • Takes the connection coming into the high port(in the example, 30294), and then it redirects it to the cluster IP for that service(which is why Nodeport service requires ClusterIP service).
# create nodeport
kubectl expose deployment/httpenv --port 8888 --name httpenv-np --type NodePort
kubectl get services
# left is container, right is node(opposite of Docker)
#$ default range of nodeports are 30000~32767
# httpenv     ClusterIP   10.104.2.205   <none>   8888/TCP         21m
#httpenv-np   NodePort    10.98.140.22   <none>   8888:30294/TCP   88s

# check connection
curl localhost:30294

LoadBalancer

  • LoadBalancer accepts the packet, passes it to the NodePort, which is then passed to the ClusterIP
# works only in Docker Desktop
kubectl expose deploy/httpenv --port 8888 --name httpenv-lb --type LoadBalancer
kubectl get services
# httpenv      ClusterIP      10.104.2.205   <none>     8888/TCP         21m
# httpenv-lb   LoadBalancer   10.99.69.133   localhost  8888:31513/TCP   18s
# httpenv-np   NodePort       10.98.140.22   <none>     8888:30294/TCP   12m
# the Nodeport(31513) always shows the Nodeport, even though that's really not the port the LoadBalancer is using on the localhost

# check connection
curl localhost:8888

# delete services
kubectl delete service httpenv-lb
kubectl delete service httpenv-np