Test redis+keepalived for simple primary and standby switching

Source: Internet
Author: User
Tags install redis

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
  1. ! Configuration File for Keepalived
  2. Global_defs {
  3. router_id lvs_redis_test_21
  4. }
  5. Vrrp_script Chk_redis_1 {
  6. Script "/home/cachedb/redis/scripts/redis_check.sh 10.15.34.21 38001"
  7. Interval 10
  8. Timeout 2
  9. Fall 3
  10. }
  11. Vrrp_script chk_redis_2 {
  12. Script "/home/cachedb/redis/scripts/redis_check.sh 10.15.34.21 38002"
  13. Interval 10
  14. Timeout 2
  15. Fall 3
  16. }
  17. Vrrp_instance Vi_1 {
  18. State BACKUP
  19. Interface eth0
  20. VIRTUAL_ROUTER_ID 40
  21. Priority 150
  22. Nopreempt
  23. Advert_int 1
  24. Authentication {
  25. Auth_type PASS
  26. Auth_pass 1111
  27. }
  28. Track_script {
  29. Chk_redis_1
  30. }
  31. virtual_ipaddress {
  32. 10.15.34.23
  33. }
  34. Notify_master "/home/cachedb/redis/scripts/redis_master.sh 10.15.34.21"
  35. Notify_backup "/home/cachedb/redis/scripts/redis_backup.sh 10.15.34.21 38001 10.15.34.22 38001"
  36. Notify_fault "/home/cachedb/redis/scripts/redis_fault.sh"
  37. Notify_stop "/home/cachedb/redis/scripts/redis_stop.sh"
  38. }
  39. Vrrp_instance Vi_2 {
  40. State BACKUP
  41. Interface eth0
  42. VIRTUAL_ROUTER_ID 41
  43. Priority 150
  44. Nopreempt
  45. Advert_int 1
  46. Authentication {
  47. Auth_type PASS
  48. Auth_pass 1111
  49. }
  50. Track_script {
  51. Chk_redis_2
  52. }
  53. virtual_ipaddress {
  54. 10.15.34.54
  55. }
  56. Notify_master "/home/cachedb/redis/scripts/redis_master.sh 10.15.34.21 38002"
  57. Notify_backup "/home/cachedb/redis/scripts/redis_backup.sh 10.15.34.21 38002 10.15.34.22 38002"
  58. Notify_fault "/home/cachedb/redis/scripts/redis_fault.sh"
  59. Notify_stop "/home/cachedb/redis/scripts/redis_stop.sh"
  60. }

2) keepalived configuration of the standby machine:

XML code
  1. ! Configuration File for Keepalived
  2. Global_defs {
  3. router_id Lvs_redis_test
  4. }
  5. Vrrp_script Chk_redis_1 {
  6. Script "/home/cachedb/redis/scripts/redis_check.sh 10.15.34.22 38001"
  7. Interval 10
  8. Timeout 2
  9. Fall 3
  10. }
  11. Vrrp_script chk_redis_2 {
  12. 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>
  13. Interval 10
  14. Timeout 2
  15. Fall 3
  16. }
  17. Vrrp_instance Vi_1 {
  18. State BACKUP
  19. Interface eth0
  20. VIRTUAL_ROUTER_ID 40
  21. Priority 100
  22. Advert_int 1
  23. Authentication {
  24. Auth_type PASS
  25. Auth_pass 1111
  26. }
  27. Track_script {
  28. Chk_redis_1
  29. }
  30. virtual_ipaddress {
  31. 10.15.34.33
  32. }
  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>
  34. 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>
  35. 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>
  36. 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>
  37. }
  38. Vrrp_instance Vi_2 {
  39. State BACKUP
  40. Interface eth0
  41. VIRTUAL_ROUTER_ID 41
  42. Priority 100
  43. Advert_int 1
  44. Authentication {
  45. Auth_type PASS
  46. Auth_pass 1111
  47. }
  48. Track_script {
  49. Chk_redis_2
  50. }
  51. virtual_ipaddress {
  52. 10.15.34.34
  53. }
  54. 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>
  55. 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>
  56. 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>
  57. 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>
  58. }

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

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.