Keepalived enables Redis dual-machine high availability

Source: Internet
Author: User
Tags redis cluster

Now the production environment basically uses the main preparation method, and how to realize the master of Redis? In general, the Redis implementation of master-slave replication is relatively simple, only need to configure the configuration file from the server "Slaveof" can be. However, there is a problem: when the primary server is hung up, the business address can be temporarily transferred to the slave server, but cannot be written from the server.


To solve this problem, we found some information on the Internet. There are several main scenarios

1, using keepalived+ monitoring script to achieve primary and standby switching

2, the use of Redis cluster to achieve a dual-master switch, the principle is to imitate the bin-log mechanism of MySQL.


This article describes the 1th scenario


Design ideas:

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;

When Master returns to normal, the data is synchronized from slave, the master-slave replication function is turned off after the data is synchronized, and the master identity is restored, while slave waits for the master synchronization data to complete before resuming slave identity.

Then cycle through it.


Environment Introduction:

master:192.168.2.23

slave:192.168.2.24

vip:192.168.2.40


Operating system:

CentOS 6.5 64bit


Software version:

Redis 3.2.0

Keepalived 1.2.13


Implementation steps:


First, to build Redis master-Slave

1. Deploying Redis to Master/slave nodes with saltstack (slightly)

2. Modify the Redis slave node configuration file:

Add the following line:

Slaveof 192.168.2.40 6379

Second, the configuration keepalived

1, master and slave two host respectively installed keepalived service

Yum-y Install keepalived

2. Modify the keepalived configuration file


Master:

Cat/etc/keepalived/keepalived.conf

