Redis + keepalived Master/Slave hot standby switch in seconds

Source: Internet
Author: User

1 Introduction

Install and use centos 5.10

Master 192.168.235.135

Slave 192.168.235.152

VIP 192.168.235.200

Compiling environment Yum-y install GCC + gcc-C ++ OpenSSL-devel PCRE-devel

 

When both the master and slave work normally, the master is responsible for the service, and the slave is responsible for standby;
When the master node fails and the slave is normal, the slave takes over the service and disables the Master/Slave replication function;
When the master node returns to normal, the Server Load balancer synchronizes data from the Server Load balancer instance. After the data is synchronized, the master node replication function is disabled to restore the master node identity. At the same time, the Server Load balancer instance restores the Server Load balancer instance identity after the master node.
And then cyclically.

Note that the localization policy must be enabled on both the master and slave. Otherwise, if the localization policy is not enabled, the other party will clear the data of the other party, completely lost data

Installation 2

Redis installation configuration->: http://liuyieyer.iteye.com/blog/2078093

Download and install wget http://www.keepalived.org/software/keepalived-1.2.12.tar.gz

Tar xf 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

 

Three configurations

First, create the following configuration on the master

Mkdir-P/etc/keepalived

Vim/etc/keepalived. conf

// Add the following script

Vrrp_script chk_redis {

Script "/etc/keepalived/scripts/redis_check.sh" ### monitoring script

Interval 2 ### monitoring time

}

Vrrp_instance vi_1 {

State master ### set to master

Interface eth0 ### monitor Nic

Virtual_router_id 51

Priority 101 ### weight

Authentication {

Auth_type pass ### Encryption

Auth_pass redis ### Password

}

Track_script {

Chk_redis ### execute the chk_redis defined above

}

Virtual_ipaddress {

192.168.235.200 ### VIP

}

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

}

 

Vim/etc/keepalived/scripts/redis_master.sh

 

#! /Bin/bash

Rediscli = "/opt/redis/bin/redis-CLI"

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 192.168.135.152 (slave) 6379 >>$ logfile 2> & 1

Sleep 10 # The synchronization will be canceled after a delay of 10 seconds.

Echo "Run slaveof no one cmd..."> $ logfile

$ Rediscli slaveof no one >>$ logfile 2> & 1

 

Vim/etc/keepalived/scripts/redis_backup.sh

#! /Bin/bash

Rediscli = "/opt/redis/bin/redis-CLI"

Logfile = "/var/log/keepalived-redis-state.log"

Echo "[Backup]" >>$ logfile

Date> $ logfile

Echo "Being slave..."> $ logfile 2> & 1

Sleep 15 # delay of 15 seconds before switching the Master/Slave role after the data is synchronized by the other party

Echo "Run slaveof cmd..."> $ logfile

$ Rediscli slaveof 192.168.135.152 (slave) 6379 >>$ logfile 2> & 1

 

Create the following file on slave:

Vim/etc/keepalived. conf

Vrrp_script chk_redis {

Script "/etc/keepalived/scripts/redis_check.sh" ### monitoring script

Interval 2 ### monitoring time

}

Vrrp_instance vi_1 {

State backup ### set to backup

Interface eth0 ### monitor Nic

Virtual_router_id 51

Priority 100 ### lower weight than mastre

Authentication {

Auth_type pass

Auth_pass redis ### same password as mastre

}

Track_script {

Chk_redis ### execute the chk_redis defined above

}

Virtual_ipaddress {

192.168.235.200 ### VIP

}

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

}

 

Vim/etc/keepalived/scripts/redis_master.sh

#! /Bin/bash

Rediscli = "/opt/redis/bin/redis-CLI"

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 192.168.235.135 (master) 6379 >>$ logfile 2> & 1

Sleep 10 # The synchronization will be canceled after a delay of 10 seconds.

Echo "Run slaveof no one cmd..."> $ logfile

$ Rediscli slaveof no one >>$ logfile 2> & 1

Vim/etc/keepalived/scripts/redis_backup.sh

#! /Bin/bash

Rediscli = "/opt/redis/bin/redis-CLI"

Logfile = "/var/log/keepalived-redis-state.log"

Echo "[Backup]" >>$ logfile

Date> $ logfile

Echo "Being slave..."> $ logfile 2> & 1

Sleep 15 # delay of 15 seconds before switching the Master/Slave role after the data is synchronized by the other party

Echo "Run slaveof cmd..."> $ logfile

$ Rediscli slaveof 10.6.1.143 6379 >>$ logfile 2> & 1

 

The following scripts are created on the master and slave respectively.

Mkdir/etc/keepalived/scripts

Vim/etc/keepalived/scripts/redis_check.sh

#! /Bin/bash

 

Alive = '/opt/redis/bin/redis-cli ping'

If ["$ alive" = "PONG"]; then

Echo $ alive

Exit 0

Else

Echo $ alive

Exit 1

Fi

Vim/etc/keepalived/scripts/redis_fault.sh

#! /Bin/bash

Logfile =/var/log/keepalived-redis-state.log

Echo "[fault]" >>$ logfile

Date> $ logfile

Vim/etc/keepalived/scripts/redis_stop.sh

#! /Bin/bash

Logfile =/var/log/keepalived-redis-state.log

Echo "[Stop]" >>$ logfile

Date> $ logfile

 

Grant execution permission chmod + x/etc/keepalived/scripts/*. Sh

 

Test

Start redis on msater and slave respectively;

Service redis start

Start keepalived

Service keepalived start

Execute ip A on the master

The virtual IP address has been bound.

Run service redis stop on the master to view the slave

We can see the second-level switch.

Next let's start redis on the master node.


Switch back.

Test redis Synchronization


 
 

The installation is complete.

Redis + keepalived Master/Slave hot standby switch in seconds

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.