browser icon
You are using an insecure version of your web browser. Please update your browser!
Using an outdated browser makes your computer unsafe. For a safer, faster, more enjoyable user experience, please update your browser today or try a newer browser.

Load Balancing & Failover With Multiple Connections on Linux

Posted by on June 9, 2008

Udah lama gak nulis jadi pengen share sekalian dijadiin logbook aja.

Buat yang punya multiple connection baik itu di kantor, di warnet, di pos kamling, sampe di pos satpam dan di warteg-warteg.. (haaaalaaaahh..) Nah berikut cara buat load balancing dan failover untuk multiple connection di linux.

Setelah baca primbon Linux Advanced Routing & Traffic Control HOWTO akhirnya menemukan caranya. Trus langkah yang paling awal kita harus ngerti dulu jaringan yang kita punya. So mari kita cari informasinya

Link internet A ipnya 192.168.5.1 gatewaynya 192.168.5.254

Link internet B ipnya 10.10.10.6 gatewaynya 10.10.10.5

Untuk koneksi yang lebih cepet adalah koneksi internet B sehingga weight nya di set 4 supaya ga kusut katanya.

Okey.. informasi udah kumplit.. Sekarang kita rock n roll di linux ^^

Buat dua routing didalam routing table ( /etc/iproute2/rt_tables ). Lalu buka file dan lakukan penambahan sesuai dengan konksi internet kita:

1 ispa
2 ispb

Untuk mengatur load balancing route dengan multiple connection tambah file ini di rc.local:
## ispa
ip route add 192.168.5.0/24 dev eth1 src 192.168.5.1 table ispa
ip route add default via 192.168.5.254 table ispa
ip rule add from 192.168.5.254 table ispa
## ispb
ip route add 10.10.10.0/24 dev eth2 src 10.10.10.6 table ispb
ip route add default via 10.10.10.5 table ispb
ip rule add from 10.10.10.5 table ispb
ip route add default scope global nexthop via 192.168.5.254 dev eth1 weight 1 nexthop via 10.10.10.5 dev eth2 weight 4

Untuk Failover

Buat file namanya /usr/local/sbin/gwping isinya

#####
EMAILFROM=”root@myserver.example.com”
EMAILTO=”someone@example.com”

#IP Address or domain name to ping. The script relies on the domain being pingable and always available
TESTIP=www.yahoo.com

#Ping timeout in seconds
TIMEOUT=2

# External interfaces
EXTIF1=eth1
EXTIF2=eth2

#IP address of external interfaces. This is not the gateway address.
IP1=192.168.5.1
IP2=10.10.10.6

#Gateway IP addresses. This is the first (hop) gateway, could be your router IP
#address if it has been configured as the gateway
GW1=192.168.5.254
GW2=10.10.10.5

# Relative weights of routes. Keep this to a low integer value. I am using 4
# for TATA connection because it is 4 times faster
W1=1
W2=4

# Broadband providers name; use your own names here.
NAME1=ispa
NAME2=ispb

#No of repeats of success or failure before changing status of connection
SUCCESSREPEATCOUNT=4
FAILUREREPEATCOUNT=1
#####

if [[ $CLS1 -eq 0 || $CLS2 -eq 0 ]]; then
if [[ $LLS1 -eq 1 && $LLS2 -eq 0 ]]; then
echo Switching to $NAME2
ip route replace default scope global via $GW2 dev $EXTIF2
LSROUTE=$(ip route show)
echo “$LSROUTE” | mail -s “GWPING – Routing table changed. Line $NAME1 is down.” -r $EMAILFROM $EMAILTO
elif [[ $LLS1 -eq 0 && $LLS2 -eq 1 ]]; then
echo Switching to $NAME1
ip route replace default scope global via $GW1 dev $EXTIF1
LSROUTE=$(ip route show)
echo “$LSROUTE” | mail -s “GWPING – Routing table changed. Line $NAME2 is down.” -r $EMAILFROM $EMAILTO
elif [[ $LLS1 -eq 0 && $LLS2 -eq 0 ]]; then
echo Restoring default load balancing
#ip route replace default scope global via $GW1 dev $EXTIF1
ip route replace default scope global nexthop via $GW1 dev $EXTIF1 weight $W1 nexthop via $GW2 dev $EXTIF2 weight $W2
LSROUTE=$(ip route show)
echo “$LSROUTE” | mail -s “GWPING – Routing table changed. Both lines are up.” -r $EMAILFROM $EMAILTO
fi
fi
sleep $SLEEPTIME
done

Lalu buat file /etc/init.d/gwping isinya

#!/bin/sh
# Author: Viliam Kocinsky

### BEGIN INIT INFO
# Short-Description: Monitor Internet access connection links and modify kernel route if some link go down.
# Description: This is startup script for gwping script, that manage internet connection failover (if you have at least 2 providers).
# That is, it reguraly ping one IP address
# on internet through certain provider (but trying all) . If ping doesn’t come back, than it addjust kernel route table.
# See inside script for details. This script is from the website:
# http://blog.taragana.com/index.php/archive/how-to-load-balancing-failover-with-dual-multi-wan-adsl-cable-connections-on-linux/
#
### END INIT INFO

command=”$1″

case “$command” in
start)
echo “Starting gwping.”
/usr/local/sbin/gwping &
echo $! > /usr/local/share/gwping_pid
;;
stop)
echo “Stoping gwping.”
kill -9 $(cat /usr/local/share/gwping_pid) && rm /usr/local/share/gwping_pid
;;
*)
echo “Usage /etc/init.d/gwping start|stop”
;;
esac

Comments

comments

6 Responses to Load Balancing & Failover With Multiple Connections on Linux

  1. hendrawan

    gue biasanya pake PowerPath,..loh..

  2. ridho

    om bisa gak eth0 di kasih ip alias beda ISP otomatis beda IP tapi subnet sama 255.255.255.248 dan beda gateway pakai metode ini, thanks

  3. portable vaporizer weed

    These reviews are often given by other customers who have both actually purchased and used the products.
    The technology is in process of being picked up by Genesis Technology Systems.
    If possible, make separate calls to your spouse and your children.

Leave a Reply to ridho Cancel reply

Your email address will not be published. Required fields are marked *

:)) :) :D (LOL) :-P (woot) ;-) :-o X-( ;-( :-& (angry) (annoyed) (bye) B-) (cozy) (sick) (: (goodluck) (griltongue) (mmm) (hungry) (music) (tears) (tongue) (unsure) (highfive) (dance) (doh) (brokenheart) (drinking) (girlkiss) (rofl) (money) (rock) (nottalking) (party) (sleeping) (thinking) (bringit) (worship) (applause) 8-) (gym) (heart) (devil) (lmao) (banana_cool) (banana_rock) (evil_grin) (headspin) (heart_beat) (ninja) (haha) (evilsmirk) (bigeyes) (funkydance) (idiot) (lonely) (scenic) (hassle) (panic) (okok) (yahoo) (blush) (fish_hit) (muhaha) (muscle) (taser) (beer) (coffee) (banana_ninja) (goal) (fireworks) (smileydance) (dance_bzz) (rusian)