peter (admin)   -  



 

Kubernetes LoadBalancer Service

A LoadBalancer service is used in Kubernetes to expose an application to external users. Kubernetes attempts to assign an external IP address so traffic from outside the cluster can reach the service and then be forwarded to the backend pods.

Checking the Kubernetes Cluster

We are already logged into both the master node and the worker node.

First, check the status of the nodes:

kubectl get nodes
peter@k8s-master:~$ kubectl get nodes
NAME          STATUS   ROLES           AGE   VERSION
k8s-master    Ready    control-plane   40d   v1.35.8
k8s-worker1   Ready    <none>          40d   v1.35.8
peter@k8s-master:~$ 

Both the master node and worker node should be in the Ready state. This confirms that the cluster is healthy and that both nodes are active and capable of handling and forwarding traffic.

Creating a Namespace

Create a new namespace named deep to logically separate the resources from the default environment:

kubectl create namespace deep

Verify that the namespace has been created:

kubectl get namespaces
peter@k8s-master:~$ kubectl get ns
NAME              STATUS   AGE
deep              Active   56s
default           Active   40d
kube-flannel      Active   40d
kube-node-lease   Active   40d
kube-public       Active   40d
kube-system       Active   40d
peter@k8s-master:~$ 

The deep namespace should now be active and ready for use.

Creating the Web Application Deployment

Create a deployment named web-app inside the deep namespace using the NGINX image:

kubectl create deployment web-app --image=nginx -n deep

The deployment runs the application in a controlled and managed way inside the deep namespace.

Verify the deployment:

peter@k8s-master:~$ kubectl get deployments -n deep
kubectl get deployments -n deep NAME      READY   UP-TO-DATE   AVAILABLE   AGE
web-app   1/1     1            1           33s
peter@k8s-master:~$

The web-app deployment should show one available pod.

You can also check the running pods:

peter@k8s-master:~$ kubectl get pods  -n deep -o wide
NAME                       READY   STATUS    RESTARTS   AGE     IP            NODE          NOMINATED NODE   READINESS GATES
web-app-5d4dd47c87-9pnvn   1/1     Running   0          2m42s   10.244.1.93   k8s-worker1   <none>           <none>
peter@k8s-master:~$ 

The output shows the pod IP address and the node where the pod is running. This confirms that the application has been successfully scheduled and is running inside the cluster.

Creating a LoadBalancer Service

Next, expose the web-app deployment using a LoadBalancer service:

kubectl expose deployment web-app
  --type=LoadBalancer
  --port=80
  --target-port=80
  -n deep

The purpose of this service is to allow traffic coming from outside the cluster to reach the application and then be distributed to the backend pods running the web server.

Verify the service:

peter@k8s-master:~$ kubectl get service -n deep
NAME      TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
web-app   LoadBalancer   10.109.1.197   <pending>     80:30676/TCP   71s
peter@k8s-master:~$ 

 

The service should be shown as type LoadBalancer.

The output will include a ClusterIP, which is used for internal communication within the cluster. However, the External IP will initially show as:

<pending>

Why the External IP Is Pending

The pending status means Kubernetes is waiting for an external load balancer to provide an IP address.

In a cloud environment, a cloud provider such as AWS, Azure, or Google Cloud can automatically provision a load balancer and assign an external IP address.

However, this cluster is running locally, so there is no cloud load balancer available.

The ClusterIP can be used for communication inside the Kubernetes cluster, but it is not normally accessible from outside the cluster.

An external IP acts as an entry point for external traffic. When a user sends a request to this address, Kubernetes receives the request and forwards it to the appropriate service and then to the backend pods.

Installing MetalLB

To provide LoadBalancer functionality in a local Kubernetes environment, we can use MetalLB.

MetalLB acts as a software-based load balancer and allows Kubernetes services of type LoadBalancer to receive external IP addresses.

For testing purposes, a MetalLB configuration file can be applied from a GitHub-hosted manifest:

kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.16.1/config/manifests/metallb-native.yaml

When the manifest is applied, Kubernetes creates the resources required for MetalLB.

These include:

  • The metallb-system namespace

  • Custom Resource Definitions

  • Service accounts

  • Roles

  • Role bindings

  • Secrets

  • Services

  • Deployments

  • DaemonSets

  • Webhook configuration

Together, these components allow MetalLB to function as a software load balancer inside the local Kubernetes environment.

