CentOS下Redis高可用安装过程 [x] 安装环境介绍:
Master: T1
Slave: T2
VIP: 192.168.68.45
注意: 安装redis前flushall的修改
查找src/redis.c文件,把
`{"flushdb",flushdbCommand,1,"w",0,NULL,0,0,0,0,0},` `{"flushall",flushallCommand,1,"w",0,NULL,0,0,0,0,0},`修改成:
`{"xflushdb",flushdbCommand,1,"w",0,NULL,0,0,0,0,0},` `{"xflushall",flushallCommand,1,"w",0,NULL,0,0,0,0,0},` vi /etc/sysctl.conf 添加一行: `vm.overcommit_memory=1` mkdir /opt/redis mkdir /opt/redis/log mkdir /opt/redis/db tar zxvf ./redis-2.8.17.tar.gz cd redis-2.8.17 make PREFIX=/opt/redis install [x] Redis启动脚本(Master,Slave):/opt/redis/bin/startRedis.sh #!/bin/bash basedir=`dirname $0` echo "Redis BASE DIR:$basedir" cd $basedir nohup ./redis-server ./redis.conf > /dev/null 2>&1 & [x] Redis停止脚本(Master,Slave):/opt/redis/bin/StopRedis.sh #!/bin/sh basedir=`dirname $0` echo "Redis BASE DIR:$basedir" cd $basedir ./redis-cli -h localhost -a 123456 shutdown [x] Redis配置文件(Master,Slave):/opt/redis/bin/redis.conf #requirepass 123456 pidfile /opt/redis/bin/redis.pid logfile /opt/redis/log/redis.log dir /opt/redis/db/ daemonize yes port 6379 timeout 300 loglevel warning databases 16 maxmemory 1g #不要快照 #save 900 1 #save 300 10 #save 60 10000 #rdbcompression yes #dbfilename dump.rdb #使用AOF appendonly yes appendfsync everysec [x] 安装Keepalived(Master,Slave): #wget http://www.keepalived.org/software/keepalived-1.2.13.tar.gz tar zxvf keepalived-1.2.13.tar.gz cd ./keepalived-1.2.13 ./configure make make install cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ cp /usr/local/sbin/keepalived /usr/sbin/ mkdir /etc/keepalived cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ chkconfig --add keepalived #chkconfig --level 35 keepalived on #service keepalived start [x] notify_*解释 Keepalived在转换状态时会依照状态来呼叫: 当进入Master状态时会呼叫notify_master 当进入Backup状态时会呼叫notify_backup 当发现异常情况时(track_script,track_interface失败)进入Fault状态呼叫notify_fault 当Keepalived程序终止时则呼叫notify_stop [x] 在Master上创建如下配置文件:vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from root@localhost smtp_server localhost smtp_connect_timeout 30 router_id LVS_REDIS } vrrp_script chk_redis { script "/etc/keepalived/scripts/redis_check.sh" ###监控脚本 interval 2 ###监控时间 } vrrp_instance VI_1 { nopreempt ###不抢占,防止脑裂 state MASTER #备的是BACKUP interface br0 virtual_router_id 51 priority 100 #备的是90 advert_int 1 authentication { auth_type PASS auth_pass redis } virtual_ipaddress { 192.168.68.45 } track_script { chk_redis ###执行上面定义的chk_redis } 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 } [x] 在Slave上创建如下配置文件: