Hello surya
glad to hear that you found your issue.
I found that calling the shell script via keepalived isn't 100% reliable in error cases. Keepalived still manages who is "master" and who is "backup", but the IP switch itself is done through a shell script. This works as following:
- Check if file '/tmp/keepalived.status' exists
Backup Nodes:
- Check if the string "BACKUP" is inside '/tmp/keepalived.status' and Keepalived is running, print info that node is not master
Master Nodes:
- Check if the string "MASTER" is inside '/tmp/keepalived.status' and Keepalived is running
- Check if DNS works, WSDL is reachable and if Node has already the floating IP
- If so, print information that node has IP
- If not, trigger failover
#!/bin/bash
# set -eux
set -eu
# Colors
Black='\033[0;30m' # Black
Red='\033[0;31m' # Red
RedBlink='\033[31;5m' # Red Blink
Green='\033[0;32m' # Green
GreenBlink='\033[32;5m' # Green Blink
Brown='\033[0;33m' # Brown
BrownBlink='\033[33;5m' # Brown Blink
Blue='\033[0;34m' # Blue
BlueBlink='\033[34;5m' # Blue Blink
Purple='\033[0;35m' # Purple
PurpleBlink='\033[35;5m' # Purple Blink
NC='\033[0m' # No Color
Bold='\033[1m'
Blink='\033[5m'
# Netcup URLs
# https://www.netcup-wiki.de/wiki/Netcup_SCP_Webservice
DOMAIN="www.servercontrolpanel.de"
WSDL="https://www.servercontrolpanel.de/WSEndUser?wsdl"
API="https://www.servercontrolpanel.de/WSEndUser"
BACKUP="BACKUP"
MASTER="MASTER"
FILE="/tmp/keepalived.status"
KEEPALIVE_PROCS=$(ps uax|grep '/usr/local/sbin/keepalived'|grep -v grep|wc -l|tr -d "\n")
# Stupid check, i know
check-dns() {
dig +short ${DOMAIN}
}
check-wsdl() {
curl -s -o /dev/null -w '%{http_code}' -m 10 "${WSDL}"
}
# Execute SOAP Action getVServers to check reachability
check-api() {
curl -s -o /dev/null -w '%{http_code}' -m 10 -H 'Content-Type: text/xml; charset=utf-8' -H 'SOAPAction:' -d @/etc/netcup/getVServers.xml -X POST ${API}
}
# Execute SOAP Action getVServerIPs to check current IPs
check-ips() {
curl -s -w '%{http_code}' -m 10 -H 'Content-Type: text/xml; charset=utf-8' -H 'SOAPAction:' -d @/etc/netcup/getVServerIPs.xml -X POST ${API} | grep -o -i XX.XX.XX.XX | wc -l
}
# Failover Process
# StatusCode 200 indicates operation has been triggered
# StatusCode 500 indicates operation isn't available, cause IP might be already switched
trigger-attach() {
while true
do
STATUS=$(curl -s -o /dev/null -w '%{http_code}' -H 'Content-Type: text/xml; charset=utf-8' -H 'SOAPAction:' -d @/etc/netcup/attachIPRouting.xml -X POST ${API})
if [ "$STATUS" -eq 200 ]; then
echo -e "Failover: ✓ Attach is triggered!"
break
elif [ "$STATUS" -eq 500 ]; then
echo -e "Attachment: ☢ Got $STATUS , seems wrong"
echo -e "Attachment: ☹ Break back into main loop"
break
else
echo -e "Attachment: ✗ Got $STATUS :( Not done yet..."
fi
sleep 10
done
}
trigger-detach() {
while true
do
STATUS=$(curl -s -o /dev/null -w '%{http_code}' -H 'Content-Type: text/xml; charset=utf-8' -H 'SOAPAction:' -d @/etc/netcup/detachIPRouting.xml -X POST ${API})
if [ "$STATUS" -eq 200 ]; then
echo -e "Detachment: ✓ Detach is triggered!"
break
elif [ "$STATUS" -eq 500 ]; then
echo -e "Detachment: ☢ Got $STATUS , seems wrong"
echo -e "Detachment: ☹ Break back into main loop"
break
else
echo -e "Detachment: ✗ Got $STATUS :( Not done yet..."
fi
sleep 10
done
}
### Real Stuff is down here
wait-for-dns() {
while true
do
IP=$(check-dns)
if [ -n "$IP" ]; then
echo -e "DNS Check: ✓ SCP API resolves to $IP!"
break
else
echo -e "DNS Check: ✗ Could not resolve $DOMAIN"
fi
sleep 10
done
}
wait-for-wsdl() {
while true
do
STATUS=$(check-wsdl)
if [ "$STATUS" -eq 200 ]; then
echo -e "WSDL Check: ✓ Netcup URL is reachable!"
break
else
echo -e "WSDL Check: ✗ Got $STATUS :( Not done yet..."
fi
sleep 10
done
}
wait-for-api() {
while true
do
STATUS=$(check-api)
if [ "$STATUS" -eq 200 ]; then
echo -e "API Check: ✓ Netcup API is reachable!"
break
else
echo -e "API Check: ✗ Got $STATUS :( Not done yet..."
fi
sleep 10
done
}
main() {
while true
do
if [ ! -f "$FILE" ]; then
echo "$FILE does not exist."
fi
if grep -q "$MASTER" "$FILE" && [[ $KEEPALIVE_PROCS -gt 0 ]]; then
wait-for-dns
wait-for-wsdl
RESULT_STRING=$(check-ips)
if [ "$RESULT_STRING" -eq 1 ]; then
echo -e "DeadManSwitch: ☑ Node has already XX.XX.XX.XX"
else
echo -e "Failover: ☐ Change FailoverIP Routing to XX.XX.XX.XX${NC}"
wait-for-api
trigger-attach
fi
elif grep -q "$BACKUP" "$FILE"; then
echo -e "DeadManSwitch: ☑ Node is not in master state"
else
echo -e "Panic: errors occured!"
fi
sleep 60
done
}
main
Alles anzeigen