Keepalived + redis implement high-availability automatic failover and keepalivedfailover

Source: Internet
Author: User
Tags failover install redis

Keepalived + redis implement high-availability automatic failover and keepalivedfailover
Keepalived + redis for high-availability automatic failover
Install redis and keepalived on server A (10.0.11.2) and server B (10.0.12.2)
A is the default master and B is the slave (add SLAVEOF 10.0.11.2 6379 TO THE redis configuration file ).
Redis on A and B All enable localization policies. Appendonly yes

Contents of the keepalived configuration file for server A ------- begin ------
! Configuration File for keepalived


Global_defs {
Notification_email {
Hq@xxx.com
}
Notification_email_from hq@xxx.com
Smtp_server 127.0.0.1
Smtp_connect_timeout 30
Router_id redis1.xxx.com
}


Vrrp_script chk_redis {
Script "/opt/redis/sh/redis_check.sh"
Interval 2
}

Vrrp_instance VI_1 {
State MASTER
Interface bond0
Virtual_router_id 51
Priority101
Advert_int 2
Authentication {
Auth_type PASS
Auth_pass 1111
}
Track_script {
Chk_redis
}
Virtual_ipaddress {
10.0.11.0
}
Notify_master/opt/redis/sh/redis_master.sh
Notify_backup/opt/redis/sh/redis_backup.sh
Notify_fault/opt/redis/sh/redis_fault.sh
Notify_stop/opt/redis/sh/redis_stop.sh
}
----- End -----
Note: Some emails in global_defs can be written as needed. To implement email notification, you must enter the actual information.
Script "/opt/redis/sh/redis_check.sh" # monitor script path
Interval 2 # monitoring frequency
State MASTER # default status
Interface bond0 # Nic name
Virtual_router_id 51 # A, B server can be set the same
Priority 101 # set a higher priority than B
Advert_int 2 # It seems that the broadcast frequency is unknown.
Authentication {# A, B server can be set the same
Auth_type PASS
Auth_pass 1111
}
Track_script {# monitoring name, as set above
Chk_redis
}
Virtual_ipaddress {# virtual IP address, which is used by the client to access redis
10.0.11.0
}
# The following is the script path executed in each status:
Notify_master/opt/redis/sh/redis_master.sh # become the master
Notify_backup/opt/redis/sh/redis_backup.sh # become backup
Notify_fault/opt/redis/sh/redis_fault.sh # When the monitoring script exit 1
Notify_stop/opt/redis/sh/redis_stop.sh # When the keepalived service is stopped
/Opt/redis/sh/redis_check.sh -- begin --


#! /Bin/bash
ALIVE = '/usr/local/bin/redis-cli ping'
LOGFILE = "/opt/redis/logs/keepalived-redis-state.log"
If ["$ ALIVE" = "PONG"]; then
Echo $ ALIVE
# Echo "check master pong"> $ LOGFILE
Exit 0
Else
Echo $ ALIVE
Exit 1
Fi
-- End --
/Opt/redis/sh/redis_master.sh -- begin --


#! /Bin/bash
REDISCLI = "/usr/local/bin/redis-cli"
LOGFILE = "/opt/redis/logs/keepalived-redis-state.log"
Echo "[master]" >>$ LOGFILE
Date> $ LOGFILE
Echo "Being master..." >>$ LOGFILE 2> & 1
Echo "Run SLAVEOF cmd..."> $ LOGFILE
$ Rediscli slaveof 10.0.12.2 6379 >>$ LOGFILE 2> & 1
Sleep 15
Echo "Run slaveof no one cmd..."> $ LOGFILE
$ REDISCLI SLAVEOF NO ONE
$ Rediscli slaveof no one >>$ LOGFILE 2> & 1
-- End --
/Opt/redis/sh/redis_backup.sh -- begin --
REDISCLI = "/usr/local/bin/redis-cli"
LOGFILE = "/opt/redis/logs/keepalived-redis-state.log"
Echo "[backup]" >>$ LOGFILE
Date> $ LOGFILE
Echo "Being slave..."> $ LOGFILE 2> & 1
Sleep 15
Echo "Run SLAVEOF cmd..."> $ LOGFILE
$ Rediscli slaveof 10.0.12.2 6379 >>$ LOGFILE 2> & 1
-- End --
/Opt/redis/sh/redis_fault.sh -- begin --
#! /Bin/bash


LOGFILE = "/opt/redis/logs/keepalived-redis-state.log"
Echo "[fault]" >>$ LOGFILE
Date> $ LOGFILE
Sh/opt/redis/sh/redis_backup.sh
-- End --
/Opt/redis/sh/redis_stop.sh -- begin --
#! /Bin/bash
LOGFILE = "/opt/redis/logs/keepalived-redis-state.log"
Echo "[stop]" >>$ LOGFILE
Date> $ LOGFILE
-- End --

Server B Configuration
Content of the keepalived configuration file ------- begin ------
! Configuration File for keepalived


Global_defs {
Notification_email {
Hq@xxx.com
}
Notification_email_from hq@xxx.com
Smtp_server 127.0.0.1
Smtp_connect_timeout 30
Router_id redis2.xxx.com
}