! configuration file for keepalivedvrrp_script chk_redis {      script  "/etc/keepalived/scripts/check_redis.sh"      interval 5      weight 20     }vrrp_instance VI_REDIS_1  {     #state  BACKUP    state MASTER     Interface eno16780032    virtual_router_id 30    mcast_src_ ip 192.168.2.23    priority 100    advert_int 1     authentication {       auth_type PASS        auth_pass 11111      } track_ script {        chk_redis     }     notify_master /etc/keepalived/scripts/master.sh    notify_backup /etc/keepalived/scripts/ backup.sh    notify_stop /etc/keepalived/scripts/stop.sh     notify_fault /etc/keepalived/scripts/fault.sh    virtual_ipaddress {        192.168.2.40    }}

Slave:

Cat/etc/keepalived/keepalived.conf

! configuration file for keepalived vrrp_script chk_redis {      script  "/etc/keepalived/scripts/check_redis.sh"      interval  5      weight 20      }  vrrp _instance vi_redis_1 {     #state  backup     state  MASTER    interface eno16780032    virtual_router_id  30     mcast_src_ip 192.168.2.24    priority 100      advert_int 1     authentication {        auth_type pass       auth_pass 11111       }   track_script {          chk_reDis      }      notify_master /etc/keepalived /scripts/master.sh    notify_backup /etc/keepalived/scripts/backup.sh     notify_stop /etc/keepalived/scripts/stop.sh    notify_fault /etc/ keepalived/scripts/fault.sh     virtual_ipaddress {        192.168.2.40    }}


3. Writing scripts

First create a Redis-monitoring script on both servers

cat/etc/keepalived/scripts/check_redis.sh

#!/bin/bashcount=1logfile= "/var/log/keepalived-redis-state.log" while truedo/usr/local/redis/bin/redis-cli  -p 6379 -a abcd*123456 ping >/dev/null 2>&1i=$?netstat - ntlup | grep 6379 >/dev/null 2>&1j=$?if [  $i  = 0 - a  $j  = 0 ];then        exit 0else         if [  $count  -gt 10 ]; then                 echo  "' hostname '  redis  check is failed,exit check script  >> $LOGFILE                  break         fi        sleep 1         let count++        continuefidone/etc/init.d/keepalived  Stop


And then the purpose of the following scripts

notify_master/etc/keepalived/scripts/master.sh

notify_backup/etc/keepalived/scripts/backup.sh

notify_stop/etc/keepalived/scripts/stop.sh

notify_fault/etc/keepalived/scripts/fault.sh

When the keepalived enters the master state, the notify_master is executed;

When the keepalived enters the backup state, the notify_backup is executed;

When the keepalived enters the fault state, the notify_fault is executed;

When the keepalived enters the stop state, the notify_stop is executed;


Write the following 4 scripts on the master, slave server


cat/etc/keepalived/scripts/master.sh

#!/bin/bashrediscli= "/usr/local/redis/bin/redis-cli" logfile= "/var/log/keepalived-redis-state.log" echo  "[ Master] " >>  $LOGFILEecho   ' date +%y-%m-%d '   '%h:%m:%s '"  >> $ logfileecho  " change to master role&nbsp"  >>  $LOGFILEnohup  / bin/bash /usr/local/shell/change_master.sh  &while truedo         /bin/bash /usr/local/shell/role_check.sh |grep slave    >>  $LOGFILE         if [ $? -eq 0 ] ; then                nohup  /bin/bash /usr/local/shell/change_master.sh  &         else                 exit 0        fi        sleep 5                                                            Done


cat/etc/keepalived/scripts/backup.sh

#!/bin/bashrediscli= "/usr/local/redis/bin/redis-cli" logfile= "/var/log/keepalived-redis-state.log" echo  "[ Backup] " >>  $LOGFILEecho   ' date +%y-%m-%d '   '%h:%m:%s '"  >> $ logfileecho  " change to slave role&nbsp ..."  >>  $LOGFILEsleep   20nohup /bin/bash /usr/local/shell/change_slave.sh  &while truedo/bin/bash / usr/local/shell/role_check.sh |grep master   >>  $LOGFILEif  [ $?  -eq 0 ];thennohup /bin/bash /usr/local/shell/change_slave.sh  & elseexit 0fisleep 5                                                         &nBsp; done 


cat/etc/keepalived/scripts/stop.sh

#!/bin/bashlog= '/var/log/keepalived-redis-state.log ' host= ' hostname ' echo ' ' Date +%y-%m-%d ' '%h:%m:%s ': ' hostname ' Keepalived Service Stop ... ">> $log


cat/etc/keepalived/scripts/fault.sh

#!/bin/bashlog= '/var/log/keepalived-redis-state.log ' echo ' ' Date +%y-%m-%d '%h:%m:%s ': Fault ... ' >> $log


Write the following 4 failover scripts on the master, slave server


cat/usr/local/shell/change_master.sh

#!/bin/bashpwd=abcd*123456master_ip=192.168.2.40port= (6379) for PORT in ${PORT[*]}DO/USR/LOCAL/REDIS/BIN/REDIS-CLI -P $PORT-a $PWD slaveof NO onedone

cat/usr/local/shell/change_slave.sh

#!/bin/bashpwd=abcd*123456master_ip=192.168.2.40port= (6379) for PORT in ${PORT[*]}DO/USR/LOCAL/REDIS/BIN/REDIS-CLI -P $PORT-a $PWD slaveof $MASTER _ip $PORTdone


cat/usr/local/shell/startredis.sh

#!/bin/bashport= (6379) for PORT in ${port[*]}do/etc/init.d/redis_$port Startdone

cat/usr/local/shell/stopredis.sh

#!/bin/bashport= (6379) for PORT in ${port[*]}do/etc/init.d/redis_$port Stopdone


cat/usr/local/shell/restartredis.sh

#!/bin/bashport= (6379) for PORT in ${port[*]}do/etc/init.d/redis_$port Restartdone


cat/usr/local/shell/role_check.sh

#!/bin/bashpwd=abcd*123456master_ip=192.168.2.40port= (6379) for PORT in ${PORT[*]}DO/USR/LOCAL/REDIS/BIN/REDIS-CLI -P $PORT-a $PWD info|egrep "Role|tcp_port" done


This article from "Let Everything with the Wind" blog, declined reprint!

Keepalived enables Redis dual-machine high availability

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.