Load balancing and failover using the Glesys API
By using "DNS round robin" together with the Glesys API to add or remove servers, you achieve both load balancing and failover.
Automating failover
#!/bin/bash
#Note: This script is hard‑coded with record IDs. Check them with:
#/usr/bin/curl -X POST -d domain=example.com -k --basic -u cl12345:API-KEY https://api.glesys.com/domain/list_records
THIS="10.0.0.1"
THAT="10.0.0.2"
URL="http://$THAT/blog/testblog"
SEARCH="Some text on the site"
RECORD1="12345"
RECORD2="12346"
ACCOUNT="CL12345"
APIKEY="SECRET"
getStatus() { /usr/bin/wget -O - $URL 2> /dev/null | grep "$SEARCH" &>/dev/null; echo "$?"; }
setRecords(){
echo "setting records to $1 and $2";
/usr/bin/curl -X POST -d record_id=$RECORD1&data=$1 -k --basic -u $ACCOUNT:$APIKEY https://api.glesys.com/domain/update_record
/usr/bin/curl -X POST -d record_id=$RECORD2&data=$2 -k --basic -u $ACCOUNT:$APIKEY https://api.glesys.com/domain/update_record
}
while :
do
STATUS=$(getStatus)
if [ $STATUS -eq 1 ]; then
echo "Site is down!";
setRecords "$THIS" "$THIS"
while [ $STATUS -eq 1 ]; do
sleep 20
STATUS=$(getStatus)
echo "still down"
done
setRecords "$THIS" "$THAT"
fi
echo "Site is up!";
sleep 60
doneLast updated
Was this helpful?