Vrrp_script chk_redis {
Script "/opt/redis/sh/redis_check.sh"
Interval 2
}




Vrrp_instance VI_1 {
State BACKUP
Interface bond0
Virtual_router_id 51
Priority 99
Authentication {
Auth_type PASS
Auth_pass 1111
}
Track_script {
Chk_redis
}
Virtual_ipaddress {
10.0.11.0
}
Notify_master/opt/redis/sh/redis_master.sh
Notify_backup/opt/redis/sh/redis_backup.sh
Notify_fault/opt/redis/sh/redis_fault.sh
Notify_stop/opt/redis/sh/redis_stop.sh
}
----- End -----

/Opt/redis/sh/redis_check.sh -- begin --
#! /Bin/bash
ALIVE = '/usr/local/bin/redis-cli-h 10.0.11.2-p 6379 PING'
LOGFILE = "/opt/redis/logs/keepalived-redis-state.log"
If ["$ ALIVE "! = "PONG"]; then
Echo $ ALIVE
Exit 0
Else
Echo $ ALIVE
Exit 1
Fi
-- End --
/Opt/redis/sh/redis_master.sh -- begin --
#! /Bin/bash
REDISCLI = "/usr/local/bin/redis-cli"
LOGFILE = "/opt/redis/logs/keepalived-redis-state.log"
Echo "[master]" >>$ LOGFILE
Date> $ LOGFILE
Echo "Being master..." >>$ LOGFILE 2> & 1
# Echo "master Run SLAVEOF 10.0.11.2 cmd..."> $ LOGFILE
# Rediscli slaveof 10.0.11.2 6379 >>$ LOGFILE 2> & 1
# Sleep 10
Echo "Run slaveof no one cmd..."> $ LOGFILE
$ Rediscli slaveof no one >>$ LOGFILE 2> & 1


-- End --
/Opt/redis/sh/redis_backup.sh -- begin --
#! /Bin/bash
REDISCLI = "/usr/local/bin/redis-cli"
LOGFILE = "/opt/redis/logs/keepalived-redis-state.log"
Echo "[backup]" >>$ LOGFILE
Date> $ LOGFILE
Echo "Being slave..."> $ LOGFILE 2> & 1
# Sleep 10
Echo "backup Run SLAVEOF 10.0.11.2 cmd..."> $ LOGFILE
$ Rediscli slaveof 10.0.11.2 6379 >>$ LOGFILE 2> & 1
-- End --
/Opt/redis/sh/redis_fault.sh -- begin --
#! /Bin/bash


LOGFILE = "/opt/redis/logs/keepalived-redis-state.log"
Echo "[fault]" >>$ LOGFILE
Date> $ LOGFILE
Sh/opt/redis/sh/redis_slave.sh
-- End --


/Opt/redis/sh/redis_stop.sh
-- Begin --
#! /Bin/bash
LOGFILE = "/opt/redis/logs/keepalived-redis-state.log"
Echo "[stop]" >>$ LOGFILE
Date> $ LOGFILE
-- End --
/Opt/redis/sh/redis_slave.sh -- begin --
#! /Bin/bash
REDISCLI = "/usr/local/bin/redis-cli"
LOGFILE = "/opt/redis/logs/keepalived-redis-state.log"
Echo "[slave]" >>$ LOGFILE
Echo "Being slave..."> $ LOGFILE 2> & 1
Sleep 35
Date> $ LOGFILE
Echo "slave Run SLAVEOF 10.0.11.2 cmd..."> $ LOGFILE
$ Rediscli slaveof 10.0.11.2 6379 >>$ LOGFILE 2> & 1
-- End --
Sleep 35 in redis_slave.sh indicates that, because redis on server A changes to the fault state after the service is started, and server A changes to the master state, if B is too fast to synchronize A (earlier than A's data synchronization from B), data will be lost. The purpose is to synchronize data from B first after A is started, and B is in slaveof,

Script Description: the logic of the script is that when the redis service on A and B is normal, A is the master, B is the slave
If A service is abnormal, B becomes the master. The "/usr/local/bin/redis-cli SLAVEOF NO ONE" command is used to disable data synchronization and change it to the Redis master.
If A switches back to the master after the service is started, the latest data is synchronized from B before it becomes the master. Execute "/usr/local/bin/redis-cli SLAVEOF 10.0.11.2 6379" on B"
Let B be the slave of A again, otherwise B is still the master.
In the redis master-slave architecture, you can use "/usr/local/bin/redis-cli-h 10.0.11.2 INFO" to view the current status. Check whether the current server is master or slave;


Under the command line
"Tail-30/opt/redis/logs/keepalived-redis-state.log" view the status transition of keepalived


"Tail-30/var/log/messages" to view changes in the keepalived virtual IP address.


The above has been tested in the actual production environment. Although some areas may not be reasonable, failover can be implemented and data will not be lost. Previously, you can switch the vip address based on the online tutorial, but there is a problem with the data.
Please let us know if there are better methods. Thank you!





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.