Turn from:
Test redis+keepalived for simple primary and standby switching-Try My Best effort-Iteye technology website
http://raising.iteye.com/blog/2311757
Keepalived is a service software that ensures high availability of clusters in cluster management, and it has the function of heartbeat detection. On Redis, it is a control function with master and standby switching.
The main and standby switching scheme of keepalived is designed as follows:
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 loop in turn.
Here's a general overview of the implementation process:
Environment preparation
Master: 10.15.34.21
Preparation: 10.15.34.22
VIP (virtual IP): 10.15.34.23,10.15.34.24
0. Create a user-deployed Redis and keepalived, respectively, on the host and standby, such as the user path:/home/cachedb
1. Install Redis on the host and standby (process slightly);
2. Install the keepalived on the host and standby machine (process slightly);
3. Modify the configuration file of the keepalived on the host and the standby keepalived.conf (can back up the original configuration file to Keepalived.conf.bak)
1) keepalived Configuration of the host:
XML code
- ! Configuration File for Keepalived
- Global_defs {
- router_id lvs_redis_test_21
- }
- Vrrp_script Chk_redis_1 {
- Script "/home/cachedb/redis/scripts/redis_check.sh 10.15.34.21 38001"
- Interval 10
- Timeout 2
- Fall 3
- }
- Vrrp_script chk_redis_2 {
- Script "/home/cachedb/redis/scripts/redis_check.sh 10.15.34.21 38002"
- Interval 10
- Timeout 2
- Fall 3
- }
- Vrrp_instance Vi_1 {
- State BACKUP
- Interface eth0
- VIRTUAL_ROUTER_ID 40
- Priority 150
- Nopreempt
- Advert_int 1
- Authentication {
- Auth_type PASS
- Auth_pass 1111
- }
- Track_script {
- Chk_redis_1
- }
- virtual_ipaddress {
- 10.15.34.23
- }
- Notify_master "/home/cachedb/redis/scripts/redis_master.sh 10.15.34.21"
- Notify_backup "/home/cachedb/redis/scripts/redis_backup.sh 10.15.34.21 38001 10.15.34.22 38001"
- Notify_fault "/home/cachedb/redis/scripts/redis_fault.sh"
- Notify_stop "/home/cachedb/redis/scripts/redis_stop.sh"
- }
- Vrrp_instance Vi_2 {
- State BACKUP
- Interface eth0
- VIRTUAL_ROUTER_ID 41
- Priority 150
- Nopreempt
- Advert_int 1
- Authentication {
- Auth_type PASS
- Auth_pass 1111
- }
- Track_script {
- Chk_redis_2
- }
- virtual_ipaddress {
- 10.15.34.54
- }
- Notify_master "/home/cachedb/redis/scripts/redis_master.sh 10.15.34.21 38002"
- Notify_backup "/home/cachedb/redis/scripts/redis_backup.sh 10.15.34.21 38002 10.15.34.22 38002"
- Notify_fault "/home/cachedb/redis/scripts/redis_fault.sh"
- Notify_stop "/home/cachedb/redis/scripts/redis_stop.sh"
- }
2) keepalived configuration of the standby machine:
XML code
- ! Configuration File for Keepalived
- Global_defs {
- router_id Lvs_redis_test
- }
- Vrrp_script Chk_redis_1 {
- Script "/home/cachedb/redis/scripts/redis_check.sh 10.15.34.22 38001"
- Interval 10
- Timeout 2
- Fall 3
- }
- Vrrp_script chk_redis_2 {
- Script "/home/<span style=" line-height:1.5; " >cachedb</span><span style= "FONT-SIZE:1EM; line-height:1.5; " >/redis/scripts/redis_check.sh 10.15.34.22 38002 "</span>
- Interval 10
- Timeout 2
- Fall 3
- }
- Vrrp_instance Vi_1 {
- State BACKUP
- Interface eth0
- VIRTUAL_ROUTER_ID 40
- Priority 100
- Advert_int 1
- Authentication {
- Auth_type PASS
- Auth_pass 1111
- }
- Track_script {
- Chk_redis_1
- }
- virtual_ipaddress {
- 10.15.34.33
- }
- Notify_master "/home/<span style=" line-height:1.5; >cachedb</span><span style= "FONT-SIZE:1EM; line-height:1.5; " >/redis/scripts/redis_master.sh 10.15.34.22 38001 "</span>
- Notify_backup "/home/<span style=" line-height:1.5; >cachedb</span><span style= "FONT-SIZE:1EM; line-height:1.5; " >/redis/scripts/redis_backup.sh 10.15.34.22 38001 10.15.34.21 38001 "</span>
- Notify_fault "/home/<span style=" line-height:1.5; >cachedb</span><span style= "FONT-SIZE:1EM; line-height:1.5; " >/redis/scripts/redis_fault.sh "</span>
- Notify_stop "/home/<span style=" line-height:1.5; >cachedb</span><span style= "FONT-SIZE:1EM; line-height:1.5; " >/redis/scripts/redis_stop.sh "</span>
- }
- Vrrp_instance Vi_2 {
- State BACKUP
- Interface eth0
- VIRTUAL_ROUTER_ID 41
- Priority 100
- Advert_int 1
- Authentication {
- Auth_type PASS
- Auth_pass 1111
- }
- Track_script {
- Chk_redis_2
- }
- virtual_ipaddress {
- 10.15.34.34
- }
- Notify_master "/home/<span style=" line-height:1.5; >cachedb</span><span style= "FONT-SIZE:1EM; line-height:1.5; " >/redis/scripts/redis_master.sh 10.15.34.22 38002 "</span>
- Notify_backup "/home/<span style=" line-height:1.5; >cachedb</span><span style= "FONT-SIZE:1EM; line-height:1.5; " >/redis/scripts/redis_backup.sh 10.15.34.22 38002 10.15.34.21 38002 "</span>
- Notify_fault "/home/<span style=" line-height:1.5; >cachedb</span><span style= "FONT-SIZE:1EM; line-height:1.5; " >/redis/scripts/redis_fault.sh "</span>
- Notify_stop "/home/<span style=" line-height:1.5; >cachedb</span><span style= "FONT-SIZE:1EM; line-height:1.5; " >/redis/scripts/redis_stop.sh "</span>
- }
Note: A. The priority of the keepalived configuration of the standby machine is lower than that of the host; B. The standby machine cannot have the nopreempt attribute;
C. virtual_router_id this master configuration to correspond to the same, and in the local area network to be unique, pay attention to using the default ID, such as 51 o'clock to check the ID of the log is occupied, if there is a change.
After the configuration is complete:
<!--[if!supportlists]-->1) <!--[endif]--> Boot host Redis service, and then start the keepalived service under root;
<!--[if!SUPPORTLISTS]-->2] <!--Redis service to start the standby, and then start the keepalived service under root;
<!--[if!supportlists]-->3) <!--[endif]--> View the role of the host:
./redis-cli-h 10.15.34.21-p 38001
To view the role of the standby machine:
./redis-cli-h 10.15.34.22-p 38001
Check the log log of the host's redis-state: (The log log location is specified by the redis_*.sh script file)
Then check the log log of the redis-state of the standby machine:
4) Try to kill the host's Redis service and then view the role roles on the standby to get:
You can see that it becomes master, and at the same time, its redis-state.log log is as follows:
1) We may wish to use the root user to view the VIP drift situation on the standby machine: (Virtual IP drift takes a short period of time, not immediately ~)
IP A
The drift of virtual IP can be found on the standby machine.
<!--[if!supportlists]-->1) <!--[endif]--> and if you restart the Redis service on master, you will see that its role is slave, which means Master and Salve have undergone role swaps.
<!--[if!supportlists]-->2) <!--[endif]--> can try to kill the Redis service on the standby and find that the role on the host is restored to master , you can run the IP a command on the host using the root user, and you will find that the virtual IP is drifting to the host.
Test redis+keepalived for simple primary and standby switching