Nginx Server Load balancer dual-host High Availability

Source: Internet
Author: User

Nginx Server Load balancer dual-host High Availability

If the Web server cluster is used as a city pool, the load balancing server is equivalent to the city gate, and its importance is self-evident. If the "City Gate" is closed, the access to the outside world will be broken. If a server is balanced, when the server fails, the whole website will be inaccessible. Therefore, we need more than two servers to achieve failover and high availability.

Dual-host high availability is generally achieved through the virtual IP (floating IP) method, based on the Linux/Unix IP alias technology. Two High Availability methods are available:

(1): A server is added with a hot standby server. Normally, the master server is bound to a public network virtual IP address to provide the Server Load balancer service. The Hot Standby server is idle, when the master server fails, the Hot Standby server takes over the public network virtual IP address of the master server to provide the Server Load balancer service;

(2): Both Server Load balancer servers are active and each server is bound with a public IP address to provide the Server Load balancer service. When one of them fails, another public network virtual IP address that takes over the faulty server.

Method 1:
(1) Resolve the domain name www.bkjia.com to the virtual IP address 61.1.1.2
(2) normally, the host 61.1.1.4 is bound to the virtual IP 61.1.1.2.

/Sbin/ifconfig eth0: 1 61.1.1.2 broadcast 61.1.1.255 netmask limit 255.0 up
/Sbin/route add-host 61.1.1.2 dev eth0: 1
/Sbin/arping-I eth0-c 61.1.1.2 61.1.1.1
(3) User Access: www.bkjia.com (virtual IP: 61.1.1.2) actually accesses the host 61.1.1.4, while the slave machine 61.1.1.5 is idle.
(4) If the host 61.1.1.4 fails, the backend 61.1.1.5 takes over the virtual IP61.1.1.2 in a few seconds, binds it to itself, and sends the APping Public Network Gateway to refresh the MAC address.

/Sbin/ifconfig etho: 1 61.1.1.2 broadcast 61.1.1.255 netmask 255.255.255.0 up
/Sbin/ifconfig add-host 61.1.1.2 dev eth0: 1
/Sbin/arping-I etho-c 3-s 61.1.1.2 61.1.1.1
(5) In this case, the user accesses www.bkjia.com (virtual IP address) to actually access the slave machine 61.1.1.1, so as to achieve failover and high availability and avoid spof.

In addition, the first method can be implemented by using the Keeoalive Software Based on VRRP routing protocol.

Method 2:
(1): The domain name www.bkjia.com is resolved to the virtual IP address 61.1.1.2 and 61.1.1.3 through DNS round robin.
(2) Under normal circumstances, the server is bound with a virtual IP address.
Run the following command on server 161.1.1.4
/Sbin/ifconfig etho: 1 61.1.1.2 broadcast 61.1.1.255 netmask 255.255.255.0 up
/Sbin/ifconfig add-host 61.1.1.2 dev eth0: 1
/Sbin/arping-I etho-c 3-s 61.1.1.2 61.1.1.1
Run the following command on server 161.1.1.5:
/Sbin/ifconfig etho: 1 61.1.1.3 broadcast 61.1.1.255 netmask 255.255.255.0 up
/Sbin/ifconfig add-host 61.1.1.2 dev eth0: 1
/Sbin/arping-I etho-c 3-s 61.1.1.3 61.1.1.1
(3): users access www, bkjia.com (virtual IP address 61.1.1.2 and 61.1.1.3). In fact, they access two Server Load balancer servers through DNS round robin, and both servers are active.
(4) If Server 1 fails, Server 2 takes over the virtual IP address 61.1.1.2 of Server 1 in seconds and is bound to itself, and send the ARPing package to the Public Relations gateway of the IDC to refresh the new MAC address.
Run the following command on Server 2:
/Sbin/ifconfig etho: 1 61.1.1.2 broadcast 61.1.1.255 netmask 255.255.255.0 up
/Sbin/ifconfig add-host 61.1.1.2 dev eth0: 1
/Sbin/arping-I etho-c 3-s 61.1.1.2 61.1.1.1

We can write two scripts to implement the second method of automatic failover.

The following is a script deployed on Nginx Server Load balancer Server 1:

#! /Bin/sh
LANG = C
Date = $ (date-d "today" + "% Y-% m-% d % H: % M: % S ")

