1、大家可以先看我的单台Redis安装教程,链接在此点击Redis在CentOS for LInux上安装详细教程
2、第一台redis配置,是正常配置。作为MASTER主服务器,第二台redis的配置文件中
加 一行代码 slaveof 主redis的IP 6379
例如我的主redis的ip是192.168.1.2
我在从redis配置文件中加一行
从: slaveof 192.168.1.2 6379
3、然后启动从redis服务器,主从复制就搭建好了。当然你的配置文件中,如果对日志文件有要求,可以这样修改
主: logfile "/var/log/redis/redis_master.log"
从: logfile "/var/log/redis/redis_slava.log"
4、此时如果主master服务器宕机了,此时主从复制就彻底垮掉了,如果你的项目挂载了redis主从复制,则项目就会出错。
5、解决redis主从复制,主服务器宕机,从服务器充当主服务器,即主从双机热备技术。使用的技术服务是Keepalived高可用。
6、Linux for CentOS 下的 keepalived 安装与卸载以及相关命令操作之详细教程 点击链接进入Keepalived的安装教程。
7、环境准备工作
准备两台Linux服务器:
1. redis主服务器地址:192.168.1.10 并且该服务器已经安装Keepalived服务
2.redis从服务器地址:192.168.1.11 并且该服务器已经安装Keepalived服务
3.备用一个虚拟IP地址:192.168.1.20(用来做IP地址漂移)
4.此两台服务器已经做好主从复制的配置。
8.登录主redis服务器 192.168.1.10
9.进入/etc/keepalived/keepalived.conf编辑配置文件
global_defs {
router_id redis1
script_user root
enable_script_security
}
vrrp_script chk_redis
{
script "/etc/keepalived/scripts/redis_check.sh 127.0.0.1 6379"
interval 2
timeout 2
fall 3
weight -20
}
vrrp_instance redis {
state MASTER
interface eth0
virtual_router_id 100
priority 100
#nopreempt # no seize,must add
advert_int 1
authentication { #all node must same
auth_type PASS
auth_pass 1111
}
unicast_src_ip 192.168.1.10
unicast_peer {
192.168.1.11
}
virtual_ipaddress {
192.168.1.20
}
track_script {
chk_redis
}
notify_master "/etc/keepalived/scripts/redis_master.sh 127.0.0.1 192.168.1.11 6379"
notify_backup "/etc/keepalived/scripts/redis_backup.sh 127.0.0.1 192.168.1.11 6379"
notify_fault /etc/keepalived/scripts/redis_fault.sh
notify_stop /etc/keepalived/scripts/redis_stop.sh
}
10. 建立目录 make /etc/keepalived/scripts 用来存放脚本
redis_backup.sh
redis_check.sh
redis_fault.sh
redis_master.sh
redis_stop.sh
每个脚本代码如下:
【redis_backup.sh】
#!/bin/bash
REDISCLI="/usr/local/bin/redis-cli -h $1 -p $3"
LOGFILE="/etc/keepalived/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_check.sh】
#!/bin/bash
ALIVE=`/usr/local/bin/redis-cli -h $1 -p $2 PING`
LOGFILE="/etc/keepalived/log/keepalived-redis-check.log"
echo "[CHECK]" >> $LOGFILE
date >> $LOGFILE
if [[ $ALIVE == "PONG" ]]; then :
echo "SUCCESS:reidis-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
【redis_fault.sh】
#!/bin/bash
LOGFILE=/etc/keepalived/log/keepalived-redis-state.log
echo "[fault]" >> $LOGFILE
date >> $LOGFILE
【redis_master.sh】
#!/bin/bash
REDISCLI="/usr/local/bin/redis-cli -h $1 -p $3"
LOGFILE="/etc/keepalived/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
【redis_stop.sh】
#!/bin/bash
LOGFILE=/etc/keepalived/log/keepalived-redis-state.log
echo "[stop]" >> $LOGFILE
date >> $LOGFILE
注释:脚本代码,如果和你路径不一样,可根据自己的适当修改
给脚本赋执行权限:chmod a+x /etc/keepalived/scripts/*.sh
11.登录从redis服务器 192.168.1.11
12.进入/etc/keepalived/keepalived.conf编辑配置文件
global_defs {
router_id redis1
script_user root
enable_script_security
}
vrrp_script chk_redis
{
script "/etc/keepalived/scripts/redis_check.sh 127.0.0.1 6379"
interval 2
timeout 2
fall 3
weight -20
}
vrrp_instance redis {
state BACKUP
interface eth0
virtual_router_id 100
priority 90
#nopreempt # no seize,must add
advert_int 1
authentication { #all node must same
auth_type PASS
auth_pass 1111
}
unicast_src_ip 192.168.1.11
unicast_peer {
192.168.1.10
}
virtual_ipaddress {
192.168.1.20
}
track_script {
chk_redis
}
notify_master "/etc/keepalived/scripts/redis_master.sh 127.0.0.1 192.168.1.10 6379"
notify_backup "/etc/keepalived/scripts/redis_backup.sh 127.0.0.1 192.168.1.10 6379"
notify_fault /etc/keepalived/scripts/redis_fault.sh
notify_stop /etc/keepalived/scripts/redis_stop.sh
}
主服务器的脚本,在从服务器这边也要复制一份过来。
注释:脚本代码,如果和你路径不一样,可根据自己的适当修改
给脚本赋执行权限:chmod a+x /etc/keepalived/scripts/*.sh
温馨提示:
1.keepalived的主备状态与state值设置无关,是简单的标识;
2.主备机由priority值和vrrp_script中的weight值之和决定,大的为主;
3.主备比较权值=priority值+weight值*标志位,当vrrp_script检测脚本为true时标志位为1,反之为0;
4.为保证正常的主备切换,weight值应大于主备priority值之差。
此时启动redis 和keepalived服务吧!
然后再主服务器输入: ip add 查看虚拟ip目前是否在主服务器上。
。。。。。
下面就开始做测试了,比如把主服务器的redis关闭,测试从服务器redis的读写情况。
开启主服务器redis,看看从服务器redis此时会主备切换不。
相信自己,一切皆有可能!