redis配置文件详解及实现主从同步切换

原理:redis复制是怎么进行工作

  1. 如果设置了一个slave,不管是在第一次链接还是重新链接master的时候,slave会发送一个同步命令 然后master开始后台保存,收集所有对修改数据的命令。当后台保存完成,master会将这个数据文件传送到slave,然后保存在磁盘,加载到内存中;master接着发送收集到的所有的修改数据的命令,这好比一个流命令,是redis协议本身来实现的。

  2. 当master和slave因一些故障宕机时,slaves会自动的重链,如果master收到多个slave的同步请求,master会执行一个后台保存,以确保所有的slaves都是正常的。 当master和slave能够维持链接,就会有一个完整的同步进行。

配置redis主从

  • 配置主从同步是很简单的,仅仅在slave的配置文件中增加类似下面这行的内容:

slaveof 192.168.1.1 6379

你可以更换master的ip地址或地址,或者,你可以使用slaveof命令,master就会启动和slave的同步。

  • 设置slave到master的认证

如果master需要通过密码登陆,那就需要配置slave在进行所有同步操作也要使用到密码。 在一个运行的实例上尝试,使用 redis-cli :

config set masterauth <password>

你也可以设置永久的。在配置文件中添加

  masterauth <password>   

redis主从实战

开启主服务器的后台运行,将配置文件中

daemonize no  =》》  daemonize yes

开启从服务器的后台运行,及设置slaveof的IP和端口:

vim redis.conf
port 6379daemonize yes
slaveof 192.168.1.21 6379
masterauth xxxxxxxx

主从切换

停止redis主服务器:

[root@localhost redis]# redis-cli -p 6379 shutdown
[root@localhost redis]# redis-cli -p 6379 
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected>

将redis从服务改为主:

#也可以直接在数据库命令行中输入slaveof no one
[root@localhost redis]# redis-cli -p 6379 slaveof NO ONEOK

假如原来的主redis恢复正常啦。要重新切换回去。

1). 将现在的主redis数据进行保存。

[root@localhost redis]# redis-cli 
127.0.0.1:6379> save
OK

2).将现在的主redis的dump.rdb文件拷贝到原来的主dump.rdb目录下面

[root@localhost redis]# scp /data/redis/dump.rdb 192.168.1.21:/data/redis/dump.rdb

3).启动原来的主redis

[root@localhost ~]# /usr/local/redis/bin/redis-server /usr/local/redis/redis.conf

4).将原来的从依然切换成从

[root@localhost redis]# redis-cli -p 6379 slaveof 192.168.1.21 6379
OK

这时你依然可以读取刚才从库写入的数据,如果不能读取则切换失败。

 

在Master和Slave上安装Keepalived  注意事项:主从的redis都要开启本地备份

$ yum install keepalived

默认安装完成keepalived有默认的配置文件,因此我们重写覆盖它:

首先,在Master上创建如下配置文件
$ vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived
global_defs {
   router_id redis100
}
vrrp_script chk_redis 

     script "/etc/keepalived/scripts/redis_check.sh 127.0.0.1 6379" 
     interval 2 
     timeout 2
     fall 3
}
vrrp_instance redis {
     state MASTER # master set to SLAVE also
     interface eth0  
     virtual_router_id 50 
     priority  150       
     nopreempt # no seize,must add
     advert_int 1
authentication {   #all node must same
         auth_type PASS
         auth_pass 1111
    }
    virtual_ipaddress {  
192.168.0.200/24
    }
    track_script { 
         chk_redis 
    } 
     notify_master "/etc/keepalived/scripts/redis_master.sh 127.0.0.1 192.168.0.101 6379"
     notify_backup "/etc/keepalived/scripts/redis_backup.sh 127.0.0.1 192.168.0.101 6379"
     notify_fault /etc/keepalived/scripts/redis_fault.sh 
     notify_stop /etc/keepalived/scripts/redis_stop.sh 
}

然后,在Slave上创建如下配置文件:

! Configuration File for keepalived

