[บทความ] [Linux] วิธีการ set Load Balance 2 wan

admin

Staff Member
Administrator
1. ระบบ Internet 2 สาย
1.1 Internet สาย 1 ต่อด้วย router IP 192.168.10.1 Endian eth1 IP 192.168.10.2
1.2 Internet สาย 2 ต่อด้วย router IP 192.168.100.1 Endian eth2 IP 192.168.100.2
ใน Script balance.sh ตรง ค่า ENDGW1 ; ENDGW2 จึงใส่ = 1
Endian ไม่ต้อง Set backup line เพราะใช้ script แทน

2. ใช้ WinSCP remote ไปแก้ file crontab [ /etc/crontab ]
2.1 กรณีกำหนดให้ Run script ทุกครึ่งชั่วโมง โดยเพิ่มบรรทัด
0,30 * * * * /usr/share/balance.sh
2.2 กรณีกำหนดให้ Run script ทุก 15 นาที โดยเพิ่มบรรทัด
0,15,30,45 * * * * /usr/share/balance.sh

3. สร้าง file balance.sh ที่ /usr/share
กำหนด attribute ให้ทุกคนสามรถ execute ได้
โดยใช้ WinSCP remote เลือก file balance.sh กด F9 Tick X หลัง Owner , Group ,Others แล้ว Click OK
3.1 หาก eth1 ต่อแบบ ppp0
ในตัว Script balance.sh ตรง IP1 กับ GW1 ให้เปลี่ยนเป็น
Bash:
IP1=`ifconfig ppp0 | grep inet | awk '{ print $2 }' | awk -F: '{ print $2 }'`
     GW1=$IP1
4. ดู Log file ใช้ WinSCP remote เลือก /var/log/gatewaystatus


file balance.sh
Bash:
# ---  2 Wan loadbalance ---
#!/bin/bash
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
PROG="balance2WAN"
PROGDIR="/usr/local/sbin"
# Configuration Variables
# ----- Begin Of Configuration -----
# IP Address to ping.
# [URL='http://www.avira.com/']www.avira.com[/URL]
CHECKHOST1=62.146.210.54
# [URL='http://www.yahoo.com/']www.yahoo.com[/URL]
CHECKHOST2=98.137.149.56
# [URL='http://www.ku.ac.th/']www.ku.ac.th[/URL]
CHECKHOST3=158.108.216.5
# [URL='http://www.yahoo.co.th/']www.yahoo.co.th[/URL]
CHECKHOST4=203.84.219.114
# [URL='http://www.sourceforge.net/']www.sourceforge.net[/URL]
CHECKHOST5=216.34.181.60
# Variables hold total ping fail of each eth
CHECKNOTRETVAL1=0
CHECKNOTRETVAL2=0
# Variables hold the exceed ping fail number
CHECKWEIGHT=3
# Variables hold ping timeout in seconds
TIMEOUT=3
# Variables hold link status used for determined in program.
LASTLINKSTATUS1=1
LASTLINKSTATUS2=1
# Variables hold link status used for report purpose.
CHECKRESAULT1=UP
CHECKRESAULT2=UP
# Broadband providers name; used for report purpose.
NAME1=TrueADSL
NAME2=TOT
# Ethernet interfaces (eth ); used for determined in program.
EXTIF1=eth1
EXTIF2=eth2
# Relative weights of routes. Keep this to a integer value.
# If WEIGHT1=1 WEIGHT2=1 ; Balance WEIGHT1:WEIGHT2 is 50 : 50
# If WEIGHT1=2 WEIGHT2=1 ; Balance WEIGHT1:WEIGHT2 is 2/3 ( 66.67 ) : 1/3 ( 33.33 )
WEIGHT1=1
WEIGHT2=1
# Log file for routing status report
CHECKGATEWAYLOG=/var/log/gatewaystatus
# ----- End Of Variable Configuration -----
# ----- Begin Of get IP and Gateway from each eth -----
# Do not change anything below this line excepted use ppp0
# If use ppp0 ; Get IP addresses from ppp0 devices by
# IP1=`ifconfig ppp0 | grep inet | awk '{ print $2 }' | awk -F: '{ print $2 }'`
IP1=`ifconfig eth1 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'`
IP2=`ifconfig eth2 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'`
# Define gateway addresses by replace last segment of each eth IP with Router IP
# ENDGW is the last segment of Router IP
ENDGW1=1
ENDGW2=1
# Do not change anything below this line excepted use ppp0
# If EXTIF1(eth1)used ppp0 , gateway is same as the ppp0 IP
# GW1=$IP1
GW1=${IP1%.*}.$ENDGW1
GW2=${IP2%.*}.$ENDGW2
# ----- End Of read IP and Gateway from each eth -----
# Check wan1 link status
for CHECKHOSTS in {$CHECKHOST1,$CHECKHOST2,$CHECKHOST3,$CHECKHOST4,$CHECKHOST5}; do
      ping -W $TIMEOUT -I $IP1 -c 1 $CHECKHOSTS > /dev/null  2>&1
      RETVAL=$?
      if [ $RETVAL -ne 0 ]; then
          CHECKNOTRETVAL1=`expr $CHECKNOTRETVAL1 + 1`
      fi
