资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

redis主从+keepalived高可用自动切换-创新互联

一环境介绍

站在用户的角度思考问题,与客户深入沟通,找到泰山网站设计与泰山网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站设计制作、成都做网站、企业官网、英文网站、手机端网站、网站推广、域名与空间、虚拟主机、企业邮箱。业务覆盖泰山地区。

 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 -h 172.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_ip 172.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_ip 172.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 SLAVEOF 172.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 SLAVEOF 172.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 -h 172.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的角色,故障切换以及自动恢复都成功了。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


文章名称:redis主从+keepalived高可用自动切换-创新互联
本文地址:http://cdkjz.cn/article/gpiep.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220