3 Using MetalLB Load Balancers
Important:
The software described in this documentation is either in Extended Support or Sustaining Support. See Oracle Open Source Support Policies for more information.
We recommend that you upgrade the software described by this documentation as soon as possible.
This chapter discusses how to install and configure the MetalLB module, as a custom resource, to set up a network load balancer for Kubernetes applications in a bare metal environment.
Creating an Application Using MetalLB
This section contains a basic test to verify you can create a Kubernetes application that uses MetalLB to provide external IP addresses.
To create a test application to use MetalLB:
-
Create a Kubernetes application that uses a LoadBalancer service. The deployment in this example creates an NGINX application with a replica count of 2, and an associated LoadBalancer service.
On a control plane node, create a file named
nginx-metallb.yaml
and copy the following into the file.--- apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: app: nginx spec: replicas: 2 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: container-registry.oracle.com/olcne/nginx:1.17.7 ports: - containerPort: 80 --- kind: Service apiVersion: v1 metadata: name: nginx-service spec: selector: app: nginx type: LoadBalancer ports: - name: http port: 80 targetPort: 80
-
Start the NGINX deployment and LoadBalancer service:
kubectl apply -f nginx-metallb.yaml
-
You can see the
nginx-deployment
application is running using thekubectl get deployment
command:kubectl get deployments.apps
The output looks similar to:
NAME READY UP-TO-DATE AVAILABLE AGE nginx-deployment 2/2 2 2 31s
-
You can see the
nginx-deployment
service is running using thekubectl get svc
command:kubectl get svc
The output looks similar to:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 25h nginx-service LoadBalancer 10.99.253.99 192.168.1.240 80:31875/TCP 70s
You can see the EXTERNAL-IP for the
nginx-service
LoadBalancer has an IP address of192.168.1.240
. This IP address is provided by MetalLB and is the external IP address that you can use to connect to the application. - Use
curl
to connect to the NGINX application's IP address and add the port for the application (192.168.1.240:80
in this example) to show the NGINX default page.curl 192.168.1.240:80
The output looks similar to:
<!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
-
You can delete the
nginx-service
LoadBalancer service using:kubectl delete svc nginx-service
-
You can delete the
nginx-deployment
application using:kubectl delete deployments.apps nginx-deployment