Configurer l'adresse IP virtuelle à l'aide de scripts pré et post

Il est possible de spécifier un pré- et un post-script lorsqu'un changement de fonctionnalité master / slave est émis. Le chemin absolu vers ces scripts peut être configuré dans 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

 

Le script ci-dessus n'appartient pas à la distribution du logiciel SKOOR. Cependant, pour plus de commodité, un exemple est fourni ci-dessous.

On peut fournir exactement le même script pour les options pre_script et post_script , comme c'est le cas dans l'exemple ci-dessus. Les pré-/post-scripts seront terminés s'ils ne se terminent pas dans le délai spécifié (les unités sont en secondes).

Cas d'utilisation:

Liez et dissociez une adresse IP virtuelle haute disponibilité à l’interface réseau maître actuelle lors du changement de rôle master / slave . Lorsqu'un commutateur est émis sur l' slave actuel, les pré- et post-scripts sont exécutés dans l'ordre suivant :

Scénario

sur le système

action

pre_script

slave actuel

ne fais rien

pre_script

master actuel

dissocier l'adresse IP HA

travail principal d’ server

 

effectuer le basculement proprement dit de la fonctionnalité master / slave

post_script

nouveau master

lier l'adresse IP HA 10.1.0.90

post_script

nouvel slave

ne fais rien

Dans l'exemple ci-dessus, 10.1.0.90 représente l'adresse HA-IP virtuelle.

Si le master n'est pas accessible depuis l' slave , les pré- et post-scripts seront omis sur l'ancien master .

Exemple ci-dessous pré/post-script /opt/eranger/sbin/vip-switch.sh   lie/dissocie l'adresse HA-IP 10.1.0.90 (masque de réseau 255.255.254.0) à/depuis l'interface réseau eth0 sur le master actuel.

#!/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

Les pré et post scripts sont toujours appelés avec les trois arguments suivants :

1 $

Adresse IP du nouveau master

2 $

modèle "pre" ou "post" afin de déterminer si le script doit effectuer des pré- ou post-tâches.

3 $

motif "m2s" ou "s2m" avec la signification :

L'hôte m2s sur lequel ce script s'exécute doit être converti de master en slave
L'hôte s2m sur lequel ce script s'exécute doit être converti d' slave en master