Skip to content

Kubernetes service types (LoadBalancer and NodePort)

Kubernetes Service is an abstraction layer that defines how a group of [[Pods]] are accessed and connected^[400-devops__06-Kubernetes__k8s-ithelp__Day7__README.md]. Unlike direct methods like port-forward which break when Pods are recreated, Services use Labels and Selectors to dynamically track Pods, ensuring continuous exposure even as Pods are created or destroyed^[400-devops__06-Kubernetes__k8s-ithelp__Day7__README.md].

Configuration Fields

A Service definition includes several key fields used to configure networking and port mapping^[400-devops__06-Kubernetes__k8s-ithelp__Day7__README.md]:

  • spec.type: Determines the Service type, such as NodePort or LoadBalancer.
  • spec.selector: Filters which Pods receive the traffic based on their labels.
  • spec.ports.protocol: Supports TCP, SCTP, and UDP, defaulting to TCP.
  • spec.ports.port: The port exposed on the Service's Cluster IP.
  • spec.ports.targetPort: The port on the Pod (container) to which traffic is forwarded.
  • spec.ports.nodePort: The port exposed on the Node itself (for NodePort/LoadBalancer).

LoadBalancer

The LoadBalancer type provisions an external load balancer to manage traffic, integrating with third-party cloud providers (e.g., AWS, Google Cloud) or local environments like Docker Desktop^[400-devops__06-Kubernetes__k8s-ithelp__Day7__README.md].

In a local development environment, the LoadBalancer type simplifies access by automatically routing the External IP to localhost, removing the need for complex manual configuration^[400-devops__06-Kubernetes__k8s-ithelp__Day7__README.md].

NodePort

The NodePort type involves specific port mappings defined in the Service configuration^[400-devops__06-Kubernetes__k8s-ithelp__Day7__README.md].

  • Explicit Mapping: You can specify a nodePort value in the configuration to map a specific Node port number to the targetPort^[400-devops__06-Kubernetes__k8s-ithelp__Day7__README.md].
  • Automatic Assignment: If no nodePort is specified, Kubernetes will automatically select and assign a random available port number^[400-devops__06-Kubernetes__k8s-ithelp__Day7__README.md].

Sources

^[400-devops__06-Kubernetes__k8s-ithelp__Day7__README.md]