Skip to content

Service load balancing behavior

In Kubernetes, a Service acts as an abstraction layer to provide a stable network endpoint for a dynamic set of [[Pods]]. To achieve this, it distributes network traffic to the backend Pods using a load balancing mechanism.^[400-devops-06-kubernetes-k8s-learning-05service-service.md]

The load balancing behavior is random by default. When traffic hits the Service's ClusterIP (the virtual IP assigned to the service), the Service proxy selects a target Pod from the healthy backend pool to handle the request.^[400-devops-06-kubernetes-k8s-learning-05service-service.md]

Demonstration

To verify this behavior, a test environment can be established where three separate Pods return unique content (e.g., "111", "222", and "333").^[400-devops-06-kubernetes-k8s-learning-05service-service.md] Exposing this deployment creates a Service with a single entry point (Cluster IP).^[400-devops-06-kubernetes-k8s-learning-05service-service.md]

When repeatedly sending requests (via curl) to the Service's IP and port, the response content changes each time.^[400-devops-06-kubernetes-k8s-learning-05service-service.md] This demonstrates that the Service is distributing the traffic sequentially across the different backend Pods rather than routing all traffic to a single instance.^[400-devops-06-kubernetes-k8s-learning-05service-service.md]

Accessing the Service

Services can be accessed via different mechanisms depending on the network configuration:

  • ClusterIP (Internal): Clients within the cluster (such as other Pods) can reach the Service using its name (DNS) or IP address. For example, curl nginx-deployment-service:8080 or curl nginx-deployment-service.default.svc:8080 will be load balanced to the backend Pods.^[400-devops-06-kubernetes-k8s-learning-05service-service.md]
  • NodePort (External): By setting the Service type to NodePort, the Service can be accessed from outside the cluster via the node's IP address and a specific high port. Load balancing behavior persists for these external connections as well.^[400-devops-06-kubernetes-k8s-learning-05service-service.md]

Sources

  • 400-devops-06-kubernetes-k8s-learning-05service-service.md