redis主从+keepalived高可用自动切换

一环境介绍

法库网站制作公司哪家好,找创新互联公司!从网页设计、网站建设、微信开发、APP开发、响应式网站建设等网站项目制作,到程序开发,运营维护。创新互联公司自2013年创立以来到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联公司

 master: 172.168.1.69

slave  :172.168.1.57      

VIP     172.168.1.253   虚拟IP

 

二。设计思路:

 

当 Master 与 Slave 均运作正常时, Master负责服务,Slave负责Standby;

 

当 Master 挂掉,Slave 正时, Slave接管服务,同时关闭主从复制功能;

 

当 Master 恢复正常,则从Slave同步数据,同步数据之后关闭主从复制功能,恢复Master身份,于此同时Slave等待Master同步数据完成之后,恢复Slave身份。

 

然后依次循环。

 

需要注意的是,这样做需要在Master与Slave上都开启本地化策略,否则在互相自动切换的过程中,未开启本地化的一方会将另一方的数据清空,造成数据完全丢失。(配置方式,打开redis的配置文件。找到appendonly。默认是appendonly no。改成appendonly yes。)

 redis配置文件不可设置只读模式

 

 

三。安装前准备工作

 

1.在主服务器172.168.1.69 上面做下面操作

 

echo "172.168.1.69  test01" >> /etc/hosts

 

echo "172.168.1.57 test" >> /etc/hosts

 

2.在从服务器172.168.1.57上面做下面操作

 

echo "172.168.1.69  test01" >> /etc/hosts

 

echo "172.168.1.57 test" >> /etc/hosts

 

四:配置redis主从 (略)

 

端口号 :P :6379

密码:123456

 

 启动服务:

# /mnt/tools/redis/bin/redis-server /mnt/tools/redis/etc/redis.conf

 

 

主服务器上执行:

#/mnt/tools/redis/bin/redis-cli -h172.168.1.69 -a 123456 set test 123456

 

 

从服务器上执行:

#/mnt/tools/redis/bin/redis-cli -h 172.168.1.57 get test

 

测试是否已启动

# /mnt/tools/redis/bin/redis-cli ping

 

 

性能测试

# /mnt/tools/redis/bin/redis-benchmark

 

 

关闭服务

#/mnt/tools/redis/bin/redis-cli -p 6379 shutdown

 

 

 

 

五:安装配置keepalived (直接yum安装也可以  方便  简单粗暴  不用自己装依赖包)

1.mkdir /root/tools -p

cd /root/tools

wget http://www.keepalived.org/software/keepalived-1.1.17.tar.gz

tar -zxf keepalived*

 

 

2.做一个内核连接

[root@zjx tools]# uname -r

3.10.0-123.9.3.el7.x86_64

[root@zjx tools]# ln -s /usr/src/kernels/3.10.0-123.9.3.el7.x86_64 /usr/src/linux

 

cd keepalived-1.1.17

 

 

 ./configure      error(yum install -y openssl openssl-devel         yum install popt-devel)

 

make && make install 

 

移动启动脚本和配置文件

[root@zjx /]# cp /usr/local/etc/rc.d/init.d/keepalived  /etc/init.d/

[root@zjx /]# cp /usr/local/etc/sysconfig/keepalived  /etc/sysconfig/

[root@zjx /]# mkdir /etc/keepalived   启动脚本中默认指定的配置文件路径

[root@zjx /]# /bin/cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/

[root@zjx /]# /bin/cp /usr/local/sbin/keepalived  /usr/sbin/

执行上述操作后就可以以 /etc/init.d/keepalived 的方式启动了

 

/etc/init.d/keepalived start

 

/etc/init.d/keepalived stop

 

/etc/init.d/keepalived restart

 

3.master   keepalived.cnf 配置文件

 

 

! Configuration File for keepalived

 

 

vrrp_script chk_redis {

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

    interval 2

    }

vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id 51

    mcast_src_ip172.168.1.253

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

 

 

track_script {

    chk_redis

    }

virtual_ipaddress {

       172.168.1.253

    }

        notify_master /etc/keepalived/scripts/redis_master.sh

 

        notify_backup /etc/keepalived/scripts/redis_backup.sh

 

        notify_fault  /etc/keepalived/scripts/redis_fault.sh

 

        notify_stop  /etc/keepalived/scripts/redis_stop.sh

}

 

 

 

slave:keepalived.cnf 配置文件

 

! Configuration File for keepalived

vrrp_script chk_redis {

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

    interval 2

}

