Redis简介:
Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。从2010年3月15日起,Redis的开发工作由VMware主持。
redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set –有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。
keepalived简介:
??Keepalived软件主要是通过VRRP协议实现高可用功能的。VRRP是Virtual Router RedundancyProtocol(虚拟路由器冗余协议)的缩写,VRRP出现的目的就是为了解决静态路由单点故障问题的,它能够保证当个别节点宕机时,整个网络可以不间断地运行。
基本步骤:
1.keepalived+redis 高可用redis主从解决方案
2.两台测试主机安装redis服务
3.在Master redis和Slave redis上安装Keepalived
4.在Master redis和Slave redis上创建配置文件
5.在Master redis和Slave redis上创建各自所需的监控脚本
6.测试redis配置文件和监控脚本是否正常
一:keepalived+redis 高可用redis主从解决方案
环境介绍:
Master: 192.168.1.10
Slave: 192.168.1.17
Virtural IP Address (VIP): 192.168.1.13
设计思路:
当 Master 与 Slave 均运作正常时, Master负责服务,Slave负责Standby;
当 Master 挂掉,Slave 正常时, Slave接管服务,同时关闭主从复制功能;
当 Master 恢复正常,则从Slave同步数据,同步数据之后关闭主从复制功能,恢复Master身份,于此同时Slave等待Master同步数据完成之后,恢复Slave身份。
然后依次循环。
需要注意的是,这样做需要在Master与Slave上都开启本地化策略,否则在互相自动切换的过程中,未开启本地化的一方会将另一方的数据清空,造成数据完全丢失。
二:两台测试主机安装redis服务
1.下载redis安装包及安装
mkdir -p /home/ywxi/tools/ cd /home/ywxi/tools/ wget http://download.redis.io/releases/redis-3.2.0.tar.gz tar -xf redis-3.2.0.tar.gz cd redis-3.2.0 make echo $?
2.添加相关文件及服务命令
mkdir /usr/local/redis/bin/ -p cd src/ cp redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-server redis-sentinel /usr/local/redis/bin/ cd ../ cp redis.conf /etc/
3.添加启动脚本
以下是个完整的脚本,拷贝即可用(注:也可以自己用shell写,判断方法很多)
echo '#chkconfig: 2345 10 90 #description: Start and Stop redis REDISPORT=6379 EXEC=/usr/local/redis/bin/redis-server REDIS_CLI=/usr/local/redis/bin/redis-cli PIDFILE=/var/run/redis.pid CONF="/etc/redis.conf" case "$1" in start) if [ -f $PIDFILE ] then echo "$PIDFILE exists, process is already running or crashed" else echo "Starting Redis server..." $EXEC $CONF fi if [ "$?"="0" ] then echo "Redis is running..." fi ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE does not exist, process is not running" else PID=$(cat $PIDFILE) echo "Stopping ..." $REDIS_CLI -p $REDISPORT SHUTDOWN while [ -x ${PIDFILE} ] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" fi ;; restart|force-reload) ${0} stop ${0} start ;; *) echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2 exit 1 esac ' > /etc/init.d/redis
4.服务开机自启以及创建redis状态日志
chmod +x /etc/init.d/redis chkconfig --add redis chkconfig redis on chkconfig --list| grep redis mkdir /var/log/redis/ -p touch /var/log/redis/redis.log
三:在Master redis和Slave redis上安装Keepalived
1.获取安装包并安装
cd /home/ywxi/tools wget http://www.keepalived.org/software/keepalived-1.2.20.tar.gz tar zxf keepalived-1.2.20.tar.gz cd keepalived-1.2.20 yum -y install openssl openssl-devel ./configure && make && make install echo $?
2.配置文件添加及配置 (很重要,服务启动需要这些文件)
mkdir /etc/keepalived mkdir /usr/local/keepalived/scripts/ -p cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/ cp /usr/local/sbin/keepalived /usr/sbin cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
3.添加开机启动
chmod +x /etc/init.d/keepalived chkconfig --add keepalived chkconfig keepalived on
四:在Master redis和Slave redis上创建配置文件
1)Master redis上创建配置文件
cd /etc/keepalived/ cp keepalived.conf keepalived.conf.bnk echo '! Configuration File for keepalived global_defs { notification_email{ [email protected] } router_id redis140 } vrrp_script chk_redis { script "/usr/local/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 lvs_sync_daemon_interface eth0 virtual_router_id 202 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.186.143 } track_script { chk_redis } notify_master "/usr/local/keepalived/scripts/redis_master.sh 127.0.0.1 192.168.186.141 6379" notify_backup "/usr/local/keepalived/scripts/redis_backup.sh 127.0.0.1 192.168.186.141 6379" notify_fault /usr/local/keepalived/scripts/redis_fault.sh notify_stop /usr/local/keepalived/scripts/redis_stop.sh }' > /etc/keepalived/keepalived.conf
2)Slave redis上创建配置文件
cd /etc/keepalived/ cp keepalived.conf keepalived.conf.bnk echo'! Configuration File for keepalived global_defs { notification_email{ [email protected] } router_id redis141 } vrrp_script chk_redis { script "/usr/local/keepalived/scripts/redis_check.sh 127.0.0.1 6379" #监控脚本 interval 2 timeout 2 fall 3 } vrrp_instance redis { state BACKUP interface eth0 lvs_sync_daemon_interface eth0 virtual_router_id 202 priority 100 nopreempt # no seize,must add advert_int 1 authentication { #all node must same auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.186.143 } track_script { chk_redis } notify_master "/usr/local/keepalived/scripts/redis_master.sh 127.0.0.1 192.168.186.140 6379" notify_backup "/usr/local/keepalived/scripts/redis_backup.sh 127.0.0.1 192.168.1.140 6379" notify_fault /usr/local/keepalived/scripts/redis_fault.sh notify_stop /usr/local/keepalived/scripts/redis_stop.sh }' >/etc/keepalived/keepalived.conf
五:在Master redis和Slave redis上创建各自所需的监控脚本
1).配置master脚本文件(可从线上拷贝再做修改)
脚本目录 /usr/local/keepalived/scripts
以下用于监控Redis的脚本:
mkdir -p /usr/local/keepalived/scripts cd /usr/local/keepalived/scripts echo '#!/bin/bash ALIVE=`/usr/local/redis/bin/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' >/usr/local/keepalived/scripts/redis_check.sh
以下负责运作的关键脚本:
notify_master /usr/local/keepalived/scripts/redis_master.sh
notify_backup /usr/local/keepalived/scripts/redis_backup.sh
notify_fault /usr/local/keepalived/scripts/redis_fault.sh
notify_stop /usr/local/keepalived/scripts/redis_stop.sh
工作原理:
因为Keepalived在转换状态时会依照状态来呼叫:
当进入Master状态时会呼叫notify_master
当进入Backup状态时会呼叫notify_backup
当发现异常情况时进入Fault状态呼叫notify_fault
当Keepalived程序终止时则呼叫notify_stop
2)首先,在Redis Master上创建notity_master与notify_backup脚本:
注:Redis Slave上创建notity_master与notify_backup脚本,与master一致
[[email protected] scripts]# vim redis_master.sh
#!/bin/bash REDISCLI="/usr/local/redis/bin/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 #延迟10秒以后待数据同步完成后再取消同步状态 echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE $REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
notify_backup脚本:
[[email protected] scripts]# vim 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 echo "Run SLAVEOF cmd ..." >> $LOGFILE 2>&1 $REDISCLI SLAVEOF $2 $3 >> $LOGFILE sleep 100 #延迟100秒以后待数据同步完成后再取消同步状态 exit(0)
3)Master额外还要创建两个脚本redis_fault.sh和redis_stop.sh
[[email protected] scripts]# vim redis_fault.sh
#!/bin/bash LOGFILE="/var/log/keepalived-redis-state.log" echo "[fault]" >> $LOGFILE date >> $LOGFILE
[[email protected] scripts]# vim redis_stop.sh
#!/bin/bash LOGFILE="/var/log/keepalived-redis-state.log" echo "[stop]" >> $LOGFILE date >> $LOGFILE
主从记得加上脚本权限:
chmod +x /usr/local/keepalived/scripts/*.sh
设置环境变量:
echo 'export PATH=$PATH:/usr/local/redis/bin' >> /root/.bash_profile
source /root/.bash_profile
启动服务:
/etc/init.d/keepalived restart
/etc/init.d/redis restart
六:测试redis配置文件和监控脚本是否正常
1.启动主从的keepalived和redis服务
检查
/etc/init.d/keepalived restart
redis-server & redis启动方式特殊,加&放在后台运行
netstat -tnlp | grep redis
ps -ef | grep keepalived
ps -ef | grep redis | awk '{print $2}' |xargs kill -9 杀redis进程
redis-server /etc/redis.conf &
2.尝试通过VIP连接Redis:
redis-cli -h 192.168.1.10
redis-cli -h 192.168.1.17
redis-cli -h 192.168.1.13
192.168.1.13:6379> 这是进入正确提示
3.尝试插入一些数据:
redis-cli -h 192.168.1.13 SET Hello Redis
OK
从VIP读取数据
redis-cli -h 192.168.1.13 GET Hello
"Redis"
从Master读取数据
redis-cli -h 192.168.1.10 GET Hello
"Redis"
从Slave读取数据
redis-cli -h 192.168.1.17 GET Hello
"Redis"
4.模拟故障发生:看keepalived是否正常切换
将Master上的Redis进程杀死:
killall -9 redis-server
查看Master上的Keepalived日志
tailf /var/log/keepalived-redis-state.log
同时Slave上的日志显示:
tailf /var/log/keepalived-redis-state.log
然后我们可以发现,Slave已经接管服务,并且担任Master的角色了。
redis-cli -h 192.168.1.13 INFO
redis-cli -h 192.168.1.17 INFO
role:master
然后我们恢复Master的Redis进程
/etc/init.d/redis start
查看Master上的Keepalived日志
tailf /var/log/keepalived-redis-state.log
同时Slave上的日志显示:
tailf /var/log/keepalived-redis-state.log
原文地址:http://blog.51cto.com/13707680/2125358