Configurazione di un indirizzo IP virtuale tramite script Pre e Post

È possibile specificare uno script pre e uno script post quando viene avviata una commutazione della funzionalità master/slave. Il percorso assoluto di questi script può essere configurato in eranger-replication.cfg 

...
## scripts to be executed before and after switch
[scripts]
pre_script=/opt/eranger/sbin/vip-switch.sh
post_script=/opt/eranger/sbin/vip-switch.sh
# pre_timeout=30
# post_timeout=30

 

Lo script precedente non appartiene alla distribuzione software SKOOR. Tuttavia, per comodità, di seguito viene fornito un esempio.

È possibile fornire lo stesso identico script per l'opzione pre_script e l'opzione post_script, come nel caso dell'esempio precedente. Gli script pre/post verranno terminati se non escono entro il timeout specificato (le unità sono in secondi).

Caso d'uso:

Associare e dissociare un indirizzo IP virtuale ad alta disponibilità all'/dall'interfaccia di rete dell'attuale master durante la commutazione dei ruoli master/slave. Quando viene avviata una commutazione sull'attuale slave, gli script pre e post vengono eseguiti nel seguente ordine:

Script

sul sistema

azione

pre_script

slave attuale

nessuna azione

pre_script

master attuale

dissociare l'indirizzo HA-IP

operazione principale di eranger-server-replication.pl

 

eseguire la commutazione effettiva della funzionalità master/slave

post_script

nuovo master

associare l'indirizzo HA-IP 10.1.0.90

post_script

nuovo slave

nessuna azione

Nell'esempio precedente 10.1.0.90 rappresenta l'indirizzo HA-IP virtuale.

Se il master non è raggiungibile dallo slave, gli script pre e post verranno omessi sul vecchio master.

Lo script pre/post di esempio seguente /opt/eranger/sbin/vip-switch.sh associa/dissocia l'indirizzo HA-IP 10.1.0.90 (netmask 255.255.254.0) all'/dall'interfaccia di rete eth0 sull'attuale master.

#!/bin/bash
#
# vip-switch.sh
#
# A script to configure virtual hosting on the WTC eRanger platform
#
# Versions:     2016-02-09 / Port from FreeBSD to RHEL
#
# Arguments:
#   $1 -> IP address of new master (not used here)
#   $2 -> mode (pre/post)
#   $3 -> switch direction (m2s/s2m)
#
# Functionality:
#   m2s mode -> remove ip ${VIP} from interface ${BASE_INT} in pre
#            -> do nothing in post
#
#   s2m mode -> do nothing in pre
#            -> add ip ${VIP} as alias to interface ${BASE_INT} in post
#

## CONFIG ##
VIP="10.1.0.90"
VWP="${VIP}/25"
NETMASK="255.255.255.128"
BASE_INT="eth0"
ALIAS_INT="eth0:0"
CONFFILE="/etc/sysconfig/network-scripts/ifcfg-${ALIAS_INT}"

## CONSTS ##
IF="/sbin/ip"
GREP="/bin/grep"
PING="/bin/ping"

## METHODS ##
function usage {
   echo "Usage: $0 <master> <pre/post> <mode>"
   echo "   master  : the ip address of the new master"
   echo "   pre/post: in which phase to run the script"
   echo "   mode    : either \"m2s\" or \"s2m\""
   exit 1
}

## CHECKS ##
for b in ${IF} ${GREP} ${PING}; do
   if [ ! -x ${b} ]; then
      echo "ERR> ${b} not available, exiting..."
      exit 1
   fi
done

if [ "x${VIP}" == "x" -o "x${BASE_INT}" == "x" ]; then
   echo "ERR> Necessary config missing, exiting..."
   exit 1
fi

${IF} addr list ${BASE_INT} &>/dev/null
if [ $? -ne 0 ]; then
   echo "ERR> Base interface ${BASE_INT} is not available, exiting..."
   exit 1
fi

# parse args
pp=$2
mode=$3
if [ "x${pp}" != "xpre" -a "x${pp}" != "xpost" ]; then
   usage
fi
if [ "x${mode}" != "xs2m" -a "x${mode}" != "xm2s" ]; then
   usage
fi

if [ ${mode} == "s2m" ]; then
   # script is running on new master

   if [ "x${pp}" == "xpre" ]; then
      # only doing stuff in post
      exit 0
   fi

   # safety check
   ${IF} addr list ${BASE_INT} 2>/dev/null | ${GREP} -q "inet ${VWP}" &>/dev/null
   if [ $? -eq 0 ]; then
      echo "OK> VIP ${VIP} is already defined on ${BASE_INT}"
      exit 0
   fi

   # is VIP up on slave?
   ${PING} -c 1 -n -W 2 ${VIP} &>/dev/null
   if [ $? -eq 0 ]; then
      # ip is still pingable
      echo "ERR> ${VIP} is still up on slave, exiting"
      exit 1
   fi

   # we are new master, VIP is down -> add VIP
   ${IF} addr add ${VWP} dev ${BASE_INT} label ${ALIAS_INT} &>/dev/null
   if [ $? -ne 0 ]; then
      echo "ERR> Failed to add IP ${VIP} to ${BASE_INT}"
      exit 1
   else
      echo "OK> ${VIP} added to ${BASE_INT}."
      # need to create CONFFILE in case server is rebooted -> VIP would be lost then
      cat << EOF > ${CONFFILE}
DEVICE=${ALIAS_INT}
IPADDR=${VIP}
NETMASK=${NETMASK}
EOF
      if [ $? -eq 0 ]; then
         echo "OK> ${VIP} configured in ${CONFFILE}."
      else
         echo "ERR> ${VIP} not configured in ${CONFFILE}."
      fi
      exit 0
   fi
else
   # script is running on new slave

   if [ "x${pp}" == "xpost" ]; then
      # nothing to do
      exit 0
   fi

   # safety check
   ${IF} addr list ${BASE_INT} 2>/dev/null | ${GREP} -q "inet ${VWP}" &>/dev/null
   if [ $? -ne 0  ]; then
      echo "OK> VIP ${VIP} is not defined on ${BASE_INT}"
      exit 0
   fi

   # eranger is about to be switched -> remove VIP
   ${IF} addr del ${VWP} dev ${BASE_INT} label ${ALIAS_INT} &>/dev/null
   if [ $? -ne 0 ]; then
      echo "ERR> Failed to remove IP ${VIP} from ${BASE_INT}"
      exit 1
   else
      echo "OK> ${VIP} removed from ${BASE_INT}"
      # need to remove CONFFILE now
      rm -f ${CONFFILE}
      exit 0
   fi
fi

exit 0

Gli script pre e post vengono sempre chiamati con i tre argomenti seguenti:

$1

Indirizzo IP del nuovo master

$2

pattern "pre" o "post"  per determinare se lo script deve eseguire attività pre o post.

$3

pattern "m2s" o "s2m" con il significato:

m2s     l'host su cui viene eseguito questo script deve essere convertito da master a slave
s2m     l'host su cui viene eseguito questo script deve essere convertito da slave a master