Impostazione dell'indirizzo IP virtuale tramite script Pre e Post
È possibile specificare uno script pre e post quando viene emessa 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
Questo script non fa parte della distribuzione del software SKOOR. Tuttavia, per comodità, ne viene fornito un esempio qui di seguito.
È possibile fornire lo stesso script per l' opzionepre_script e post_script, come nel caso dell'esempio precedente. I pre/post-script saranno terminati se non escono entro il timeout specificato (le unità sono in secondi).
Caso d'uso:
Legare e scollegare un indirizzo IP virtuale ad alta disponibilità all'interfaccia di rete del master corrente durante la commutazione dei ruoli master/slave. Quando viene eseguito uno switch sullo slave corrente, i pre e post script vengono eseguiti nel seguente ordine:
Script | sul sistema | azione |
pre_script | slave corrente | non fare nulla |
pre_script | master corrente | disabbinare l'indirizzo IP dell'HA |
lavoro fondamentale di eranger-server-replication.pl |
| eseguire l'effettivo passaggio della funzionalità master/slave |
post_script | nuovo master | associare l'indirizzo IP HA 10.1.0.90 |
post-script | nuovo slave | non fare nulla |
Nell'esempio precedente 10.1.0.90 rappresenta l'indirizzo virtuale HA-IP.
Se il master non è raggiungibile dallo slave, i pre e post-script saranno omessi sul vecchio master.
L'esempio seguente pre/post-script /opt/eranger/sbin/vip-switch.sh lega/sblocca l 'indirizzo HA-IP 10.1.0.90 (netmask 255.255.254.0) all'interfaccia di rete eth0 del master corrente.
#!/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 sono sempre chiamati con i seguenti tre argomenti:
$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 host su cui gira questo script deve essere convertito da master a slave |