-
-
Save aledbf/4512224861674403ec2220380acdadb4 to your computer and use it in GitHub Desktop.
Virtual IP with ExaBGP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
LOCAL_AS=65210 | |
PEER_AS=65200 | |
LB_IP="1.2.3.4" | |
HOST_IP=`ip address list | grep "scope global" | grep -v "scope global lo" | cut -d" " -f6 | cut -d"/" -f 1` | |
GATEWAY=`ip route | grep "^default" | cut -d" " -f3` | |
EXA_BGP_CONFIG="/etc/exabgp/exabgp.conf" | |
EXA_BGP_CHECK="/etc/exabgp/exabgp-check.sh" | |
if [ "$1" == "turndown" ]; then | |
ip addr del ${LB_IP}/32 dev lo | |
exit 0 | |
fi | |
if ip address list lo |grep ${LB_IP} | |
then | |
echo "${LB_IP}/32 is already set" | |
else | |
ip add a ${LB_IP}/32 dev lo | |
fi | |
cat <<EOF > ${EXA_BGP_CONFIG} | |
group LB { | |
hold-time 180; | |
local-as ${LOCAL_AS}; | |
peer-as ${PEER_AS}; | |
# local physical interface address | |
router-id ${HOST_IP}; | |
# neighbor is default gateway address | |
neighbor ${GATEWAY} { | |
# local-address = local physical interface address | |
local-address ${HOST_IP}; | |
description "LB to router"; | |
process exabgp-check { | |
run /etc/exabgp/exabgp-check.sh; | |
} | |
} | |
} | |
EOF | |
cat <<EOF > ${EXA_BGP_CHECK} | |
#!/usr/bin/env bash | |
STATE="down" | |
VIP="${LB_IP}" | |
GW_IP="${HOST_IP}" | |
while true; do | |
ip address list lo |grep ${VIP}/32 >/dev/null 2>&1 | |
if [[ \$? == 0 ]]; then | |
if [[ "\$STATE" != "up" ]]; then | |
echo "announce route \${VIP}/32 next-hop \${GW_IP}" | |
STATE="up" | |
fi | |
else | |
if [[ "\$STATE" != "down" ]]; then | |
echo "withdraw route \${VIP}/32 next-hop \${GW_IP}" | |
STATE="down" | |
fi | |
fi | |
sleep 1 | |
done | |
EOF | |
chmod 755 ${EXA_BGP_CHECK} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment