Skip to content

LoadBalancer service type in Kubernetes

A LoadBalancer is a type of Kubernetes Service that exposes the service externally using a cloud provider's load balancer.^[400-devops__06-Kubernetes__k8s-ithelp__Day14__README.md] It allows applications running within a cluster to be accessible from outside the cluster network, typically by assigning an external IP address or a DNS name to the service.^[400-devops__06-Kubernetes__k8s-ithelp__Day14__README.md]

Mechanism

When a Service is defined with type: LoadBalancer, Kubernetes orchestrates the creation of an external load balancer through the integration with the underlying cloud infrastructure (e.g., AWS ELB, GCP Load Balancer, Azure Load Balancer).^[400-devops__06-Kubernetes__k8s-ithelp__Day14__README.md] This resource automatically routes external traffic to the backend Pods selected by the Service.

Configuration

In a manifest file, the Service type is specified under the spec section.^[400-devops__06-Kubernetes__k8s-ithelp__Day14__README.md]

apiVersion: v1
kind: Service
metadata:
  name: blue-green-service
spec:
  selector:
    app: my-app
    version: v1
  type: LoadBalancer
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080

Attributes

  • selector: Defines the label query that determines which Pods receive the traffic.^[400-devops__06-Kubernetes__k8s-ithelp__Day14__README.md]
  • type: Must be set to LoadBalancer to provision an external IP.^[400-devops__06-Kubernetes__k8s-ithelp__Day14__README.md]
  • ports: Specifies the mapping between the external port (port) and the port the container listens on (targetPort).^[400-devops__06-Kubernetes__k8s-ithelp__Day14__README.md]

Usage in Deployment Strategies

LoadBalancer Services are instrumental in implementing advanced deployment strategies, such as Blue/Green Deployment.^[400-devops__06-Kubernetes__k8s-ithelp__Day14__README.md] By acting as the stable entry point for external traffic, the LoadBalancer allows the underlying Pods (versions) to be swapped out by simply updating the Service's selector.^[400-devops__06-Kubernetes__k8s-ithelp__Day14__README.md]

External IP and Port Mapping

Upon creation, the cloud controller assigns an external IP address.^[400-devops__06-Kubernetes__k8s-ithelp__Day14__README.md] For example, a service might show LoadBalancer Ingress: localhost or a specific public IP, with a port mapping such as 8080:30138/TCP, forwarding traffic from the external port to the Service's port.^[400-devops__06-Kubernetes__k8s-ithelp__Day14__README.md]

Sources