当前位置: 代码迷 >> 综合 >> Keepalived+redis主从
  详细解决方案

Keepalived+redis主从

热度:40   发布时间:2023-11-21 14:16:15.0

详细参考:

https://blog.csdn.net/qingrx/article/details/9146779

https://blog.csdn.net/lxn19860201/article/details/51422489

一、思路与环境

redis 主和 redis从  正常同步;

设计思路:
redis 主和 redis从  正常同步;

redis主keepalived 启动,执行redis_master.sh脚本,vip在自己身上;

redis 从keepalived 启动,执行redis_slave.sh脚本;

redis主服务关闭,则redis主执行redis_fault.sh脚本, vip落在redis 从上,redis从执行redis_master.sh脚本,变成主;

redis主服务开启,则redis主执行redis_slave.sh脚本变成从,vip还在redis 从上,还是主;

redis从服务关闭,则redis从执行redis_fault.sh脚本, vip落在redis主上,redis主执行redis_master.sh脚本,变成主;

redis从服务开启,则redis从执行redis_slave.sh脚本变成从, vip还在redis主上,redis主继续做主,此时为redis初试主从状态;

环境:

192.168.136.121 redis

192.168.136.122 redis-slave

192.168.136.223 vip

二、安装keepalived

(redis和redis-slave两台服务器都安装)

1.yum -y install ipvsadm
2.yum install popt-devel
3.tar -zxvf keepalived-1.2.2.tar.gz
4.cd keepalived-1.2.2
5.  ./configure
6.make
7.make install
8.cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
   chmod 755 /etc/init.d/keepalived 
   cp /usr/local/sbin/keepalived /usr/sbin/
   cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

三、安装redis

(redis和redis-slave两台服务器都安装)

tar   -zxvf redis-4.0.11.tar.gz
cd redis-4.0.11
make
make install####redis-slve只需注意一点
###[root@redis2 redis-4.0.11]# vim redis.conf
slaveof 192.168.136.121 6379##将主从节点的redis-cli 拷贝至/usr/local/redis/bin/ 后面用到
cp src/redis-cli /usr/local/redis/bin/  

四、修改主机名称

修改Master和Slave的/etc/hosts文件

[root@redis redis-4.0.11]# cat /etc/hosts
192.168.136.121 redis
192.168.136.122 redis-slave

 

Master上

[root@redis redis-4.0.11]# cat /etc/sysconfig/network
NETWORKING=yes

HOSTNAME=redis

 

slave上

[root@redis2 redis-4.0.11]# cat /etc/sysconfig/network
NETWORKING=yes

HOSTNAME=redis-slave

修改完主机名重启机器生效

五、创建配置文件

1.首先,在Slave上创建如下配置文件

vim /etc/keepalived/keepalived.conf

global_defs {

   notification_email {

     1272743926@qq.com

   }

   notification_email_from keepalived@redis-slave

   router_id redis-salve

}

vrrp_script chk_redis {

    script "/etc/keepalived/scripts/redis_check.sh"

    interval 1

}

vrrp_instance mes_Redis {

    state BACKUP

    interface ens33

    garp_master_delay 10

    smtp_alert

    virtual_router_id 3

    priority 90

    nopreempt

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.136.223

    }

    track_script {

        chk_redis

    }

    notify_master /etc/keepalived/scripts/redis_master.sh

    notify_backup /etc/keepalived/scripts/redis_slave.sh

    notify_fault  /etc/keepalived/scripts/redis_fault.sh

    notify_stop   /etc/keepalived/scripts/redis_stop.sh

}

2.然后,在Master上创建如下配置文件

vim /etc/keepalived/keepalived.conf

global_defs {

   notification_email {

     1272743926@qq.com

   }

   notification_email_from keepalived@redis

   router_id redis

}

vrrp_script chk_redis {

    script "/etc/keepalived/scripts/redis_check.sh"

    interval 1

}

vrrp_instance mes_Redis {

    state MASTER

    interface ens33

    garp_master_delay 10

    smtp_alert

    virtual_router_id 3

    priority 100

    nopreempt

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.136.223

    }

    track_script {

        chk_redis

    }

    notify_master /etc/keepalived/scripts/redis_master.sh

    notify_backup /etc/keepalived/scripts/redis_slave.sh

    notify_fault  /etc/keepalived/scripts/redis_fault.sh

    notify_stop   /etc/keepalived/scripts/redis_stop.sh

}

3.在Master和Slave上创建监控Redis的脚本

#!/bin/bash

ALIVE=`/usr/local/redis/bin/redis-cli PING`

if [ "$ALIVE" == "PONG" ]; then

  echo $ALIVE

  exit 0

else

  echo $ALIVE

  exit 1

fi

 

 

编写以下负责运作的关键脚本:

 notify_master /etc/keepalived/scripts/redis_master.sh

 notify_backup /etc/keepalived/scripts/redis_slave.sh

 notify_fault /etc/keepalived/scripts/redis_fault.sh

 notify_stop /etc/keepalived/scripts/redis_stop.sh 

 

