keepalived enable Redis Master and standby switching

Source: Internet
Author: User


Test target:

1 : Master Redis When a failure occurs, the Redis Auto Switch main Redis .

2 : Master Redis when it returns to normal, it is automatically downgraded to Redis .

Test environment:

Master redis:192.168.189.138

from redis:192.168.189.131

VIP : 192.168.189.123

Master Redis are installed with keepalived with the Redis , Redis The master/slave is configured. Redis Port 44444.

1 : Production Redis Monitoring and primary standby switch scripts:

Redis Monitoring scripts: (Monitoring Redis is normal)

# Cat/usr/local/scripts/redis_monitor.sh#!/bin/bash alive=$ (/home/usr/local/redis/bin/redis-cli-h 192.168.189.138 -P 44444 PING) if ["$ALIVE" = = "PONG"]; Thenecho $ALIVEexit 0elseecho $ALIVEkillall-9 keepalivedservice network Restartexit 1FI


redis_master script :(for switching from Redis to master redis)

# cat /usr/local/scripts/redis_master.sh#!/bin/bashrediscli= "/home/usr/local/redis/bin/redis-cli-h  192.168.189.138 -p 44444  "logfile="/home/usr/local/redis/var/keepalived-redis-state.log " echo  "[Master]"  >> $LOGFILEdate  >>  $LOGFILEecho   "Being master ..." >>  $LOGFILE  2>&1echo  "run slaveof cmd&nbsp ..." >>  $LOGFILE # Specify yourself as a 131 slave$rediscli slaveof 192.168.189.13144444 >>  $LOGFILE   2> &1# delay of 10 seconds for data synchronization sleep 10                                        echo  "Run slaveof no one cmd ..."  >>  $LOGFILE # Lift Your slave state and become master$rediscli slaveof no one >>$ Logfile 2>&1


Redis_backup script: (For the main Redis switch from Redis )

# cat/usr/local/scripts/redis_backup.sh#!/bin/bashrediscli= "/home/usr/local/redis/bin/redis-cli-h 192.168.189.138-p 44444 "logfile="/home/usr/local/redis/var/keepalived-redis-state.log "echo" [Backup] ">>$ Logfiledate >> $LOGFILEecho "Being slave ..." >> $LOGFILE 2>&1sleep 15echo "Run slaveof cmd ..." >> ; $LOGFILE $rediscli slaveof 192.168.189.13144444 >> $LOGFILE 2>&1


The above is the main Redis on the script, from Redis as soon as you change the script in the corresponding IP .


2 : Configure keepalived

Master node:

# cat /usr/local/keepalived/etc/keepalived/keepalived.confglobal_defs { router_id lvs _devel}vrrp_script redis_monitor {                 ------Monitoring Script  script "/usr/local/scripts/redis_monitor.sh"  interval 2   weight 2 }vrrp_instance VI_1{  state BACKUP                      -------- The state is consistent with the standby node and is backup interface eth0 virtual_router_id 51  mcast_src_ip  192.168.189.138 nopreempt                       ----------settings do not preempt ip priority 100                    -------- --Priority  ADVERT_INT&NBSp;1 authentication { auth_type pass auth_pass password_123} track_script  { redis_monitor                    -------Consistent} virtual_ipaddress { 192.168.189.123&nbsp with the above monitoring script;}  notify_master/usr/local/scripts/redis_master.sh   -------keepalived run when switching to master   notify_backup/usr/local/scripts/redis_backup.sh   -------keepalived run when switching to slave}


from node:

# cat /usr/local/keepalived/etc/keepalived/keepalived.conf! configuration file for  keepalived global_defs { router_id LVS_DEVEL}vrrp_script redis_monitor{  Script "/usr/local/scripts/redis_monitor.sh"  interval 2  weight 2 } vrrp_ instance vi_1{  state backup                      ----------consistent with the primary node,bakcup interface  eth0 virtual_router_id 51  mcast_src_ip 192.168.189.131 priority 99                       -----Priority is less than the primary node  advert_int 1  authentication { auth_type pass  auth_pass password_123} track_script { redis_monitor } virtual_ipaddress  { 192.168.189.123&nBSP;} NOTIFY_MASTER/USR/LOCAL/SCRIPTS/REDIS_MASTER.SHNOTIFY_BACKUP/USR/LOCAL/SCRIPTS/REDIS_BACKUP.SH}


