Skip to content

Show load balancing

View as Markdown

Each page shows the pod and the node that answered. With several pods, you can watch a Service spread requests across them. This guide installs four pods, sees where they run, and sends requests to them from inside the cluster.

You need kubectl, Helm 4, and a cluster. With more than one node, the pods spread across them.

Terminal
helm install spread oci://ghcr.io/paulbouwer/charts/hello-kubernetes \
--namespace hello-lb --create-namespace \
--set deployment.replicaCount=4 \
--set service.type=ClusterIP \
--wait
kubectl get pods --namespace hello-lb --output wide

The NODE column shows where each pod runs. The scheduler spreads them across nodes where it can.

Run a short-lived pod that asks the Service for the page 10 times, and prints the pod that answered each time:

Terminal
kubectl run lb-test --namespace hello-lb --rm -i --quiet --restart=Never --image=busybox:1.37 -- \
sh -c 'for i in $(seq 1 10); do wget -qO- http://hello-kubernetes-spread | grep -A1 "pod:" | tail -n1; done'
<td>hello-kubernetes-spread-7798544b7b-fvsmz</td>
<td>hello-kubernetes-spread-7798544b7b-s9bfh</td>
<td>hello-kubernetes-spread-7798544b7b-fvsmz</td>
<td>hello-kubernetes-spread-7798544b7b-lqzrb</td>
...

Each request opens a new connection, and the Service sends each connection to one of the pods.

Reach the app through a LoadBalancer Service, an Ingress or a Gateway, and refresh the page. The pod name changes, but not on every refresh. A browser keeps its connection open, and the Service balances connections, not requests. An Ingress controller or a Gateway balances each request, so the change is easier to see through one.

kubectl port-forward always connects to one pod, so the name never changes through it.

Terminal
helm uninstall spread --namespace hello-lb
kubectl delete namespace hello-lb