Total Pageviews

Popular Posts

Monday, June 09, 2014

Load Balancing On Linux Using Nexthop

How to get faster internet connection for your workstations ? basically the more bandwidth, the low latency, the faster internet you will get. But it depends on your networking devices too. Using a gigabits ethernet adapter while using 10/100 Megabits of networking switches wont make your packet goes through faster (vise-versa)

The theory to get faster internet is to have faster networking devices, from network adapter, cables or high speed wireless router and also a fiber internet.

Load balancing is a technique to distribute packets to multiple gateways. Aim to optimize our available resources and the most important is to maximize throughput. Using multiple connections with load balancing instead of a single connections may increase reliability through redundancy.

On Linux operating systems, route command has many options. One of them are nexthop option. The next hop, or gateway, is the address of the next station to which the packet is to be sent on the way to its final destination.

If you are using both cable and wireless, operating system such as Linux or Windows will choose which one has the lowest latency to the gateway. In this case, cable will be always the winner.

How to maximize your available resources ? The command is simple, when you are connected to both cable and wireless, just type ip route del default scope global ; ip route add default scope global nexthop via 192.168.0.1 dev eth0 weight 1 nexthop via 192.168.1.1 dev wlan0 weight 1 && ip r

ip route del default scope global - to remove the default gateway that operating system has already choose for you based on the latency to the gateway.

ip route add default scope global nexthop via 192.168.0.1 dev eth0 weight 1 nexthop via 192.168.1.1 dev wlan0 weight 1 - this command will define your gateways for your selected network adapters. You will need to readjust your gateway addresses depending on your networks configuration. In my case, my gateway for my cable connection is 192.168.0.1 and my gateway for my wireless connection is 192.168.1.1. Please notice that my wireless adapter name is wlan0.

ip r - this command will show your gateways. You will see the outputs. For example,

root@linux:~# ip r
default
nexthop via 192.168.0.1  dev eth0 weight 1
nexthop via 192.168.1.1  dev wlan0 weight 1

This script will do the magic job. When i connects to my wifi, it will automatically runs the load balancing script.

#!/bin/bash

IF=$1
STATUS=$2

if [ "$IF" == "wlan0" ]
then
    case "$2" in
        up)
        ip route del default scope global ; ip route add default scope global nexthop via 192.168.0.1 dev eth0 weight 1 nexthop via 192.168.1.1 dev wlan0 weight 1
        ;;
   esac
fi

Put it on /etc/NetworkManager/dispatcher.d/ and name it as 90loadbalance.sh. This "90" in the name of the script means that this script will be executed in the last 10% of all scripts if you have a bunch of scripts to execute. Dont forget to make it executable. 

Watch this video and see the output of iptraf for my 2 network adapter which is eth0 and wlan0.





p/s - if you use desktop computer, you can install wifi usb adapter. You can try to install 3 or 5 network adapter (wifi, ethernet) to your desktop or laptop and readjust the command depending on your devices and gateways. Practically it will work, but i never tried. You also can make the script to run periodically, persistently by putting it to the crontab.








No comments:

Post a Comment