Function_bind_vip1 ()
{
/Sbin/ifconfig etho: ha1 61.1.1.2 broadcast 219.232.254.255 netmask bandwidth limit 192 up
/Sbin/route add-host 61.1.1.2 dev eth0: hal
}
Function_bind_vip2 ()
{

/Sbin/ifconfig etho: ha2 61.1.1.3 broadcast 219.232.254.255 netmask merge limit 192 up
/Sbin/route add-host 61.1.1.2 dev eth0: ha2
}
Function_restart_nginx ()
{
Kill-USR1 'cat/usr/local/webserver/nginx. pid'
}
Function_remove_vip1 ()
{
/Sbin/ifconfig etho: ha1 61.1.1.2 broadcast 219.232.254.255 netamsk bandwidth limit 192 down
}

Function_remove_vip2 ()
{
/Sbin/ifconfig etho: ha2 61.1.1.3 broadcast 219.232.254.255 netamsk bandwidth limit 192 down
}
Function_vip_arping1 ()
{
/Sbin/ifconfig-I eth0-c 3-e 611 .. 1.1.2 61.1.1.1>/dev/null 2> & 1
}
Function_vip_arping2 ()
{
/Sbin/ifconfig-I eth0-c 3-e 611 .. 1.1.3 61.1.1.1>/dev/null 2> & 1
}

Bind_time_vip1 = "N ";
Bind_time_vip2 = "N ";

While true
Do
Httpcode_arp1 = '/usr/bin/cur1-o/dev/null s-w % {http_code} http: // 61.1.1.4'
Httpcode_arp2 = '/usr/bin/cur1-o/dev/null s-w % {http_code} http: // 61.1.1.5'

If [x $ httpcode_rip1 = "x200"];
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_remote_vip1
Bind_time_vip = "N"
Fi
Fi

If [x $ httpcode_arp2 = "x200"];
Then
If [$ bind_time_vip2 = "Y"]
Then
Function_rempve_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
Run the script on Nginx Server 1 in the background:
Nohup/bin/sh./nginx_hal.sh 2> & 1> dev/null &

Run the following code on Server 2:

#! /Bin/sh
LANG = C
Date = $ (date-d "today" + "% Y-% m-% d % H: % M: % S ")

Function_bind_vip1 ()
{
/Sbin/ifconfig etho: ha1 61.1.1.3 broadcast 219.232.254.255 netmask merge limit 192 up
/Sbin/route add-host 61.1.1.3 dev eth0: hal
}
Function_bind_vip2 ()
{

/Sbin/ifconfig etho: ha2 61.1.1.2 broadcast 219.232.254.255 netmask bandwidth limit 192 up
/Sbin/route add-host 61.1.1.2 dev eth0: ha2
}
Function_restart_nginx ()
{
Kill-USR1 'cat/usr/local/webserver/nginx. pid'
}
Function_remove_vip1 ()
{
/Sbin/ifconfig etho: ha1 61.1.1.3 broadcast 219.232.254.255 netamsk bandwidth limit 192 down
}

Function_remove_vip2 ()
{
/Sbin/ifconfig etho: ha2 61.1.1.2 broadcast 219.232.254.255 netamsk bandwidth limit 192 down
}
Function_vip_arping1 ()
{
/Sbin/ifconfig-I eth0-c 3-e 61.1.1.3 61.1.1.1>/dev/null 2> & 1
}
Function_vip_arping2 ()
{
/Sbin/ifconfig-I eth0-c 3-e 61.1.1.2 61.1.1.1>/dev/null 2> & 1
}

Bind_time_vip1 = "N ";
Bind_time_vip2 = "N ";

While true
Do
Httpcode_arp1 = '/usr/bin/cur1-o/dev/null s-w % {http_code} http: // 61.1.1.5'
Httpcode_arp2 = '/usr/bin/cur1-o/dev/null s-w % {http_code} http: // 61.1.1.4'

If [x $ httpcode_rip1 = "x200"];
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_remote_vip1
Bind_time_vip = "N"
Fi
Fi

If [x $ httpcode_arp2 = "x200"];
Then
If [$ bind_time_vip2 = "Y"]
Then
Function_rempve_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

Run the script on Nginx Server 2 in the background:
Nohup/bin/sh./nginx_ha2.sh 2> & 1> dev/null &

For more Nginx tutorials, see the following:

Deployment of Nginx + MySQL + PHP in CentOS 6.2

Build a WEB server using Nginx

Build a Web server based on Linux6.3 + Nginx1.2 + PHP5 + MySQL5.5

Performance Tuning for Nginx in CentOS 6.3

Configure Nginx to load the ngx_pagespeed module in CentOS 6.3

Install and configure Nginx + Pcre + php-fpm in CentOS 6.4

Nginx installation and configuration instructions

Nginx log filtering using ngx_log_if does not record specific logs

Nginx details: click here
Nginx: click here

Link for permanent update of this 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.