global_defs {
   router_id redis101
}
vrrp_script chk_redis 

     script "/etc/keepalived/scripts/redis_check.sh 127.0.0.1 6379" 
     interval 2
     timeout 2
     fall 3
}
vrrp_instance redis {
    state BACKUP   
    interface eth0   
    virtual_router_id 50  
    priority  100       
    advert_int 1
authentication {   #all node must same
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress { 
    192.168.0.200/24
    }
    track_script { 
         chk_redis 
    } 
    notify_master "/etc/keepalived/scripts/redis_master.sh 127.0.0.1 192.168.0.100 6379"
    notify_backup "/etc/keepalived/scripts/redis_backup.sh 127.0.0.1 192.168.0.100 6379"
    notify_fault /etc/keepalived/scripts/redis_fault.sh 
    notify_stop /etc/keepalived/scripts/redis_stop.sh 
}

在Master和Slave上创建监控Redis的脚本
$ mkdir /etc/keepalived/scripts
$ vim /etc/keepalived/scripts/redis_check.sh

#!/bin/bash 
ALIVE=`/usr/redis/redis-cli -h $1 -p $2 PING` 
LOGFILE="/var/log/keepalived-redis-check.log" 
echo "[CHECK]" >> $LOGFILE
date >> $LOGFILE
if [ $ALIVE == "PONG" ]; then :
   echo "Success: redis-cli -h $1 -p $2 PING $ALIVE" >> $LOGFILE 2>&1
    exit 0 
else 
    echo "Failed:redis-cli -h $1 -p $2 PING $ALIVE " >> $LOGFILE 2>&1
    exit 1 
fi 

编写以下负责运作的关键脚本:
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

首先,在Redis Master上创建notity_master与notify_backup脚本:
$ vim /etc/keepalived/scripts/redis_master.sh

#!/bin/bash 
REDISCLI="/usr/redis/redis-cli -h $1 -p $3" 
LOGFILE="/var/log/keepalived-redis-state.log" 
echo "[master]" >> $LOGFILE 
date >> $LOGFILE 
echo "Being master...." >> $LOGFILE 2>&1  
echo "Run MASTER cmd ..." >> $LOGFILE 2>&1
$REDISCLI SLAVEOF $2 $3 >> $LOGFILE  
sleep 10 #delay 10 s wait data async cancel sync
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1

 

 

$ sudo vim /etc/keepalived/scripts/redis_backup.sh

#!/bin/bash 
REDISCLI="/usr/redis/redis-cli" 
LOGFILE="/var/log/keepalived-redis-state.log" 
echo "[backup]" >> $LOGFILE 
date >> $LOGFILE 
echo "Run SLAVEOF cmd ..." >> $LOGFILE 
$REDISCLI SLAVEOF $2 $3 >> $LOGFILE 2>&1 
# echo "Being slave...." >> $LOGFILE 2>&1 
sleep 15 #delay 15 s wait data sync exchange role 


接着,在Redis Slave上创建notity_master与notify_backup脚本:

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

#!/bin/bash 
REDISCLI="/usr/redis/redis-cli -h $1 -p $3" 
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 $2 $3 >> $LOGFILE  2>&1
#echo "SLAVEOF $2 cmd can't excute ... " >> $LOGFILE 
sleep 10 ##delay 15 s wait data sync exchange role
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/redis/redis-cli" 
LOGFILE="/var/log/keepalived-redis-state.log" 
echo "[BACKUP]" >> $LOGFILE 
date >> $LOGFILE 
echo "Being slave...." >> $LOGFILE 2>&1 
echo "Run SLAVEOF cmd ..." >> $LOGFILE 2>&1
$REDISCLI SLAVEOF $2 $3 >> $LOGFILE  
sleep 100 #delay 10 s wait data async cancel sync 
exit(0)


然后在Master与Slave创建如下相同的脚本:
$ vim /etc/keepalived/scripts/redis_fault.sh

#!/bin/bash 
LOGFILE=/var/log/keepalived-redis-state.log 
echo "[fault]" >> $LOGFILE
date >> $LOGFILE 

$ sudo vim /etc/keepalived/scripts/redis_stop.sh

#!/bin/bash 
LOGFILE=/var/log/keepalived-redis-state.log 
echo "[stop]" >> $LOGFILE 
date >> $LOGFILE 

给脚本都加上可执行权限:

(这点很重要,最开始由于这不没做,运行后一直报错 "VRRP_Instance(Redis) Now in FAULT state")

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

脚本创建完成以后,我们开始按照如下流程进行测试
1.启动Master上的Redis
$ /etc/init.d/redis start

2.启动Slave上的Redis
$ /etc/init.d/redis start

3.启动Master上的Keepalived
$ /etc/init.d/keepalived start

4.启动Slave上的Keepalived
$ /etc/init.d/keepalived start


5.尝试通过VIP连接Redis:
$ redis-cli -h 10.6.1.200 INFO

连接成功,Slave也连接上来了。
role:master
slave0:10.6.1.144,6379,online

6.尝试插入一些数据:
$ redis-cli -h 10.6.1.200 SET Hello Redis
OK

从VIP读取数据
$ redis-cli -h 10.6.1.200 GET Hello
"Redis"

从Master读取数据
$ redis-cli -h 10.6.1.143 GET Hello
"Redis"

从Slave读取数据
$ redis-cli -h 10.6.1.144 GET Hello
"Redis"


下面,模拟故障产生:
将Master上的Redis停了
$ service redis_6379 stop

查看Master上的Keepalived日志
$ tailf /var/log/keepalived-redis-state.log
[fault]
Thu Sep 27 08:29:01 CST 2012

同时Slave上的日志显示:
$ tailf /var/log/keepalived-redis-state.log
[master]
Fri Sep 28 14:14:09 CST 2012
Being master....
Run SLAVEOF cmd ...
OK
Run SLAVEOF NO ONE cmd ...
OK

然后我们可以发现,Slave已经接管服务,并且担任Master的角色了。
$ redis-cli -h 192.168.0.200 INFO

role:master

然后我们恢复Master的Redis进程
$ service redis_6379 start

查看Master上的Keepalived日志
$ tailf /var/log/keepalived-redis-state.log
[master]
Thu Sep 27 08:31:33 CST 2012
Being master....
Run SLAVEOF cmd ...
OK
Run SLAVEOF NO ONE cmd ...
OK

同时Slave上的日志显示:
$ tailf /var/log/keepalived-redis-state.log
[backup]
Fri Sep 28 14:16:37 CST 2012
Being slave....
Run SLAVEOF cmd ...
OK

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

posted @ 2017-09-04 14:04  GeminiMp  阅读(907)  评论(0编辑  收藏  举报