Redis + keepalived Master-slave cluster construction and failover

Source: Internet
Author: User
Tags failover redis sleep server port redis server
Redis + keepalived master-slave cluster construction and failover Design ideas:When Master and Slave are working normally, master is in charge of the service, Slave is responsible for standby; When Master hangs up, Slave is normal, Slave takes over the service while shutting down the master-slave copy function; When Master returns to normal, the data is synchronized from Slave , after synchronizing the data to turn off the master-slave replication function, restore master identity, at the same time slave wait for master synchronization data to complete, restore slave identity. Then loop in turn. Precautions :

The localization policy needs to be turned on both master and slave, otherwise the one party that does not open the localization will empty the data of the other party in the process of auto-switching with each other, causing the data to be completely lost. Environment Preparation

A cluster can be a different port for a single machine, or it can be multiple machines, often in practical applications, with multiple machines. Assuming that 2 machines are currently installed, IP and ports are

Primary Redis server ip:192.168.1.148
Master Redis Server port number: 6379 from
redis server ip:192.168.1.158
from Redis server port number: 6379

Set up a virtual IP

192.168.1.200
go to root user
xiaoyao@xiaoyao-virtual-machine:~$ su
password: 
root@xiaoyao-virtual-machine:/home/xiaoyao# 
separate installation of keepalived on both master and slave machines
root@xiaoyao-virtual-machine:/home/xiaoyao# Apt-get Install keepalived
master server modifies hosts file separately
root@xiaoyao-virtual-machine:/home/xiaoyao# vim/etc/hosts
Add
192.168.1.148 redis148
at the end of the file 192.168.1.158 redis158
master server creation configuration file
root@xiaoyao-virtual-machine:/home/xiaoyao# vim/etc/keepalived/keepalived.conf

Configuration content:

