新的redis-sentinel虚拟IP偏移脚本

新的redis-sentinel虚拟IP偏移脚本

   小樱     2020年1月4日 02:17     2019    

(1)漂移VIP的脚本

之前也写过一个漂移的脚本,因为没有仔细的研读配置文件,所以在获取master的时候费了点事,仔细看过配置文件后,才知道,原理redis在执行脚本后会传递一些参数到脚本里边,传递的参数里边正好有旧的master和新的master的地址和端口号,如此一来,写漂移的脚本就会简单许多。

下边是配置文件的一些知识和脚本的内容。

下边是配置文件对sentinel client-reconfig-script的解释

# CLIENTS RECONFIGURATION SCRIPT

# sentinel client-reconfig-script <master-name> <script-path>

# When the master changed because of a failover a script can be called in

# order to perform application-specific tasks to notify the clients that the

# configuration has changed and the master is at a different address.

# The following arguments are passed to the script:

# <master-name> <role> <state> <from-ip> <from-port> <to-ip> <to-port>

# <state> is currently always "failover"

# <role> is either "leader" or "observer"

# The arguments from-ip, from-port, to-ip, to-port are used to communicate

# the old address of the master and the new address of the elected replica

# (now a master).

# This script should be resistant to multiple invocations.

# Example:

sentinel client-reconfig-script mymaster /usr/local/redis/scripts/changeVip.sh


下边是翻译:

客户端重新配置脚本

# sentinel client-reconfig-script <主名> <脚本路径>

当主服务器因为故障转移而改变时,可以调用脚本执行特定于应用程序的任务,以通知客户端配置已经更改,主服务器在另一个地址。

以下参数被传递给脚本:

# <主机名> <角色> <状态> <from-ip> <from-port> <to-ip> <to-port>

# <状态>当前总是“故障转移”

# <角色>要么是“领导者”,要么是“观察者”

来自ip、来自端口、到ip、到端口的参数用于通信

主服务器的旧地址和所选副本的新地址

这个脚本应该能够抵抗多次调用。

sentinel client-reconfig-script mymaster /usr/local/redis/scripts/changeVip.sh


所以说当发生故障转移的时候,会有7个参数被传递到自动执行的脚本当中去,其中可以通过to-ip和to-port这两个参数获得新master的地址。

#!/bin/bash

VIP="192.168.0.224"

#设置虚拟IP地址

VIP_NETMASK="255.255.255.255"

#虚拟IP的子网掩码

IP=$(ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6 | awk '{print $2}'|grep -v $VIP)

#获取本机IP地址

NETNAME=$(ls /sys/class/net | grep -v lo)

#获取网卡的名称,在添加虚拟IP时会使用到

REDIS_HOME='/usr/local/shopnc/redis-5.0.0'

#redis的安装目录

OLD_MASTER=$4

获取旧master-redis的地址

NEW_MASTER=$6

获取新master-redis的地址

 

#下边是redis进行切换的模块

#判断本机地址是不是旧的master-redis

if [ $IP = $OLD_MASTER ]

then

#如果是旧master-redis地址的话旧删除虚拟IP

       ip addr del "$VIP/32" dev $NETNAME

       exit 0

else

#如果是新master-redis地址的话旧添加虚拟IP

       if [ $IP = $NEW_MASTER ]

       then

              ifconfig "$NETNAME:1" $VIP netmask $VIP_NETMASK

       fi

fi

 

(2)旧的脚本

下边是旧的偏移脚本,和上边不同的是,通过命令获取新的master,保存到REDIS_NAME这个变量当中。

#!/bin/bash

RPORT=6380

#获取端口号

VIP="192.168.0.224"

#设置虚拟IP地址

VIP_NETMASK="255.255.255.255"

#虚拟IP的子网掩码

IP=$(ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6 | awk '{print $2}'|grep -v $VIP)

#获取本机IP地址

EXISTPORT=$(netstat -an | grep 6380 | grep LISTEN | wc -l)

#查看redis端口号

NETNAME=$(ls /sys/class/net | grep -v lo)

#获取网卡的名称,在添加虚拟IP时会使用到

REDIS_HOME='/usr/local/shopnc/redis-5.0.0'

#redis的安装目录

REDIS_NAME=$($REDIS_HOME/src/redis-cli -h $IP -p $RPORT INFO replication | grep role|tr -d '\r' | cut -d: -f2)

#获取redis的角色

#下边是redis进行切换的模块

if [ $EXISTPORT -eq 0 ]

then

       ip addr del "$VIP/32" dev $NETNAME

       exit 0

else

       if [ "$REDIS_NAME" = "master" ]

       then

              ifconfig "$NETNAME:1" $VIP netmask $VIP_NETMASK

       fi

fi

上边脚本的思路

当旧master-redis去执行的时候,因为redis已经没有了端口,所以就去掉虚拟IP。

当slave-redis去执行的时候,首先判断自己是不是晋升为了新master-redis,如果晋升为了新的master就将虚拟IP添加上去。没有晋升的就退出执行了。

 


文章评论

0

其他文章