Backup Internet with VRRP and Ubiquiti EdgeRouter
One of the easiest ways to gain hight available Internet access with routers from two different vendors is to use VRRP.
Out of the box, VRRP is just checking if the routers can reach each other, and not for a correct working Internet connection. My shell script switches from the main to the backup router when the specified hosts are not pingable and back again to the main router when they are reachable again. In my case, the setup is a combination of one Ubiquiti EdgeRouter Lite as main router and a Cisco device as backup router.
Here is how to setup the script on your Ubiquity EdgeRouter.
Upload the script and modify
Upload the following script on your router to /config/scripts. And change the variables to match your configuration.
#!/bin/bash
run=/opt/vyatta/sbin/vyatta-cfg-cmd-wrapper
# NextHop: Put here, eg. "4.1.1.2" "8.8.8.8"
Nexthop=( "HostA" "HostB" )
# Number of Pings
Count=4
# Source Interface, eg. eth0.100/ eth1
Inf=eth1
#Get the current VRRP priority
Prio=$(cat /etc/keepalived/keepalived.conf|grep priority|cut -d ' ' -f 2|awk 'NR>1{print $1}')
# Function: Commands to set the priority back
function SetPrioUp() {
$run begin
# Uncomment and adjust
#$run set interfaces ethernet eth0 vrrp vrrp-group 1 priority 110
#$run set interfaces ethernet eth0 vif 102 vrrp vrrp-group 102 priority 110
$run commit
# $run save
$run end
}
# Function: Commands to set the VRRP priority lower
function SetPrioDown() {
$run begin
# Uncomment and adjust
#$run set interfaces ethernet eth0 vrrp vrrp-group 1 priority 80
#$run set interfaces ethernet eth0 vif 102 vrrp vrrp-group 102 priority 80
$run commit
#$run save
$run end
}
# Function: Change the Priority if
function SetPrioUp {
# Check current prio and set up if necessary
if [ "$Prio" -ne 110 ] ; then
echo "setting prio up"
SetPrioUp
fi
}
function SetPrioDown {
# Check current prio and set down if necessary
if [ "$Prio" -ne 80 ] ; then
echo "setting prio down"
SetPrioDown
fi
}
# Ping test the $Nexthop
for i in "${Nexthop[@]}"
do
/bin/ping -c $Count -I $Inf $i > /dev/null
if [ $? -ne 0 ] ; then
# Set down or keep Priority
SetPrioDown; break
fi
done
# Set higher or keep Priority
SetPrioUp
Activate the script
Next step is to activate the VRRP tracker script and let it run every minute
set system task-scheduler task vrrp executable path /config/scripts/vrrptracker.sh
set system task-scheduler task vrrp interval 1m
Comments
You can use your Mastodon account to reply to this post.