Redis+keepalived enables Redis master-slave replication:
Environment: CentOs6.5
master:10.10.10.203
slave:10.10.10.204
Virtural IP Address (VIP): 10.10.10.205
Required Software:
Keepalived-1.2.12.tar.gz
Redis-2.8.7.tar.gz
Implementation features:
When Master and Slave are working normally, Master is responsible for service, Slave is responsible for standby;
When Master hangs up, Slave is normal, Slave takes over the service and shuts down the master-slave copy function;
First, the installation of pre-configuration work:
Yum-y install gcc gcc+ gcc-c++ OpenSSL openssl-devel pcre pcre-devel
Redis belongs to the in-memory database, so tuning is focused on memory and needs to be set to 1 vm.overcommit
echo "Vm.overcommit_memory=1" >>/etc/sysctl
Sysctl-p
1. Do the following on the main server 10.10.10.203
echo "10.10.10.203 gc-dabairedis1" >>/etc/hosts
echo "10.10.10.204 Gc-dabairedis2" >>/etc/hosts
2. Do the following from the server 10.10.10.204
echo "10.10.10.203 gc-dabairedis1" >>/etc/hosts
echo "10.10.10.204 Gc-dabairedis2" >>/etc/hosts
Second, install Redis on master and slave:
TAR-ZXVF redis-2.8.7.tar.gz
CD redis-2.8.7
Make && make install
CD src
Mkdir-p/usr/local/redis/bin
CP redis-server REDIS-CLI Redis-benchmark redis-check-aof redis-check-dump/usr/local/redis/bin/
Mkdir-p/usr/local/redis/etc
Copy the redis.conf from the source to the/usr/local/redis/etc/
redis-2.8.7]# CP redis.conf/usr/local/redis/etc/
Modify/usr/local/redis/etc/redis.conf inside can change daemonize No to daemonize Yes
To create a INIT.D startup script:
Vim/etc/init.d/redis-server
chmod o+x/etc/init.d/redis-server
Chkconfig--add Redis-server
/etc/init.d/redis-server start
###########################################################################################
Third, install the keepalived on master and slave:
TAR-ZXVF keepalived-1.2.12.tar.gz
CD keepalived-1.2.12
./configure
Make && make install
cp/usr/local/etc/rc.d/init.d/keepalived/etc/init.d/
cp/usr/local/etc/sysconfig/keepalived/etc/sysconfig/
chmod +x/etc/init.d/keepalived
Chkconfig--add keepalived
Mkdir/etc/keepalived
Ln-s/usr/local/sbin/keepalived/usr/sbin
Keepalived is called according to the state when it is converted:
Notify_master is called when the master State is entered
Notify_backup is called when the backup status is entered
Enter fault status call Notify_fault when abnormal conditions are found
Call Notify_stop when the keepalived program terminates
Create the configuration on master:
Create the configuration file on Master keepalived.conf as follows
Vim/etc/keepalived/keepalived.conf
1 vrrp_script Chk_redis {2Script"/etc/keepalived/scripts/redis_check.sh" 3Interval14 }5 6 vrrp_instance Mes_redis {7 State MASTER8 Interfaceeth09Garp_master_delayTenTen Smtp_alert Onevirtual_router_id3 APriority101 - nopreempt -Advert_int1 the Authentication { - Auth_type PASS -Auth_pass1111 - } + virtual_ipaddress { - 10.10.10.205 + } A Track_script { at Chk_redis - } -notify_master/etc/keepalived/scripts/redis_master.sh -notify_backup/etc/keepalived/scripts/redis_slave.sh -notify_fault/etc/keepalived/scripts/redis_fault.sh -notify_stop/etc/keepalived/scripts/redis_stop.sh in}
Create Notity_master and Notify_backup scripts on the Redis master:
vim/etc/keepalived/scripts/redis_master.sh #!/bin/bashrediscli="/usr/local/redis/bin/redis-cli"LOGFILE="/var/log/keepalived-redis-state.log"Echo"[Master]">>$LOGFILEdate>>$LOGFILEecho"Being master ....">> $LOGFILE2>&1Echo"Run slaveof cmd ...">>$LOGFILE $rediscli slaveof10.10.10.204 6379>> $LOGFILE2>&1Sleep5#延迟10秒以后待数据同步完成后再取消同步状态echo"Run slaveof NO one cmd ...">>$LOGFILE $rediscli slaveof NO One>> $LOGFILE2>&1Vim/etc/keepalived/scripts/redis_backup.sh #!/bin/bashrediscli="/usr/local/redis/bin/redis-cli"LOGFILE="/var/log/keepalived-redis-state.log"Echo"[Backup]">>$LOGFILEdate>>$LOGFILEecho"Being slave ....">> $LOGFILE2>&1Sleep the#延迟15秒待数据被对方同步完成之后再切换主从角色echo"Run slaveof cmd ...">>$LOGFILE $rediscli slaveof10.10.10.204 6379>> $LOGFILE2>&1
To create a configuration on slave:
Keepalived.conf as follows:
vim/etc/keepalived/keepalived.conf
1 vrrp_script Chk_redis {2Script"/etc/keepalived/scripts/redis_check.sh"# # #监控脚本3Interval1# # #监控时间4 }5 6 vrrp_instance Mes_redis {7 State BACKUP # # #设置为BACKUP8 Interfaceeth0 # # #监控网卡9Garp_master_delayTenTen Smtp_alert Onevirtual_router_id3 APriority -# # #比MASTRE权重值低 - nopreempt -Advert_int1 the Authentication { - Auth_type PASS -Auth_pass1111# # #密码与MASTRE相同 - } + virtual_ipaddress { - 10.10.10.205# # #VIP地址 + } A Track_script { at 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 in}
Create Notity_master and Notify_backup scripts on the Redis slave:
vim/etc/keepalived/scripts/redis_master.sh#!/bin/bashrediscli="/usr/local/redis/bin/redis-cli"LOGFILE="/var/log/keepalived-redis-state.log"Echo"[Master]">>$LOGFILEdate>>$LOGFILEecho"Being master ....">> $LOGFILE2>&1Echo"Run slaveof cmd ...">>$LOGFILE $rediscli slaveof10.10.10.203 6379>> $LOGFILE2>&1Sleep5#延迟10秒以后待数据同步完成后再取消同步状态echo"Run slaveof NO one cmd ...">>$LOGFILE $rediscli slaveof NO One>> $LOGFILE2>&1
Vim/etc/keepalived/scripts/redis_backup.sh#!/bin/bashrediscli="/usr/local/redis/bin/redis-cli"LOGFILE="/var/log/keepalived-redis-state.log"Echo"[Backup]">>$LOGFILEdate>>$LOGFILEecho"Being slave ....">> $LOGFILE2>&1Sleep the#延迟15秒待数据被对方同步完成之后再切换主从角色echo"Run slaveof cmd ...">>$LOGFILE $rediscli slaveof10.10.10.203 6379>> $LOGFILE2>&1
Create the same monitoring Redis script on master and slave:
vim/etc/keepalived/scripts/redis_check.sh#!/bin/bashalive= '/opt/redis/bin/redis-cli PING ' if"$ALIVE""PONG " ]; Then echo $ALIVE 0Else echo $ALIVE 1 Fi
Master and slave Create the same script as the following:
vim/etc/keepalived/scripts/redis_fault.sh# !/bin/bashlogfile =/var /log/keepalived-redis- state.logecho " [fault] >> $LOGFILEdate >> $LOGFILEvim /etc/keepalived/scripts/redis_stop.sh# !/bin/bashlogfile =/var /log/keepalived-redis- state.log Echo " [stop] " >> $LOGFILEdate >> $ LOGFILE
Add executable permissions to the script:
chmod +x/etc/keepalived/scripts/*.sh
After the script has been created, we begin to test it in the following process:
1 to start Redis on master /etc/init.d/ redis start 3 startsredis/etc/init.d/redis start on slave 5 start the keepalived on master 6 /etc/init.d/keepalived start 7 start the keepalived on the slave 8 /etc/init.d/keepalived start
Redis+keepalived implementing Redis Master-slave replication