因为Keepalived在转换状态时会依照状态来呼叫:

 当进入Master状态时会呼叫notify_master

 当进入Backup状态时会呼叫notify_backup

 当发现异常情况时进入Fault状态呼叫notify_fault

 当Keepalived程序终止时则呼叫notify_stop

4.首先,在Redis Master上创建notity_master与notify_backup脚本:

vim /etc/keepalived/scripts/redis_master.sh

#!/bin/bash

REDISCLI="/usr/local/redis/bin/redis-cli"

LOGFILE="/var/log/keepalived-redis-state.log"

echo "[master]" >> $LOGFILE

date >> $LOGFILE

echo "Being master...." >> $LOGFILE 2>&1

echo "Run SLAVEOF cmd ..." >> $LOGFILE

$REDISCLI SLAVEOF 192.168.136.122 6379 >> $LOGFILE  2>&1

sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态

echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE

$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1

 

vim /etc/keepalived/scripts/redis_slave.sh

#!/bin/bash

REDISCLI="/usr/local/redis/bin/redis-cli"

LOGFILE="/var/log/keepalived-redis-state.log"

echo "[backup]" >> $LOGFILE

date >> $LOGFILE

echo "Being slave...." >> $LOGFILE 2>&1

sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色

echo "Run SLAVEOF cmd ..." >> $LOGFILE

$REDISCLI SLAVEOF 192.168.1.236 6379 >> $LOGFILE  2>&1

 

5.在Redis Slave上创建notity_master与notify_backup脚本:

vim /etc/keepalived/scripts/redis_master.sh

#!/bin/bash

REDISCLI="/usr/local/redis/bin/redis-cli"

LOGFILE="/var/log/keepalived-redis-state.log"

echo "[master]" >> $LOGFILE

date >> $LOGFILE

echo "Being master...." >> $LOGFILE 2>&1

echo "Run SLAVEOF cmd ..." >> $LOGFILE

$REDISCLI SLAVEOF 192.168.136.121 6379 >> $LOGFILE  2>&1

sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态

echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE

$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1

 

 

vim /etc/keepalived/scripts/redis_slave.sh

#!/bin/bash

REDISCLI="/usr/local/redis/bin/redis-cli"

LOGFILE="/var/log/keepalived-redis-state.log"

echo "[backup]" >> $LOGFILE

date >> $LOGFILE

echo "Being slave...." >> $LOGFILE 2>&1

sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色

echo "Run SLAVEOF cmd ..." >> $LOGFILE

$REDISCLI SLAVEOF 192.168.136.121 6379 >> $LOGFILE  2>&1

 

6.然后在Master与Slave创建如下相同的脚本:

 vim  /etc/keepalived/scripts/redis_fault.sh 
#!/bin/bash

LOGFILE=/var/log/keepalived-redis-state.log

echo "[fault]" >> $LOGFILE

date >> $LOGFILE

 

 

vim  /etc/keepalived/scripts/redis_stop.sh 
#!/bin/bash

LOGFILE=/var/log/keepalived-redis-state.log

echo "[stop]" >> $LOGFILE

date >> $LOGFILE

7.在Master与Slave给脚本都加上可执行权限:

chmod +x /etc/keepalived/scripts/*.sh

六、进行测试

1.启动Master上redis

[root@redis redis-4.0.11]# pwd
/opt/software/redis-4.0.11
[root@redis redis-4.0.11]# ./src/redis-server redis.conf

2.启动Slave上的redis

[root@redis2 redis-4.0.11]# pwd
/opt/software/redis-4.0.11
[root@redis2 redis-4.0.11]# ./src/redis-server redis.conf

3.启动Master上的Keepalived

/etc/init.d/keepalived start

####启动失败

解决方法

修改/usr/local/etc/rc.d/init.d/keepalived中的配置
#. /etc/sysconfig/keepalived(这行注释掉,换成下面的那行)
. /usr/local/etc/sysconfig/keepalived##更改以后执行命令
cp /usr/local/sbin/keepalived /usr/sbin/###换另一个命令启动   keepalived
/usr/local/etc/rc.d/init.d/keepalived start

4.启动Slave上的Keepalived

   通 启动master节点一样

5.尝试通过VIP连接Redis:

 ./src/redis-cli -h 192.168.136.223

6.模拟故障 

 将Master上的Redis进程杀死:      ./src/redis-cli shutdown

查看Master上的Keepalived日志:tail /var/log/keepalived-redis-state.log

同时查看Slave上的日志显示:tail /var/log/keepalived-redis-state.log

然后我们可以发现,Slave已经接管服务,并且担任Master的角色了

然后我们恢复Master的Redis进程

主变成slave

然后把122redis停掉

121恢复主的角色,再把122redis开启

恢复121是主,122redis是备

自动切换成功!