! Configuration File for keepalived global_defs {router_id redis148} vrrp_script Chk_redis {script '/etc/keepal ived/scripts/redis_check.sh 127.0.0.1 6379 "Interval 2 timeout 2 fall 3} vrrp_instance Redis {State MA  STER # Master set to SLAVE also interface eth0 virtual_router_id
    Seize,must Add Advert_int 1 Authentication {#all node must same auth_type PASS auth_pass 1111 } virtual_ipaddress {192.168.1.200/24} track_script {Chk_redis} notify _master "/etc/keepalived/scripts/redis_master.sh 127.0.0.1 192.168.1.158 6379" Notify_backup "/etc/keepalived/ scripts/redis_backup.sh 127.0.0.1 192.168.1.158 6379 "notify_fault/etc/keepalived/scripts/redis_fault.sh notify_ STOP/ETC/KEEPALIVED/SCRIPTS/REDIS_STOP.SH} 

This can be an error, the general virtual machine is configured to: interface eth0, but some machines may be em1, this should be configured according to the situation, otherwise the creation of virtual IP will fail .
As an example:
Look at the IP after it looks like this

root@ubuntu:/etc/init.d# cat/etc/network/interfaces
# This file describes the network interfaces available on your SY Stem # and how to
activate them. For more information, see Interfaces (5).

# The Loopback network interface
Auto em1
iface em1 inet static
address 192.168.0.33
netmask 255.255.255.0
Gateway 192.168.0.1

Then it should be configured as

Interface Em1
create a profile from the server
root@xiaoyao-virtual-machine:/home/xiaoyao# vim/etc/keepalived/keepalived.conf

Configuration content:

! Configuration File for keepalived

global_defs {
   router_id redis158
}
vrrp_script Chk_redis 
{ 
     script "/etc/keepalived/scripts/redis_check.sh 127.0.0.1 6379" 
     interval 2
     timeout 2
     fall 3
}

vrrp_instance Redis {State
    BACKUP   
    interface eth0   
    virtual_router_id  100       
    Advert_int 1
    authentication {   #all node must same
        auth_type PASS
        auth_pass 1111
    }   
    virtual_ipaddress { 
    192.168.1.200/24
    }
    track_script { 
         Chk_redis 
    } 
    notify_ Master "/etc/keepalived/scripts/redis_master.sh 127.0.0.1 192.168.1.148 6379"
    notify_backup "/etc/keepalived/ scripts/redis_backup.sh 127.0.0.1 192.168.1.148 6379 "
    notify_fault/etc/keepalived/scripts/redis_fault.sh 
    notify_stop/etc/keepalived/scripts/redis_stop.sh 
}
Create a separate monitoring Redis script on the primary and slave servers
root@xiaoyao-virtual-machine:/home/xiaoyao# mkdir/etc/keepalived/scripts
Root@xiaoyao-virtual-machine:/home /xiaoyao# vim/etc/keepalived/scripts/redis_check.sh

Script content:

#!/bin/bash 
alive= '/usr/local/bin/redis-cli-h $1-p $ PING ' 
logfile= "/var/log/keepalived-redis-check.log" 
echo "[CHECK]" >> $LOGFILE
date >> $LOGFILE
if [$ALIVE = = "PONG"]; then: 
   echo "Success:re  Dis-cli-h $1-p $ PING $ALIVE ">> $LOGFILE 2>&1
   exit 0 
Else 
   echo" Failed:redis-cli-h $1-p $ PING $ALIVE ">> $LOGFILE 2>&1
   exit 1 
fi 
Keepalived is called according to the state when the state is converted:When the master state is entered, the Redis_master is called when the backup status is entered redis_backup when an abnormal condition is found, enter the fault status call Redis_fault when the keepalived program terminates the call Redis_ Stop

The following scripts are created in the master server to create Redis_master scripts on the primary server, respectively

root@xiaoyao-virtual-machine:/home/xiaoyao# vim/etc/keepalived/scripts/redis_master.sh

Script Content

#!/bin/bash 
rediscli= "/usr/local/bin/redis-cli-h $1-p $" 
logfile= "/var/log/keepalived-redis-state.log" 
echo "[Master]" >> $LOGFILE 
date >> $LOGFILE 
echo "Being master ..." >> $LOGFILE 2>& Amp;1  
echo "Run MASTER cmd ..." >> $LOGFILE 2>&1
$REDISCLI slaveof $ >> $LOGFILE  
Sleep Ten #delay s wait data async cancel Sync
echo "Run slaveof NO one cmd ..." >> $LOGFILE
$REDISCLI slaveof N O One >> $LOGFILE 2>&1
Create a Redis_backup script on the primary server
root@xiaoyao-virtual-machine:/home/xiaoyao# vim/etc/keepalived/scripts/redis_backup.sh

Script Content

#!/bin/bash 
rediscli= "/usr/local/bin/redis-cli" 
logfile= "/var/log/keepalived-redis-state.log" 
Echo [Backup] >> $LOGFILE 
date >> $LOGFILE 
echo "Run slaveof cmd ..." >> $LOGFILE 
$REDISCLI SL Aveof $ >> $LOGFILE 2>&1 
# echo "Being slave ..." >> $LOGFILE 2>&1 
sleep #delay 1 5 s Wait Data sync Exchange role 
Create a Redis_fault script on the primary server
root@xiaoyao-virtual-machine:/home/xiaoyao# vim/etc/keepalived/scripts/redis_fault.sh

Script Content

#!/bin/bash 
Logfile=/var/log/keepalived-redis-state.log 
echo "[Fault]" >> $LOGFILE
date > > $LOGFILE 
Create a Redis_stop script on the primary server
root@xiaoyao-virtual-machine:/home/xiaoyao# vim/etc/keepalived/scripts/redis_stop.sh

Script Content

#!/bin/bash 
Logfile=/var/log/keepalived-redis-state.log 
echo "[Stop]" >> $LOGFILE 
date >> $ LOGFILE 
to create a redis_master script from the server
root@xiaoyao-virtual-machine:/home/xiaoyao# vim/etc/keepalived/scripts/redis_master.sh

Script Content

#!/bin/bash 
rediscli= "/usr/local/bin/redis-cli-h $1-p $" 
logfile= "/var/log/keepalived-redis-state.log" 
echo "[Master]" >> $LOGFILE 
date >> $LOGFILE 
echo "Being master ..." >> $LOGFILE 2>& Amp;1 
echo "Run slaveof cmd ... ">> $LOGFILE 
$REDISCLI slaveof $ >> $LOGFILE  2>&1
#echo" slaveof $ cmd can ' t excut E... >> $LOGFILE 
Sleep # #delay s wait Data sync Exchange role
echo "Run slaveof NO one cmd ..." >> ; $LOGFILE
$REDISCLI slaveof NO one >> $LOGFILE 2>&1 
to create a redis_backup script from the server
root@xiaoyao-virtual-machine:/home/xiaoyao# vim/etc/keepalived/scripts/redis_backup.sh

Script Content

#!/bin/bash 
rediscli= "/usr/local/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 $ >> $LOGFILE  
sleep #dela Y s wait data async cancel Sync 
exit (0)
to create a Redis_fault script from the server
root@xiaoyao-virtual-machine:/home/xiaoyao# vim/etc/keepalived/scripts/redis_fault.sh

Script Content

#!/bin/bash 
Logfile=/var/log/keepalived-redis-state.log 
echo "[Fault]" >> $LOGFILE
date > > $LOGFILE 
to create a redis_stop script from the server
root@xiaoyao-virtual-machine:/home/xiaoyao# vim/etc/keepalived/scripts/redis_stop.sh

Script Content

#!/bin/bash 
Logfile=/var/log/keepalived-redis-state.log 
echo "[Stop]" >> $LOGFILE 
date >> $ LOGFILE 
the primary server and the slave server add executable permissions to the script separately
root@xiaoyao-virtual-machine:/home/xiaoyao# chmod +x/etc/keepalived/scripts/*.sh
Start the service test

Start Redis on the primary server

root@xiaoyao-virtual-machine:/home/xiaoyao# Service Redis Start

To start Redis from the server

root@xiaoyao-virtual-machine:/home/xiaoyao# Service Redis Start

Start keepalived on the primary server

root@xiaoyao-virtual-machine:/home/xiaoyao#/etc/init.d/keepalived Start

Start the keepalived from the server

root@xiaoyao-virtual-machine:/home/xiaoyao#/etc/init.d/keepalived Start

Connect to Redis via virtual IP

root@xiaoyao-virtual-machine:/home/xiaoyao# redis-cli-h 192.168.1.200 INFO 
# Replication
role:master
Connected_slaves:1
slave0:ip=192.168.1.158,port=6379,state=online,offset=15,lag=1
master_repl_offset :
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_ Backlog_histlen:14

Inserting a piece of data through a virtual IP

root@xiaoyao-virtual-machine:/home/xiaoyao# redis-cli-h 192.168.1.200 SET Hello Redis

This data can be read by both master and slave servers

root@xiaoyao-virtual-machine:/home/xiaoyao# redis-cli-h 192.168.1.200 GET Hello
root@xiaoyao-virtual-machine :/home/xiaoyao# redis-cli-h 192.168.1.148 GET Hello
root@xiaoyao-virtual-machine:/home/xiaoyao# redis-cli-h 192.168.1.158 GET Hello

Kill the Redis process on the primary server

root@xiaoyao-virtual-machine:/home/xiaoyao# killall-9 Redis-server

To view the keepalived log on the primary server

root@xiaoyao-virtual-machine:/home/xiaoyao# Tailf/var/log/keepalived-redis-state.log

View the keepalived log from the server

root@xiaoyao-virtual-machine:/home/xiaoyao# Tailf/var/log/keepalived-redis-state.log

View information from a server

root@xiaoyao-virtual-machine:/home/xiaoyao# redis-cli-h 192.168.1.158 INFO
# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_ Backlog_first_byte_offset:2
repl_backlog_histlen:0
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.