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
#! /Bin/bash
Lang = C
Date = $ (date-d "today" + "% Y-% m-% d % H: % m: % s ")
Bind_vip1 = 192.168.16.238
Bind_vip2 = 192.168.16.239
Web_ip_rip1 = 192.168.16.240
Web_ip_rip2 = 192.168.16.241
Netmask = 255.255.255.0
Gateway = 192.168.16.1
Broadcast = 219.232.254.255
Function_bind_vip1 ()
{
/Sbin/ifconfig eth0: HA1 $ {bind_vip1} broadcast $ {broadcast} netmask $ {netmask} Up
/Sbin/route add-host $ {gateway} Dev eth0: HA1
}
Function_bind_vip2 ()
{
/Sbin/ifconfig eth0: ha2 $ {bind_vip2} broadcast $ {broadcast} netmask $ {netmask} Up
/Sbin/route add-host $ {gateway} Dev eth0: ha2
}
Function_restart_nginx ()
{
Kill-usr1 'cat/var/log/nginx. Pi'
}
Function_remove_vip1 ()
{
/Sbin/ifconfig eth0: HA1 $ {bind_vip1} broadcast $ {broadcast} netmask $ {netmask} Down
}
Function_remove_vip2 ()
{
/Sbin/ifconfig eth0: ha2 $ {bind_vip2} broadcast $ {broadcast} netmask $ {netmask} Down
}
Function_vip_arping1 ()
{
/Sbin/arping-I eth0-C 3-S ${bind_vip1 }1 {gateway}>/dev/null 2> & 1
}
Function_vip_arping2 ()
{
/Sbin/arping-I eth0-C 3-S ${bind_vip2 }$ {gateway}>/dev/null 2> & 1
}
Bind_time_vip1 = "N ";
Bind_time_vip2 = "N ";
While true
Do
Httpcode_rip1 = '/usr/bin/curl-O/dev/null-S-w % {http_code} http: // $ {web_ip_rip1}/status.htm'
Httpcode_rip2 = '/usr/bin/curl-O/dev/null-S-w % {http_code} http: // $ {web_ip_rip2}/status.htm'
# VIP1
If [x $ httpcode_rip1 = "x200"];
Then
If [$ bind_time_vip1 = "N"];
Then
Function_bind_vip1
Function_vip_arping1
Function_restart_nginx
Bind_time_vip1 = "Y"
Fi
Function_vip_arping1
Else
If [$ bind_time_vip1 = "Y"];
Then
Function_remove_vip1
Bind_time_vip1 = "N"
Fi
Fi
# VIP2
If [x $ httpcode_rip2 = "x200"];
Then
If [$ bind_time_vip2 = "Y"];
Then
Function_remove_vip2
Bind_time_vip2 = "N"
Fi
Else
If [$ bind_time_vip2 = "N"];
Then
Function_bind_vip2
Function_vip_arping2
Function_restart_nginx
Bind_time_vip2 = "Y"
Fi
Function_vip_arping2
Fi
Sleep 5
Done
[Switch] nginx dual-machine Hot Standby High Availability Solution