liveBook · Manning

Load balancing

As explained in Kubernetes in Action, Second Edition

Load balancing is the distribution of incoming work across multiple application instances or other processing targets.

Load balancing distributes incoming work across multiple application instances. In Kubernetes, a Service distributes requests among the pods backing it. This lets an application use additional replicas without making clients choose a pod themselves. section 1.2.1 section 3.3.3

See requests reach different pods

Run `kubectl scale deployment kiada --replicas=3`, then check `kubectl get deploy` for three ready replicas. Send several requests to the Service URL. The responses identify different pod names, in no fixed order. The application must still support horizontal scaling; Kubernetes can replicate it, but cannot make it scalable. section 3.3.3

Do not confuse the Service’s internal load balancing with an external load balancer. A Service gives the pods one stable cluster IP and distributes traffic among them. A Service of type `LoadBalancer` additionally asks supported infrastructure for a load balancer that clients can reach externally. That load balancer forwards connections to healthy nodes; the Service distributes the resulting traffic among pods. section 3.4 section 11.2.2

Listing 11.4 A LoadBalancer-type service
apiVersion: v1
kind: Service
metadata:
  name: kiada
spec:
  type: LoadBalancer     #1
  selector:
    app: kiada
  ports:
  - name: http
    port: 80
    nodePort: 30080
    targetPort: 8080
  - name: https
    port: 443
    nodePort: 30443
    targetPort: 8443

This Service manifest sets `type: LoadBalancer` while retaining a pod selector and port mappings. Apply it, then inspect the Service’s external IP with `kubectl get svc kiada`. section 11.2.2

A `LoadBalancer` Service depends on cluster support. Cloud clusters commonly provide it; an on-premises cluster may need an add-on. Without support, the Service can still be created but is accessible only through its node ports. For HTTP traffic routed to backend services through an Ingress, a separate L7 load balancer or reverse proxy performs the routing. section 11.2.2 section 12.1

How Kubernetes in Action, Second Edition covers it

How deep it goes
Works through a three-pod request example and distinguishes internal Service balancing from external node balancing without detailing balancing algorithms.
Examples
Uses a three-replica scaling command, responses naming different pods, and a LoadBalancer Service manifest.
What it assumes
Introduces Service-based balancing through observable requests while assuming an application deployed as pods and reachable through a Service.
What you can do after
Operate/configure it with kubectl and a Service manifest, then distinguish pod distribution from external node distribution.
How it connects
Connects load balancing to horizontal scaling, Deployments, pods, Services, NodePort, and Ingress.

Also explained in

Compare how 14 books explain load balancing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kubernetes in Action, Second Edition. The section links point to the parts of the book it draws on.