vrrp_instance VI_1 {

    state BACKUP

    interface eth0

    virtual_router_id 51

mcast_src_ip172.168.1.253

    priority 10

advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

track_script {

    chk_redis

    }

virtual_ipaddress {

       172.168.1.253

    }

         notify_master /etc/keepalived/scripts/redis_master.sh

 

        notify_backup /etc/keepalived/scripts/redis_backup.sh

 

        notify_fault  /etc/keepalived/scripts/redis_fault.sh

 

        notify_stop  /etc/keepalived/scripts/redis_stop.sh

}

 

 

 

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

 

$  mkdir /etc/keepalived/scripts

 

$  vim /etc/keepalived/scripts/redis_check.sh

 

 

#!/bin/bash

 

ALIVE=`/mnt/tools/redis/bin/redis-cli -a 123456 PING`

 

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

 

echo $ALIVE

 

exit 0

 

else

 

echo $ALIVE

 

exit 1

 

fi

 

 

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

 

notify_master /etc/keepalived/scripts/redis_master.sh

 

notify_backup /etc/keepalived/scripts/redis_backup.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

 

1)首先,在Redis Master上创建notity_master与notify_backup脚本:

 

$  vim /etc/keepalived/scripts/redis_master.sh

 

 

 

#!/bin/bash

 

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

 

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 172.168.1.57 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_backup.sh

 

 

 

 

 

#!/bin/bash

 

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

 

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 172.168.1.57 6379 >> $LOGFILE  2>&1

 

 

2)在Redis Slave上创建notity_master与notify_backup脚本:

 

$  vim /etc/keepalived/scripts/redis_master.sh

 

 

 

 

 

#!/bin/bash

 

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

 

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

 

 

 

echo "[master]" >> $LOGFILE

 

date >> $LOGFILE

 

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

 

 

 

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

 

$REDISCLI SLAVEOF172.168.1.69 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_backup.sh

 

 

 

 

 

#!/bin/bash

 

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

 

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 SLAVEOF172.168.1.69 6379 >> $LOGFILE  2>&1

 

 

 

3)然后在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

 

 

6.在主从服务器上面给脚本都加上可执行权限:

 

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

 

 

7.进行相关功能测试

 

启动Master和slave上的Redis

 

$  /mnt/tools/redis/bin/redis-server /mnt/tools/redis/etc/redis.conf

 

启动Master和slave上的Keepalived

 

$     

 

 

尝试通过VIP连接Redis:

 

$ redis-cli -h 172.168.1.253 INFO

 

连接成功,Slave也连接上来了。

 

role:master

 

slave0:172.168.1.57,6379,online

 

尝试插入一些数据:

 

$ redis-cli -h 172.168.1.69 SET Hello Redis

 

OK

 

 从VIP读取数据

 

$ redis-cli -h 172.168.1.253 GET Hello

 

"Redis"

 

从Master读取数据   

 

$ redis-cli -h172.168.1.69 GET Hello

 

"Redis"

 

从Slave读取数据

 

$ redis-cli -h 172.168.1.57 GET Hello

 

"Redis"

 

8.通过模拟相关故障,进行功能测试()

 

将Master上的Redis进程杀死:

killall -9 redis-server

 

查看Master上的Keepalived日志

 

查看Master上的Keepalived日志

 

$ tail -f /var/log/keepalived-redis-state.log

 

[fault]

 

Thu Sep 27 08:29:01 CST 2012

 

 

 

 

同时Slave上的日志显示:

 

$ tail -f /var/log/keepalived-redis-state.log

 

[master]

 

 

Being master....

 

Run SLAVEOF cmd ...

 

OK Already connected to specified master

 

Run SLAVEOF NO ONE cmd ...

 

OK

 

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

 

$ redis-cli -h 172.168.1.253 INFO

 

$ redis-cli -h 172.168.1.57 INFO

 

role:master

 

 

 

 

然后我们恢复Master的Redis进程(恢复主redis后,需要重启keepalived  和从库的redis  可恢复正常)

 

$  /etc/init.d/redis start

 

查看Master上的Keepalived日志

 

$ tail -f /var/log/keepalived-redis-state.log

 

[master]

 

 

Being master....

 

Run SLAVEOF cmd ...

 

OK

 

Run SLAVEOF NO ONE cmd ...

 

OK

 

同时Slave上的日志显示:

 

$ tail -f /var/log/keepalived-redis-state.log

 

[backup]

 

 

Being slave....

 

Run SLAVEOF cmd ...

 

OK

 

发现目前的Master已经再次恢复了Master的角色,故障切换以及自动恢复都成功了。

 

 


网站标题:redis主从+keepalived高可用自动切换
分享URL:http://csdahua.cn/article/gsddcp.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流