[Switch] nginx dual-machine Hot Standby High Availability Solution

Source: Internet
Author: User

1. You can obtain the random VIP1 and VIP2 through DNS query.

2. VIP1 and VIP2 are hot standby, load balancing, and high availability.

When rip1 cannot be accessed, host 2 will detect that the eth0: ha2 added to VIP 1 is highly available at any time.

3. The shell script VIP and rip must correspond. nginx_loadbalance_vip1.sh runs on Host 1 and the command is

# Nohup./nginx_loadbalance_vip1.sh>/dev/null & background running

4. For the monitoring of two machines, you only need to monitor rip. You can also write a crontab for the monitoring script to prevent unexpected termination of the script.

Structure chart

650) This. width = 650; "src =" http://www.linuxidc.com/upload/2012_07/120715072781341.jpg "width =" 580 "vspace =" 5 "style =" border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; "alt =" 120715072781341.jpg"/>

--------------------------------------------------------------------

The script for HOST 1 is as follows:

Cat nginx_loadbalance_vip1.sh

  1. #! /Bin/bash

  2.  

  3. Lang = C

  4. Date = $ (date-d "today" + "% Y-% m-% d % H: % m: % s ")

  5. Bind_vip1 = 192.168.16.238

  6. Bind_vip2 = 192.168.16.239

  7. Web_ip_rip1 = 192.168.16.240

  8. Web_ip_rip2 = 192.168.16.241

  9. Netmask = 255.255.255.0

  10. Gateway = 192.168.16.1

  11. Broadcast = 219.232.254.255

  12. Function_bind_vip1 ()

  13. {

  14. /Sbin/ifconfig eth0: HA1 $ {bind_vip1} broadcast $ {broadcast} netmask $ {netmask} Up

  15. /Sbin/route add-host $ {gateway} Dev eth0: HA1

  16. }

  17.  

  18.  

  19. Function_bind_vip2 ()

  20. {

  21. /Sbin/ifconfig eth0: ha2 $ {bind_vip2} broadcast $ {broadcast} netmask $ {netmask} Up

  22. /Sbin/route add-host $ {gateway} Dev eth0: ha2

  23. }

  24.  

  25. Function_restart_nginx ()

  26. {

  27. Kill-usr1 'cat/var/log/nginx. Pi'

  28. }

  29.  

  30. Function_remove_vip1 ()

  31. {

  32. /Sbin/ifconfig eth0: HA1 $ {bind_vip1} broadcast $ {broadcast} netmask $ {netmask} Down

  33. }

  34.  

  35. Function_remove_vip2 ()

  36. {

  37. /Sbin/ifconfig eth0: ha2 $ {bind_vip2} broadcast $ {broadcast} netmask $ {netmask} Down

  38. }

  39.  

  40. Function_vip_arping1 ()

  41. {

  42. /Sbin/arping-I eth0-C 3-S ${bind_vip1 }1 {gateway}>/dev/null 2> & 1

  43. }

  44.  

  45. Function_vip_arping2 ()

  46. {

  47. /Sbin/arping-I eth0-C 3-S ${bind_vip2 }$ {gateway}>/dev/null 2> & 1

  48. }

  49.  

  50. Bind_time_vip1 = "N ";

  51. Bind_time_vip2 = "N ";

  52.  

  53.  

  54.  

  55.  

  56. While true

  57. Do

  58. Httpcode_rip1 = '/usr/bin/curl-O/dev/null-S-w % {http_code} http: // $ {web_ip_rip1}/status.htm'

  59. Httpcode_rip2 = '/usr/bin/curl-O/dev/null-S-w % {http_code} http: // $ {web_ip_rip2}/status.htm'

  60. # VIP1

  61. If [x $ httpcode_rip1 = "x200"];

  62. Then

  63. If [$ bind_time_vip1 = "N"];

  64. Then

  65. Function_bind_vip1

  66. Function_vip_arping1

  67. Function_restart_nginx

  68. Bind_time_vip1 = "Y"

  69. Fi

  70. Function_vip_arping1

  71. Else

  72. If [$ bind_time_vip1 = "Y"];

  73. Then

  74.  

  75. Function_remove_vip1

  76. Bind_time_vip1 = "N"

  77. Fi

  78. Fi

  79.  

  80. # VIP2

  81. If [x $ httpcode_rip2 = "x200"];

  82. Then

  83. If [$ bind_time_vip2 = "Y"];

  84. Then

  85. Function_remove_vip2

  86. Bind_time_vip2 = "N"

  87. Fi

  88. Else

  89. If [$ bind_time_vip2 = "N"];

  90. Then

  91. Function_bind_vip2

  92. Function_vip_arping2

  93. Function_restart_nginx

  94. Bind_time_vip2 = "Y"

  95. Fi

  96. Function_vip_arping2

  97. Fi

  98. Sleep 5

  99. Done


[Switch] nginx dual-machine Hot Standby High Availability Solution

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.