Tuesday, July 20, 2021

Load balancing IP addresses using metallb

 For aws,azure cloud environments you will get public ip when the service type changed to LoadBalancer type from ClusterIP.


But with internal environments or test environments you might need separate applications to provide you ip addresses.


One of that is metallb.


For demonstration purpose we use kind(Kubernetes in docker).

Run the commands in the "using manifest" section.

installation by manifest from site : https://metallb.universe.tf/

The above commands will create a namespace with name metallb-system and deployments related to metallb.


dminuser@testing:~/Desktop$ kubectl get pods -n metallb-system

NAME                          READY   STATUS    RESTARTS   AGE

controller-6b78bff7d9-77hhh   1/1     Running   0          7m24s

speaker-6dcjz                 1/1     Running   0          7m24s

speaker-96btj                 1/1     Running   0          7m24s

speaker-j7mqx                 1/1     Running   0          7m24s



Then you need to configure the ip adress range for the metallb, this range is used for assigning the load balancer ips to applications deployed.

Get the ip range used in kubernetes nodes.

kubectl get nodes -o wide

dminuser@testing:~/Desktop$ kubectl get nodes -o wide

NAME                 STATUS   ROLES                  AGE   VERSION   INTERNAL-IP   EXTERNAL-IP   OS-IMAGE       KERNEL-VERSION     CONTAINER-RUNTIME

kind-control-plane   Ready    control-plane,master   32m   v1.21.1   172.18.0.2    <none>        Ubuntu 21.04   5.8.0-59-generic   containerd://1.5.2

kind-worker          Ready    <none>                 31m   v1.21.1   172.18.0.3    <none>        Ubuntu 21.04   5.8.0-59-generic   containerd://1.5.2

kind-worker2         Ready    <none>                 31m   v1.21.1   172.18.0.4    <none>        Ubuntu 21.04   5.8.0-59-generic   containerd://1.5.2

You can see the ip address of a node is 172.18.0.2


If you are using kind, there is a separate bridge created for it.Which you can see using (ip a s)

5: br-c2f1d19e1185: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 

    link/ether 02:42:3c:41:38:6b brd ff:ff:ff:ff:ff:ff

    inet 172.18.0.1/16 brd 172.18.255.255 scope global br-c2f1d19e1185

       valid_lft forever preferred_lft forever

    inet6 fc00:f853:ccd:e793::1/64 scope global 

       valid_lft forever preferred_lft forever

    inet6 fe80::42:3cff:fe41:386b/64 scope link 

       valid_lft forever preferred_lft forever

    inet6 fe80::1/64 scope link 

       valid_lft forever preferred_lft forever


From the above command, you found the network range.

sudo apt install sipcalc

sipcalc 172.18.0.1/16


adminuser@testing:~/Desktop$ sipcalc 172.18.0.1/16

-[ipv4 : 172.18.0.1/16] - 0


[CIDR]

Host address - 172.18.0.1

Host address (decimal) - 2886860801

Host address (hex) - AC120001

Network address - 172.18.0.0

Network mask - 255.255.0.0

Network mask (bits) - 16

Network mask (hex) - FFFF0000

Broadcast address - 172.18.255.255

Cisco wildcard - 0.0.255.255

Addresses in network - 65536

Network range - 172.18.0.0 - 172.18.255.255

Usable range - 172.18.0.1 - 172.18.255.254

From the usable range, you can select few ip addresses and add to the config file provided in the link.
use Layer2 method for configuration
https://metallb.universe.tf/configuration/

apiVersion: v1
kind: ConfigMap
metadata:
  namespace: metallb-system
  name: config
data:
  config: |
    address-pools:
    - name: default
      protocol: layer2
      addresses:
      - 172.18.255.1-172.18.255.250


Then apply this using: kubectl apply -f metallb-config.yaml

For testing, deploy nginx.

kubectl create deploy nginx --image nginx

Expose deployment as a service type load balancer.

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

This will assign an external ip in the provided range:

adminuser@testing:~/Desktop$ kubectl get svc

NAME         TYPE           CLUSTER-IP     EXTERNAL-IP    PORT(S)        AGE

kubernetes   ClusterIP      10.96.0.1      <none>         443/TCP        36m

nginx        LoadBalancer   10.96.38.124   172.18.255.1   80:31432/TCP   11s

You can test it using http://172.18.255.1 from the machine where you installed king.

Reference video

No comments:

Post a Comment