环境架构图:
环境部署
Redis-M | 10.20.0.100 |
Redis-S | 10.20.0.200 |
VIP | 10.20.0.150 |
1,redis安装软件(两台同时安装M S)
#mkdir /home/tools -p
#cd /home/tools
#wget
#yum -y install tcl
#tar zxvf redis-3.0.5.tar.gz
#cd redis-3.0.5
#make MALLOC=libc
#cd src && make all
#make PREFIX=/usr/local/redis install
#LANG=EN
#echo'export PATH=/usr/local/redis/bin/:$PATH' >> /etc/profile
#source /etc/profile
#mkdir /usr/local/redis/conf
#cd /home/tools/redis-3.0.5
#cp redis.conf /usr/local/redis/conf/
#sysctl vm.overcommit_memory=1 如果不执行的话 会报错
#echo never > /sys/kernel/mm/transparent_hugepage/enabled
#redis-server /usr/local/redis/conf/redis.conf & 后台启动
#lsof-i:6379 查看端口
#COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODENAME
#redis-ser5970 root 4u IPv6 36532 0t0 TCP *:6379 (LISTEN)
#redis-ser5970 root 5u IPv4 36534 0t0 TCP *:6379 (LISTEN)
#killall redis-server 杀掉redis进程 内存不够的话
#redis正确关闭方式
#redis-cli shutdown
设计思路:
当 Master 与 Slave 均运作正常时, Master负责服务,Slave负责Standby;
当 Master 挂掉,Slave 正常时,Slave接管服务,同时关闭主从复制功能;
当 Master 恢复正常,则从Slave同步数据,同步数据之后关闭主从复制功能,恢复Master身份,于此同时Slave等待Master同步数据完成之后,恢复Slave身份。
然后依次循环。
需要注意的是,这样做需要在Master与Slave上都开启本地化策略,否则在互相自动切换的过程中,未开启本地化的一方会将另一方的数据清空,造成数据完全丢失。
2,keepalived安装(两台同时安装)
# wget
# keepalived-1.2.19.tar.gz
# cd /software
# tar zxvf keepalived-1.2.19.tar.gz
# cd keepalived-1.2.19
# ./configure
# make && make install
# /bin/cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/ ##生成启动命令
# /bin/cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ ##配置启动脚本的参数
# mkdir /etc/keepalived ##创建默认的keepalived配置文件路径
# /bin/cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ ####把keepalived.conf模板拷贝到/etc/keepalived下
# /bin/cp /usr/local/sbin/keepalived /usr/sbin/
# /etc/init.d/keepalived start 或者/etc/init.d/keepalived stop
查看进程是否起来
#ps -ef | grep keepalived
添加日志
vi /etc/sysconfig/keepalived
KEEPALIVED_OPTIONS="-D -d -S 0"
##参数解释都在#注释里面2. 修改主从节点日志配置文件/etc/rsyslog.conf#vi /etc/rsyslog.conf加入如下配置:#keepalived -S 0local0.* /var/log/keepalived.log3.重启日志服务# /etc/init.d/rsyslog restart4.检查/var/log/keepalived.log文件是否存在
3,设置master上面的配置文件
vim /etc/keepalived/keepalived.conf global_defs { router_id redis100 } 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 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_redis } virtual_ipaddress { 10.20.0.150 } 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,设置Slave配置
vim /etc/keepalived/keepalived.conf global_defs { router_id redis200 } 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 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_redis } virtual_ipaddress { 10.20.0.150 } 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 } |
5,在Master和Slave上创建监控Redis的脚本
# mkdir/etc/keepalived/scripts
# vim/etc/keepalived/scripts/redis_check.sh
cat redis_check.sh #!/bin/bash ALIVE=`/usr/local/redis/bin/redis-cli PING` if [ "$ALIVE" == "PONG" ]; then echo $ALIVE exit 0 else echo $ALIVE exit 1 fi |
6,在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.shnotify_backup /etc/keepalived/scripts/redis_backup.shnotify_fault /etc/keepalived/scripts/redis_fault.shnotify_stop /etc/keepalived/scripts/redis_stop.sh
因为Keepalived在转换状态时会依照状态来呼叫:当进入Master状态时会呼叫notify_master当进入Backup状态时会呼叫notify_backup当发现异常情况时进入Fault状态呼叫notify_fault当Keepalived程序终止时则呼叫notify_stop
7,首先,在Redis Master上创建notity_master与notify_backup两个文件脚本:
# vim/etc/keepalived/scripts/redis_master.sh
cat redis_master.sh #!/bin/bash REDISCLI="/usr/local/redis/bin/redis-cli" LOGFILE="/var/log/keepalived-redis-state.log" echo "[master]" >> $LOGFILE echo "Being master...." >> $LOGFILE 2>&1 echo "Run SLAVEOF cmd ..." >> $LOGFILE $REDISCLI SLAVEOF 10.20.0.200 6379 >> $LOGFILE 2>&1 sleep 10 echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE $REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1 |
# vim /etc/keepalived/scripts/redis_backup.sh
cat redis_backup.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 echo "Run SLAVEOF cmd ..." >> $LOGFILE $REDISCLI SLAVEOF 10.20.0.200 6379 >> $LOGFILE 2>&1 |
接着,在RedisSlave从服务器上创建notity_master与notify_backup脚本:
# vim/etc/keepalived/scripts/redis_master.sh
cat redis_master.sh #!/bin/bash REDISCLI="/usr/local/redis/bin/redis-cli" LOGFILE="/var/log/keepalived-redis-state.log" echo "[master]" >> $LOGFILE echo "Being master...." >> $LOGFILE 2>&1 echo "Run SLAVEOF cmd ..." >> $LOGFILE $REDISCLI SLAVEOF 10.20.0.100 6379 >> $LOGFILE 2>&1 sleep 10 echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE $REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1 |
# vim /etc/keepalived/scripts/redis_backup.sh
cat redis_backup.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 echo "Run SLAVEOF cmd ..." >> $LOGFILE $REDISCLI SLAVEOF 10.20.0.100 6379 >> $LOGFILE 2>&1 |
8,然后在master和slave服务器各自创建相同的脚本
# vim /etc/keepalived/scripts/redis_fault.sh
cat redis_fault.sh #!/bin/bash LOGFILE=/var/log/keepalived-redis-state.log echo "[fault]" >> $LOGFILE date >> $LOGFILE |
# vim /etc/keepalived/scripts/redis_stop.sh
# cat redis_stop.sh #!/bin/bash LOGFILE=/var/log/keepalived-redis-state.log echo "[stop]" >> $LOGFILE date >> $LOGFILE |
主从服务器:给脚本加上可执行的权限
# chmod +x/etc/keepalived/scripts/*.sh
# chmod +x/etc/keepalived/scripts/*.sh
9,脚本创建完成以后,我们开始按照如下流程进行测试:
1.启动Master上的Redis
2.启动Slave上的Redis
3.启动Master上的Keepalived
/etc/init.d/keepalivedstart
4.启动Slave上的Keepalived
/etc/init.d/keepalivedstart