High-availability installation of Redis in CentOS
High-availability installation of Redis in CentOS [x] environment Introduction:
Master: T1
Slave: T2
VIP: 192.168.68.45
[X] installing Redis (Master, Slave)
Note: The flushall modification before redis is installed
Searchsrc/redis.c
File, put
`{"flushdb",flushdbCommand,1,"w",0,NULL,0,0,0,0,0},` `{"flushall",flushallCommand,1,"w",0,NULL,0,0,0,0,0},`
Modify:
`{"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. add a line to conf: 'vm. overcommit_memory = 1' mkdir/opt/redismkdir/opt/redis/logmkdir/opt/redis/dbtar zxvf. /redis-2.8.17.tar.gzcd redis-2.8.17make PREFIX =/opt/redis install
[X] Redis STARTUP script (Master, Slave):/opt/redis/bin/startRedis. sh
#!/bin/bashbasedir=`dirname $0`echo "Redis BASE DIR:$basedir"cd $basedirnohup ./redis-server ./redis.conf > /dev/null 2>&1 &
[X] Redis stop script (Master, Slave):/opt/redis/bin/StopRedis. sh
#!/bin/shbasedir=`dirname $0`echo "Redis BASE DIR:$basedir"cd $basedir./redis-cli -h localhost -a 123456 shutdown
[X] Redis configuration file (Master, Slave):/opt/redis/bin/redis. conf
# Requirepass 123456 pidfile/opt/redis/bin/redis. pidlogfile/opt/redis/log/redis. logdir/opt/redis/db/daemonize yesport 6379 timeout 300 loglevel warningdatabases 16 maxmemory 1g # Do not take snapshots # save 900 1 # save 300 10 # save 60 10000 # rdbcompression yes # dbfilename dump. rdb # Use AOFappendonly yesappendfsync everysec
[X] install Keepalived (Master, Slave ):
#wget http://www.keepalived.org/software/keepalived-1.2.13.tar.gztar zxvf keepalived-1.2.13.tar.gzcd ./keepalived-1.2.13./configuremake make installcp /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/keepalivedcp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/chkconfig --add keepalived#chkconfig --level 35 keepalived on#service keepalived start
[X] notify _ * Explanation
Keepalived will call in accordance with the status when switching to the status: when it enters the Master Status, it will call yy_master. When it enters the Backup status, it will call yy_backup. When an exception is found (track_script, track_interface failed) in the Fault status, call policy_fault. When the Keepalived program is terminated, call policy_stop.
[X] create the following configuration file on the Master:
vim /etc/keepalived/keepalived.conf
! Configuration File for login {icationication_email {root @ localhost} restart root @ localhost smtp_server localhost route 30 router_id LVS_REDIS} vrrp_script chk_redis {script "/etc/keepalived/scripts/redis_check.sh" ### monitoring script interval 2 ### monitoring time} vrrp_instance VI_1 {nopreempt ### do not preemptible, prevent split-brain state MASTER # BACKUP is BACKUP interface br0 virtual_router_id 51 priority 100 # BACKUP is 90 advert_int 1 authentication {auth_type PASS auth_pass redis} virtual_ipaddress {192.168.68.45} track_script {chk_redis ## run chk_redis} yy_master/etc/keepalived/scripts/redis_master.sh notify_backup/etc/keepalived/scripts/zookeeper/etc/keepalived/scripts/redis_fault.sh policy_stop/etc/keepalived/scripts/ redis_stop.sh}
[X] create the following configuration file on Slave:
vim /etc/keepalived/keepalived.conf
! Configuration File for login {icationication_email {root @ localhost} restart root @ localhost smtp_server localhost route 30 router_id LVS_REDIS} vrrp_script chk_redis {script "/etc/keepalived/scripts/redis_check.sh" ### monitoring script interval 2 ### monitoring time} vrrp_instance VI_1 {nopreempt ### do not preemptible, prevent split-brain state BACKUP # BACKUP is BACKUP interface br0 virtual_router_id 51 priority 90 # BACKUP is 90 advert_int 1 authentication {auth_type PASS auth_pass redis} virtual_ipaddress {192.168.68.45} track_script {chk_redis ## Execute chk_redis} yy_master/etc/keepalived/scripts/redis_master.sh notify_backup/etc/keepalived/scripts/zookeeper/etc/keepalived/scripts/redis_fault.sh policy_stop/etc/keepalived/scripts/ redis_stop.sh}
[X] Create a Redis monitoring script on the Master and Slave:
mkdir /etc/keepalived/scripts/
vi /etc/keepalived/scripts/redis_check.sh
#!/bin/bashC_DATE=`date +"[%Y-%m-%d %H:%M:%S]"`ALIVE=`/opt/redis/bin/redis-cli PING`if [ "$ALIVE" == "PONG" ]; then echo "${C_DATE} $ALIVE" exit 0else echo "${C_DATE} $ALIVE" exit 1fi
[X] create the following script in Master and Slave
notify_fault
And
notify_stop
:
vim /etc/keepalived/scripts/redis_fault.sh
#!/bin/bashC_DATE=`date +"[%Y-%m-%d %H:%M:%S]"`LOGFILE=/var/log/keepalived-redis-state.logecho "${C_DATE} [fault]" >> $LOGFILE
vim /etc/keepalived/scripts/redis_stop.sh
#!/bin/bashC_DATE=`date +"[%Y-%m-%d %H:%M:%S]"`LOGFILE=/var/log/keepalived-redis-state.logecho "${C_DATE} [stop]" >> $LOGFILE
[X] Create on Master
notity_master
And
notify_backup
Script:
vim /etc/keepalived/scripts/redis_master.sh
#! /Bin/bashC_DATE = 'date + "[% Y-% m-% d % H: % M: % S] "'rediscli ="/opt/redis/bin/redis-cli "LOGFILE ="/var/log/keepalived-redis-state.log "echo" $ {C_DATE} [master] ">> $ LOGFILE # When keepalived is configured as "preemptible, open the following comment # echo "Being master .... ">>$ LOGFILE 2> & 1 # echo" Run SLAVEOF cmd... ">>$ LOGFILE # $ rediscli slaveof T2 6379 >>$ LOGFILE 2> & 1 # sleep 10 # Wait 10 seconds until the data synchronization is complete and then cancel the synchronization status echo" Run SLAVEOF no one cmd... ">>$ LOGFILE $ rediscli slaveof no one >>$ LOGFILE 2> & 1
vim /etc/keepalived/scripts/redis_backup.sh
#! /Bin/bashC_DATE = 'date + "[% Y-% m-% d % H: % M: % S] "'redis cli ="/opt/redis/bin/redis-cli "LOGFILE ="/var/log/keepalived-redis-state.log "echo" $ {C_DATE} [backup] ">> $ LOGFILE # When keepalived is configured as "preemptible, open the following comment # echo "Being slave .... ">>$ LOGFILE 2> & 1 # sleep 15 # Wait for 15 seconds until the data is synchronized by the other party and then switch the master-slave role echo" Run SLAVEOF cmd... ">>$ LOGFILE $ rediscli slaveof T2 6379 >>$ LOGFILE 2> & 1
[X] Create on Slave
notity_master
And
notify_backup
Script:
vim /etc/keepalived/scripts/redis_master.sh
#! /Bin/bashC_DATE = 'date + "[% Y-% m-% d % H: % M: % S] "'rediscli ="/opt/redis/bin/redis-cli "LOGFILE ="/var/log/keepalived-redis-state.log "echo" $ {C_DATE} [master] ">> $ LOGFILE # When keepalived is configured as "preemptible, open the following comment # echo "Being master .... ">>$ LOGFILE 2> & 1 # echo" Run SLAVEOF cmd... ">>$ LOGFILE # $ rediscli slaveof T1 6379 >>$ LOGFILE 2> & 1 # sleep 10 # Wait 10 seconds until the data synchronization is complete and then cancel the synchronization status echo" Run SLAVEOF no one cmd... ">>$ LOGFILE $ rediscli slaveof no one >>$ LOGFILE 2> & 1
vim /etc/keepalived/scripts/redis_backup.sh
#! /Bin/bashC_DATE = 'date + "[% Y-% m-% d % H: % M: % S] "'redis cli ="/opt/redis/bin/redis-cli "LOGFILE ="/var/log/keepalived-redis-state.log "echo" $ {C_DATE} [backup] ">> $ LOGFILE # When keepalived is configured as "preemptible, open the following comment # echo "Being slave .... ">>$ LOGFILE 2> & 1 # sleep 15 # Wait for 15 seconds until the data is synchronized by the other party and then switch the master-slave role echo" Run SLAVEOF cmd... ">>$ LOGFILE $ rediscli slaveof T1 6379 >>$ LOGFILE 2> & 1
[X] on the Master and Slave, add executable permissions to the monitoring scripts:
chmod +x /etc/keepalived/scripts/*.sh
[X] startup steps:
Start Redis on the Master
/opt/redis/bin/startRedis.sh
Start Redis on Slave
/opt/redis/bin/startRedis.sh
Start Keepalived on the Master
service keepalived start
Start Keepalived on Slave
service keepalived start
You may also like the following articles about Redis. For details, refer:
Install and test Redis in Ubuntu 14.04
Basic configuration of Redis master-slave Replication
Redis cluster details
Install Redis in Ubuntu 12.10 (graphic explanation) + Jedis to connect to Redis
Redis series-installation, deployment, and maintenance
Install Redis in CentOS 6.3
Learning notes on Redis installation and deployment
Redis. conf
Redis details: click here
Redis: click here
This article permanently updates the link address: