Kubernetes Services - LoadBalancer

In the last article we studied about NodePort type of service. It is good to use but not as much flexible as it has a fixed port range. i.e. 30000 - 32767. Now next comes the LoadBalancer type of service which is really awesome.

Now one main thing to be noted here is that if you are hosting your cluster on virtual machines, then unfortunately it is not so easy to use a load balancer service.

LoadBalancer service type is really easy to use if we are hosting our kubernetes cluster on any of the cloud provider such as AWS/Azure/GCP. Now what happens on back end is that when we create a new service of the type LoadBalancer, the cloud provider automatically provisions a load balancer and assigns a public IP for the service.

We can easily access the service through the web browser without any port-forwarding or the use of kube-proxy.

To demonstrate the same we will create a kubernetes cluster in GCP (GKE) and see how LoadBalancer type of service works.

First let us create a pod that will provide the service

gcpconsole7138@blog-1:~/test$ cat ngin.yaml 
apiVersion: v1
kind: Pod
   name: nginx-pod
     app: myapp
   - name: nginx-container
     image: nginx
     ## Execute kubectl apply -f ngin.yaml
     to create a pod

Now let us create a service of the type LoadBalancer using the above pod.

gcpconsole7138@blog-1:~/test$ cat lb.yaml 
apiVersion: v1
kind: Service
  name: example-service
    app: myapp
    - port: 80
      targetPort: 9376
  type: LoadBalancer
  ## Execute the command kubectl create -f lb.yaml to create the service

Now when you open the IP address in the web browser will display the nginx web page.

251 views0 comments

Subscribe Form

©2020 by Linux Advise