keepalived status und cluster switch


keepalived cluster status

#!/bin/bash
#
# check vrrp cluster state
# djonz nov 2018

# remove old data file if any
if [ -f /tmp/keepalived.data ]; then
   rm /tmp/keepalived.data
fi

# tell keepalived to dump data and wait a bit
kill -USR1 $(cat /var/run/keepalived.pid)
sleep 1

# cluster state output
if [ -f /tmp/keepalived.data ]; then
   STATE=`cat /tmp/keepalived.data | grep -A 5 'VRRP Topology' | grep State | awk '{print $3}'`
   echo "$STATE"
else
   echo "Unable to determine cluster state!"
fi

keepalived cluster switch

#!/bin/bash
# purpose: switch haproxy loadbalancer cluster
# netcat needed to check cluster vip
# MGO/2020

CLUSTER_VIP="192.168.1.1"
PORT="8080"

if [ -f /etc/keepalived/MASTER ]; then
   echo "Switching loadbalancer cluster."
   systemctl stop haproxy.service
   COUNTER=0
   echo -n "Waiting for load balancer on other node to start."
   until [ $COUNTER -gt 5 ]
   do
      PORT=`/bin/nc -z $CLUSTER_VIP $PORT && echo Open`
      if [ "$PORT" == "Open" ]; then
         echo ""
         echo "Load balancer alive on other node."
         COUNTER=7
      else
         echo -n "."
         ((COUNTER++))
         sleep 2
      fi
   done
   if [ ! "$PORT" == "Open" ]; then
      echo "Timeout waiting for service on other node!"
      exit 1
   fi
   echo "Waiting for local state change..."
   sleep 4
   kill -USR1 $(cat /var/run/keepalived.pid)
   sleep 1
   if [ -f /tmp/keepalived.data ]; then
      STATE=`cat /tmp/keepalived.data | grep -A 5 'VRRP Topology' | grep State | awk '{print $3}'`
   else
      echo "Unable to determine cluster state!"
   fi
   echo "Cluster member now in $STATE state."
else
   echo "Cluster member is not master!"
fi