Once all resources have been successfully created, MetalLB is ready to assign external IP addresses to LoadBalancer services.

Checking MetalLB

Check the MetalLB pods:

peter@k8s-master:~$ kubectl get pods -n metallb-system
NAME                          READY   STATUS    RESTARTS   AGE
controller-867646bb46-qvsqd   1/1     Running   0          93s
speaker-mv6tt                 1/1     Running   0          93s
speaker-n2mq6                 1/1     Running   0          93s
peter@k8s-master:~$ 

The controller and speaker pods should be in the Running state.

This confirms that MetalLB has been successfully installed and is active.

Removing the MetalLB Validating Webhook

In a local lab environment, the MetalLB validating webhook can sometimes block or delay configuration changes if it is not fully ready or cannot validate requests correctly.

The webhook can therefore be removed in this lab setup:

kubectl delete validatingwebhookconfiguration metallb-webhook-configuration

Removing the webhook can avoid unnecessary configuration errors in a non-production environment.

Configuring the Firewall

The firewall must allow the Kubernetes network traffic required for communication between pods, services, and nodes.

First, allow the pod network range:

10.244.0.0/16

This allows communication between pods across the cluster.

Next, allow the Kubernetes service network:

10.96.0.0/12

This network is used by ClusterIP services for internal communication.

Flannel also requires UDP port 8472 for its overlay networking:

UDP 8472

This allows pods to communicate across different nodes.

TCP port 10250 should also be allowed:

TCP 10250

This port is used by the kubelet for communication with the control plane.

Finally, reload the firewall so that the new rules take effect.

Configuring the Worker Node Firewall

The same firewall configuration must be applied to the worker node however we implemented those rules in the previos section 

peter@k8s-master:~$ sudo ufw status
Status: active
To                         Action      From
--                         ------      ----
6443/tcp                   ALLOW       Anywhere                  
2379:2380/tcp              ALLOW       Anywhere                  
10250/tcp                  ALLOW       Anywhere                  
22/tcp                     ALLOW       Anywhere                  
0.0.0.0/1                  ALLOW       Anywhere                  
10.244.0.0/16              ALLOW       Anywhere                  
Anywhere                   ALLOW       10.244.0.0/16             
8472/udp                   ALLOW       Anywhere                  
30000:32767/tcp            ALLOW       Anywhere                  
30000:32767/udp            ALLOW       Anywhere                  
6443/tcp (v6)              ALLOW       Anywhere (v6)             
2379:2380/tcp (v6)         ALLOW       Anywhere (v6)             
10250/tcp (v6)             ALLOW       Anywhere (v6)             
22/tcp (v6)                ALLOW       Anywhere (v6)             
8472/udp (v6)              ALLOW       Anywhere (v6)             
30000:32767/tcp (v6)       ALLOW       Anywhere (v6)             
30000:32767/udp (v6)       ALLOW       Anywhere (v6)             
peter@k8s-master:~$

Kubernetes networking operates across multiple nodes, so configuring the firewall only on the master node is not sufficient.

The same rules should therefore be applied to the worker node to ensure that:

  • Pod-to-pod communication works.

  • Service traffic can flow correctly.

  • Flannel overlay networking works.

  • Pods on different nodes can communicate.

Creating the MetalLB IP Address Pool

MetalLB needs to know which IP addresses it is allowed to assign to LoadBalancer services.

Create an IP address pool using a YAML definition:

apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
  name: lab-pool
  namespace: metallb-system
spec:
  addresses:
    - 192.168.2.240-192.168.2.250

The pool is named lab-pool and is placed in the metallb-system namespace.

The configured address range is:

192.168.2.240 - 192.168.2.250

MetalLB can select an available address from this range and assign it to a LoadBalancer service.

In simple terms, this configuration tells MetalLB which local network IP addresses it is allowed to use as external IP addresses.

Apply the configuration:

peter@k8s-master:~$ kubectl apply -f IPAddressPool.yaml 
ipaddresspool.metallb.io/lab-pool created
peter@k8s-master:~$ 

The output should confirm that the IP address pool was successfully created.

Creating an L2 Advertisement

MetalLB also needs to know how to advertise the assigned IP addresses on the local network.

Create an L2 advertisement:

apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
  name: lab-advertisement
  namespace: metallb-system
spec:
  ipAddressPools:
    - lab-pool

This configuration links the advertisement to the lab-pool created earlier.

The L2 advertisement tells MetalLB to announce assigned IP addresses on the local network using Layer 2 networking so that other machines can reach them.

Apply the configuration:

peter@k8s-master:~$ kubectl apply -f L2Advertisement.yaml 
l2advertisement.metallb.io/lab-advertisement created
peter@k8s-master:~$ 

Once the advertisement has been successfully created, MetalLB is fully configured to assign and advertise external IP addresses for LoadBalancer services.

Verifying the External IP

Check the LoadBalancer service again:

kubectl get service -n deep

The web-app service should now have an external IP address, for example:

192.168.2.240

This confirms that MetalLB has selected an IP address from the configured pool and assigned it to the LoadBalancer service.

External users or applications can now use this IP address to access the web application.

Testing External Access

The application can be tested using a browser or curl:

curl 192.168.2.240

The request reaches the Kubernetes LoadBalancer service, which forwards the request to the backend NGINX pod.

The response should contain the default NGINX web page.

This confirms that external access through the assigned IP address is working.

Scaling the Application

To demonstrate how traffic is distributed between multiple pods, scale the deployment to two replicas:

kubectl scale deployment web-app --replicas=2 -n deep

Verify the pods:

peter@k8s-master:~$ kubectl get pods -n deep -o wide
NAME                       READY   STATUS    RESTARTS   AGE   IP            NODE          NOMINATED NODE   READINESS GATES
web-app-5d4dd47c87-9pnvn   1/1     Running   0          45m   10.244.1.93   k8s-worker1   <none>           <none>
web-app-5d4dd47c87-ptbss   1/1     Running   0          13s   10.244.1.95   k8s-worker1   <none>           <none>
peter@k8s-master:~$

There should now be two running pods, each with its own IP address.

Kubernetes can distribute incoming requests between these replicas.

Identifying the Backend Pods

To make it easier to identify which pod responds to a request, modify the NGINX web page in each container.

Access the first pod:

kubectl exec -it <pod-1> -n deep -- /bin/bash

Modify the default NGINX page so that it displays a unique message such as:

Hello from pod 1

Exit the container:

exit

Repeat the process for the second pod:

kubectl exec -it <pod-2> -n deep -- /bin/bash

Change the NGINX page to display:

Hello from pod 2

Then exit the container.

Each replica now returns a different response, making it possible to observe how traffic is distributed.

Testing Load Balancing

Use curl against the external IP:

curl 192.168.2.240

The response may come from either pod.

For example:

Hello from pod 2

Run the command again:

curl 192.168.2.240

The response may now come from the other replica:

Hello from pod 1

 

peter@k8s-master:~$ for i in {1..10}; do curl 192.168.2.240; done
hello from pod 2
hello from pod 1
hello from pod 2
hello from pod 2
hello from pod 2
hello from pod 2
hello from pod 1
hello from pod 2
hello from pod 1
hello from pod 2
peter@k8s-master:~$ 

Repeated requests can therefore demonstrate that traffic is being distributed between the two replicas.

LoadBalancer Traffic Flow

The overall traffic flow can be represented as:

External User
      |
      v
192.168.2.240
      |
      v
LoadBalancer Service
      |
      v
Kubernetes Service
      |
      +------------------+
      |                  |
      v                  v
  Pod 1              Pod 2
  NGINX              NGINX

MetalLB provides the external IP address, while Kubernetes forwards incoming traffic through the service to the available backend pods.

Summary

A Kubernetes LoadBalancer service is designed to expose an application outside the cluster.

In a cloud environment, Kubernetes can work with a cloud provider to provision an external load balancer and obtain an external IP address.

In a local Kubernetes cluster, there is no cloud load balancer available, so the external IP remains Pending.

MetalLB provides a solution for local and bare-metal environments by acting as a software-based load balancer. It assigns IP addresses from a configured pool and advertises them on the local network.

In this example:

  • The web-app deployment runs NGINX.

  • The application is deployed in the deep namespace.

  • A LoadBalancer service exposes the application.

  • MetalLB provides the external IP address 192.168.2.240.

  • The IP address is advertised using Layer 2.

  • External requests reach the LoadBalancer service.

  • Kubernetes forwards requests to the backend pods.

  • Multiple replicas allow traffic to be distributed across the application pods.

This demonstrates how a Kubernetes LoadBalancer service can expose an application to external users and forward incoming traffic to backend pods.

 


Comments (0)

There are no comments yet. Be the first to comment.

Log in or register to leave a comment. all comments are subject to admin approval