done
if [ $CHECKNOTRETVAL1 -ge $CHECKWEIGHT ]; then
   LASTLINKSTATUS1=1
   CHECKRESAULT1=DOWN
else
   LASTLINKSTATUS1=0
   CHECKRESAULT1=UP
fi
# Check wan2 link status
for CHECKHOSTS in {$CHECKHOST1,$CHECKHOST2,$CHECKHOST3,$CHECKHOST4,$CHECKHOST5}; do
     ping -W $TIMEOUT -I $IP2 -c 1 $CHECKHOSTS > /dev/null  2>&1
     RETVAL=$?
     if [ $RETVAL -ne 0 ]; then
        CHECKNOTRETVAL2=`expr $CHECKNOTRETVAL2 + 1`
     fi
done
if [ $CHECKNOTRETVAL2 -ge $CHECKWEIGHT ]; then
   LASTLINKSTATUS2=1
   CHECKRESAULT2=DOWN
else
   LASTLINKSTATUS2=0
   CHECKRESAULT2=UP
fi
# Send WAN Status to log file
date +'-------------------------(%D %T)-------------------------' >> $CHECKGATEWAYLOG
echo ---------------------------------------------------------------------  >> $CHECKGATEWAYLOG
echo Line $NAME1 IP $IP1 GW $GW1 is $CHECKRESAULT1 : Ping fail $CHECKNOTRETVAL1 OF 5   >> $CHECKGATEWAYLOG
echo Line $NAME2 IP $IP2 GW $GW2 is $CHECKRESAULT2 : Ping fail $CHECKNOTRETVAL2 OF 5   >> $CHECKGATEWAYLOG
echo ---------------------------------------------------------------------  >> $CHECKGATEWAYLOG
# Custom routing base on WAN status
if [[ $LASTLINKSTATUS1 -eq 1 && $LASTLINKSTATUS2 -eq 0 ]]; then
   echo - Routing using 1 WAN - $NAME2>> $CHECKGATEWAYLOG
   echo ---------------------------------------------------------------------  >> $CHECKGATEWAYLOG
   ip route flush cache     
   ip route replace default scope global via $GW2 dev $EXTIF2
   ip route show | awk '{print}' >> $CHECKGATEWAYLOG
elif [[ $LASTLINKSTATUS1 -eq 0 && $LASTLINKSTATUS2 -eq 1 ]]; then
   echo - Routing using 1 WAN - $NAME1 >> $CHECKGATEWAYLOG
   echo ---------------------------------------------------------------------  >> $CHECKGATEWAYLOG
   ip route flush cache      
   ip route replace default scope global via $GW1 dev $EXTIF1
   ip route show | awk '{print}' >> $CHECKGATEWAYLOG
elif [[ $LASTLINKSTATUS1 -eq 0 && $LASTLINKSTATUS2 -eq 0 ]]; then
   echo - Routing using 2 WAN - $NAME1, $NAME2 >> $CHECKGATEWAYLOG
   echo ---------------------------------------------------------------------  >> $CHECKGATEWAYLOG
   ip route flush cache
   ip route replace default scope global nexthop via $GW1 dev $EXTIF1 weight $WEIGHT1 nexthop via $GW2 dev $EXTIF2 weight $WEIGHT2
   ip route show | awk '{print}' >> $CHECKGATEWAYLOG
fi
echo ===================================================================== >> $CHECKGATEWAYLOG
# ---  End balancing ---
 

สมาชิกที่ออนไลน์

ไม่มีสมาชิกที่ออนไลน์ในตอนนี้.

แฟนเพจ


Top