Kubernetes LoadBalancer service with hostNetwork binding

7/16/2019

I have a query regarding the usage of a LoadBalancer service with hostNetwork

If we set hostNetwork: true, then the pods bind on the host network - to which the external services connect to. If we need only one instance of the pod running - then I believe we do not need a LoadBalancer service for the external services to connect to the pod. I do not see any use-case for a a LoadBalancer service here, or are there any I am missing ?

-- Chillax
kubernetes
kubernetes-service

1 Answer

7/16/2019

hostNetwork=true is not the recommended approach for exposing pods outside of the cluster. It has a few limitations:

  1. Only 1 instance of a pod can run on a specific node on the same port
  2. You have to use the nodeIP to access the pod, however, the node IP can change.
  3. If the pod fails, the k8s scheduler may spawn it on a different node.

The recommended way for exposing pods outside of the cluster is via Kubernetes Service Controllers. All service controllers act as load balancers (they will balance the traffic across all "ready" pods) no matter the Service.spec.type property. Service.spec.type property can be one of the below:

ClusterIP, NodePort, LoadBalancer, ExternalName

The LoadBalancer type means that k8s will use a cloud provider LoadBalancer to expose the service outside of the cluster (for example AWS Elastic Load balancer if the k8s cluster is running on AWS).

LoadBalancer: Exposes the Service externally using a cloud provider’s load balancer. NodePort and ClusterIP Services, to which the external load balancer routes, are automatically created.

More on k8s service types

-- cecunami
Source: StackOverflow