kubernetes

How to create LoadBalancer service in Kubernetes?

Let’s understand what contents make a LoadBalancer service and what exactly it means.

apiVersion: v1
kind: Service
metadata:
  name: <service_name>
  labels:
    app: <service_label_name>
spec:
  type: LoadBalancer
  ports:
  - protocol: TCP
    port: 8000
    name: http
    targetPort: 8000
  selector:
    app: <value_name>
  • apiVersion represents from which group Kind belongs.
  • kind represents the type of Kubernetes objects.
  • metadata defines the details of the service, such as, what label to be added, what should be the name of the service, etc.
  • spec defines the specification for the service.
    • The service type is defined by type.
      • LoadBalancer – Exposes the Service externally using a cloud provider’s load balancer. It is also called an upgradation of the NodePort service.
    • ports, define the port on which communication establish.
      • protocol, define which type of protocol wants to use.
      • port, define for cluster mode access.
      • targetPort, defines the container port, on which the application is running.
      • name, it is unique to the port definition. You can add more port definitions in the service file.
    • selector, which is in charge of selecting all pods whose values are defined in the key(app).

LoadBalancer also uses nodePort so the application can also be accessible through the nodePort service port. You can get the port by describing the service. You can also add the nodePort field in the service file.

To create the service in the Kubernetes cluster run the below command

kubectl create -f <service.yaml>

MtroView team is giving its best & working hard to provide the content to help you understand things better in simple terms. We are heartily open to receiving suggestions to improve our content to help more people in society. Please write to us in the form of a comment or can also send an email to mtroview@gmail.com

Leave a Comment

Your email address will not be published. Required fields are marked *