DEV Community

Cover image for Expose the Kubernetes Load Balancer on Bare Metal using MetalLB
Cheulong Sear
Cheulong Sear

Posted on

Expose the Kubernetes Load Balancer on Bare Metal using MetalLB

This article will show you how to install MetalLB in K8s and expose a load balancer in your network.
Please refer to the official doc for more detail.

Preparation

If you’re using kube-proxy in IPVS mode, since Kubernetes v1.14.2 you have to enable strict ARP mode.
run

kubectl get configmap kube-proxy -n kube-system -o yaml | \
sed -e "s/strictARP: false/strictARP: true/" | \
kubectl diff -f - -n kube-system
Enter fullscreen mode Exit fullscreen mode

if you see something like this

1

then run

kubectl get configmap kube-proxy -n kube-system -o yaml | \
sed -e "s/strictARP: false/strictARP: true/" | \
kubectl apply -f - -n kube-system
Enter fullscreen mode Exit fullscreen mode

Installation with Helm

helm repo add metallb https://metallb.github.io/metallb
helm install metallb metallb/metallb --create-namespace -n metallb-system 
Enter fullscreen mode Exit fullscreen mode

L2 configuration

create metallb-config.yaml

apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
  name: default-ip-pool
  namespace: metallb-system
spec:
  addresses:
  - 192.168.186.241-192.168.186.250
---
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
  name: default
  namespace: metallb-system
spec:
  ipAddressPools:
  - default-ip-pool
Enter fullscreen mode Exit fullscreen mode

run kubectl apply -f metallb-config.yaml
Note: ip range need to be inside 192.168.186.0/24 range

Check if it works

kubectl get ipaddresspools -n metallb-system
kubectl get l2advertisement -n metallb-system

Deploy nginx

run kubectl create deploy nginx --image nginx:latest

Expose nginx

using load balancer

run kubectl expose deploy nginx --port 80 --type LoadBalancer

cheulong-sear@cheulong-linux:~$ k get svc
NAME         TYPE           CLUSTER-IP      EXTERNAL-IP       PORT(S)        AGE
kubernetes   ClusterIP      10.96.0.1       <none>            443/TCP        16d
nginx        LoadBalancer   10.106.24.222   192.168.186.242   80:32454/TCP   69s
test-nginx   NodePort       10.99.173.211   <none>            80:32544/TCP   16d
Enter fullscreen mode Exit fullscreen mode

go to 192.168.186.242

2

using Ingress Nginx

Install ingress nginx using helm

helm upgrade --install ingress-nginx ingress-nginx \
  --repo https://kubernetes.github.io/ingress-nginx \
  --namespace ingress-nginx --create-namespace
Enter fullscreen mode Exit fullscreen mode

Expose ClusterIp Service

run kubectl expose deploy nginx --port 80

create manifest to route to nginx service

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx
            port:
              number: 8080
Enter fullscreen mode Exit fullscreen mode

kubectl apply ingress.yaml

kubectl -n ingress-nginx get svc ingress-nginx-controller

NAME                       TYPE           CLUSTER-IP    EXTERNAL-IP       PORT(S)                      AGE
ingress-nginx-controller   LoadBalancer   10.97.61.44   192.168.186.241   80:31540/TCP,443:32342/TCP   16m
Enter fullscreen mode Exit fullscreen mode

go to 192.168.186.242

2

=== Done ===

Code for this article

(back to top)

Leave a comment if you have any questions.

===========
Please keep in touch
Portfolio
Linkedin
Github
Youtube

Top comments (0)