3 : Open Redis with the keepalived

#/usr/local/redis/bin/redis-server/home/usr/local/redis/etc/redis.conf#/usr/local/keepalived/sbin/keepalived-d -f/usr/local/keepalived/etc/keepalived/keepalived.conf


4 : Observation Test

After you start redis-server , you can see that The 138 host preempted the VIP123 and Redis role is Master

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/83/2C/wKioL1dsnzeySe3YAAAYhenpztE563.png "style=" float: none; "title=" 1.png "alt=" Wkiol1dsnzeyse3yaaayhenpzte563.png "/>

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/83/2D/wKioL1dsnzfQB7bfAAAbr--Osl4448.png "style=" float: none; "title=" 2.png "alt=" Wkiol1dsnzfqb7bfaaabr--osl4448.png "/>

131 the mainframe didn't preempt it. 123IP , Redis role is Slave

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/83/2D/wKioL1dsn3_B1rh4AAAZuHBzfG0297.png "title=" 3.png " alt= "Wkiol1dsn3_b1rh4aaazuhbzfg0297.png"/>

Configure data on the master node to see if the slave node is synchronized.

Main:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/83/2D/wKiom1dsn5uiN4yfAAAlvnCxEsw296.png "title=" 4.png " alt= "Wkiom1dsn5uin4yfaaalvncxesw296.png"/>

From:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/83/2D/wKioL1dsn6OzTj6xAAATAlIVV-w690.png "title=" 5.png " alt= "Wkiol1dsn6oztj6xaaatalivv-w690.png"/>

The above can be seen that the primary and standby data synchronization is normal.

1, Close the main Redis to see if the slave node is preempted IP , from Redis whether it becomes the primary Redis

#/home/usr/local/redis/bin/redis-cli-h 192.168.189.138-p 44444 shutdown

View Log from node:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/83/2D/wKioL1dsn8bgu26eAACsz-sQkXA663.png "title=" 6.png " alt= "Wkiol1dsn8bgu26eaacsz-sqkxa663.png"/>

The above log shows that the 131 node is preempted to IP123and becomes the keepalived master node.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/83/2D/wKioL1dsn-zjQmeeAAAgg_V8PuU595.png "title=" 7.png " alt= "Wkiol1dsn-zjqmeeaaagg_v8puu595.png"/>

The above log shows that the redis role of the 131 node becomes Master.

to view the status of 131redis characters:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/83/2D/wKiom1dsoB_jU556AAAY_kLHMrw901.png "title=" 8.png " alt= "Wkiom1dsob_ju556aaay_klhmrw901.png"/>

131 of the Redis has succeeded from slave into Master .

 2, restore 138 status , redis is automatically demoted to slave

re-open keepalived, and Redison the master node (note to turn on redisfirst, then turn on keepalived).

#/home/usr/local/redis/bin/redis-server/home/usr/local/redis/etc/redis.conf#/usr/local/keepalived/sbin/ Keepalived-d-f/usr/local/keepalived/etc/keepalived/keepalived.conf

to view the 138 node log:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/83/2D/wKioL1dsoTOAUvHAAAC9Odo-qh4584.png "title=" 9.png " alt= "Wkiol1dsotoauvhaaac9odo-qh4584.png"/> can see keepalived Primary node automatically enters BACKUP state, not preempted 123VIP .

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/83/2D/wKioL1dsoVDgYHLnAAARQOP9mYY680.png "title=" 10.png "alt=" Wkiol1dsovdgyhlnaaarqop9myy680.png "/>

The Redis role is also automatically changed to Slave.

To view the primary and standby Redis role:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/83/2E/wKiom1dsoXPhLBB7AAAaJqjsXaU677.png "style=" float: none; "title=" 11.png "alt=" Wkiom1dsoxphlbb7aaaajqjsxau677.png "/>

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/83/2E/wKiom1dsoXTydtWKAAAZLcb7Kkk651.png "style=" float: none; "title=" 12.png "alt=" Wkiom1dsoxtydtwkaaazlcb7kkk651.png "/>

Test Target completed!


This article from the "Play God Clown" blog, reproduced please contact the author!

keepalived enable Redis Master and standby switching

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.