Nginx + PHP + MySQL dual-host mutual backup and automatic switch solution [original]

Source: Internet
Author: User

[Author: Zhang banquet this article version: V1.0 final modification: 2008.11.19 reprint please indicate the original article link: http://blog.s135.com/post/#/]

In production applications, a data server with the "nginx + PHP + MySQL" interface plays a very important role. If the server hardware or nginx or MySQL fails, however, it cannot be recovered within a short period of time, and the consequences will be very serious. In order to avoid single point of failure, I designed this solution and compiled the Failover. Sh script to implement dual-host mutual backup and automatic failover. The Failover takes only dozens of seconds.

  I. Dual-host mutual backup and automatic switching solution:
  1. Topology:
  

  2. Explanation:
(1) assume that the Internet domain name blog.s135.com is resolved to the Internet virtual IP address 72.249.146.214, and the Intranet hosts sets db10 to the Intranet virtual IP address 192.168.146.214.

(2) by default, the host is bound with an intranet or Internet virtual IP address, and the slave is used as a backup. When the MySQL, nginx, or server of the host fails to be accessed, the slave node automatically takes over the Intranet and Internet virtual IP addresses. Both servers start the daemon process that monitors and automatically switches the virtual IP Address/usr/bin/nohup/bin/sh/usr/local/webserver/failover. sh 2> & 1>/dev/null &

(3) The master and slave MySQL servers are mutually active and standby. When the host is active (that is, when the host is bound with a virtual IP address), the MySQL of the read/write host and data written to the host will be synchronized to the slave machine; when the slave machine is active, the data written to the slave database is synchronized to the master database, the data is automatically synchronized from the backup machine, and vice versa ).

(4) When the host is active,/data0/htdocs/(directory for storing webpages, programs, and images) is stored every 20 seconds) /usr/local/webserver/PHP/etc/(PHP. INI and other configuration files),/usr/local/webserver/nginx/CONF/(nginx configuration file directory) files under the three directories are pushed to the corresponding directory on the backup server through rsync (incremental push, the same files on the two servers are not repeatedly pushed ), if the standby machine is active, files are pushed to the host every 20 seconds. For the rsync configuration file, see/etc/rsyncd. conf on the two servers. The Rsync daemon is the startup command of the rsync daemon.

  3. Automatic Switch Process
(1) by default, the host is bound to an intranet or Internet virtual IP address. When the MySQL and nginx of the host cannot be accessed or the server goes down, the Failover on the host. the sh daemon automatically removes the internal and external virtual IP addresses bound to it (if the Failover on the host. SH is dead, and it does not matter if the virtual IP address bound to the slave server cannot be removed. the sh daemon automatically takes over the internal and external virtual IP addresses bound to the standby machine, sends an arping packet to the internal and external network gateways to update the MAC address, and forcibly takes over the IP address.

(2) After the slave machine is bound with a virtual IP address, it will send an arping packet to the Intranet and Internet gateways, notifying the gateway to update the MAC address of the virtual IP address to the MAC address of the slave machine, this ensures that the slave server can be accessed in a timely manner through the virtual IP address after the switchover.

(3) If the MySQL and nginx of the host are started, all access will be restored, and the Failover on the host will be restored. the sh daemon checks whether Mysql Data on the host has been fully synchronized from the slave machine. If the synchronization delay time is 0, the host automatically takes over the internal and external virtual IP addresses, and sends an arping packet to the internal and external network gateways. The standby machine also automatically removes the internal and external virtual IP addresses.

(4) The entire switching process is automatically completed by failover. sh without manual processing.

  4. Notes (important ):
(1) the files in crontab are not automatically synchronized. If the files are modified, they must be manually modified on both servers.

(2) In the/data0/htdocs/directory, rsync will not automatically synchronize any soft connections established with Ln-S. If a soft connection is established on a server, you need to manually create the same soft connection on another server.

(3) If you want to delete some files or directories in the/data0/htdocs/directory, you must first delete the files or directories on the Active Server (that is, the virtual IP address is bound, then, delete the files or directories on the backup server.

(4) Except/data0/htdocs/(web page, program, image storage directory),/usr/local/webserver/PHP/etc/(PHP. INI and other configuration file directories) and/usr/local/webserver/nginx/CONF/(nginx configuration file directory, it must be modified on both servers.

  Ii. Configuration documents and scripts:
  1. Configure rsync for the host and backup servers (same configuration)
(1) rsync configuration file

VI/etc/rsyncd. conf

Enter some content and save it:

Reference uid = root
Gid = root
Use chroot = No
Max connections = 20
PID file =/var/run/rsyncd. PID
Lock file =/var/run/rsync. Lock
Log File =/var/log/rsyncd. Log

[Data0_htdocs]
Path =/data0/htdocs/
Ignore errors
Read Only = No
Hosts allow = 192.168.146.0/24
Hosts deny = 0.0.0.0/32

[Php_etc]
Path =/usr/local/webserver/PHP/etc/
Ignore errors
Read Only = No
Hosts allow = 192.168.146.0/24
Hosts deny = 0.0.0.0/32

[Nginx_conf]
Path =/usr/local/webserver/nginx/CONF/
Ignore errors
Read Only = No
Hosts allow = 192.168.146.0/24
Hosts deny = 0.0.0.0/32

(2) Start the rsync daemon

/Usr/bin/rsync -- daemon

  2. Two MySQL instances are configured as master-slave instances.
Here we will not detail the configuration process of mutual master and slave. If you do not understand it, you can search for it on Google. Note that the skip-name-resolve parameter must be added to the my. CNF configuration file to use IP addresses for MySQL account verification.

  3. failover. Sh daemon for server load monitoring and automatic failover of virtual IP addresses
(1) Start the Failover. Sh daemon (Add the following statement to the/etc/rc. Local file to automatically run the Failover. Sh daemon on ):

/Usr/bin/nohup/bin/sh/usr/local/webserver/failover. Sh 2> & 1>/dev/null &

(2) Stop the Failover. Sh daemon:

PS-Ef | grep failover. Sh

The following information is displayed:
Root 15428 1 0 nov17? 00:00:03/bin/sh/usr/local/webserver/failover. Sh
Root 20123 6878 0 00:00:00 pts/2 grep failover. Sh
Then kill the Failover. Sh process:

Kill-9 15428

(3) failover. Sh code (note the type settings, set the host to master, and the slave to slave ):

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

# --------------- Configuration information (start )---------------
# Type: Set the host to master and the slave to slave.
Type = "master"

# Log Path for host/backup Switch
Logfile = "/var/log/failover. log"

# MySQL executable file address, such as/usr/local/MySQL/bin/MySQL; MySQL user name; password; Port
Mysql_bin = "/usr/local/webserver/MySQL/bin/MySQL"
Mysql_username = "root"
Mysql_password = "123456"
Mysql_port = "3306"

# Intranet Gateway
Gateway_eth0 = "192.168.146.1"

# Host Intranet real IP Address
Rip_eth0_master = "192.168.146.213"

# Real Intranet IP address of the standby machine
Rip_eth0_slave = "192.168.146.215"

# Virtual IP addresses shared by the Intranet of the host and slave
Vip_eth0_share = "192.168.113.214"

# Internet gateway
Gateway_eth1 = "72.249.146.193"

# Real Internet IP address of the host
Rip_eth1_master = "72.249.146.213"

# Real Internet IP address of the standby machine
Rip_eth1_slave = "72.249.146.215"

# Virtual IP addresses shared by external networks of hosts and backup hosts
Vip_eth1_share = "72.249.146.214"
# --------------- Configuration information (end )---------------

# Binding Intranet and Internet virtual IP addresses
Function_bind_vip ()
{
/Sbin/ifconfig eth0: VIP $ broadcast $ netmask running 255.255.255 up
/Sbin/route add-host $ Dev eth0: VIP
/Sbin/ifconfig eth1: VIP $ broadcast $ netmask running 255.255.255 up
/Sbin/route add-host $ Dev eth1: VIP
/Usr/local/webserver/PHP/sbin/PHP-FPM reload
Kill-usr1 'cat/usr/local/webserver/nginx/logs/nginx. pid'
/Sbin/service crond start
}

# Remove the Intranet and Internet virtual IP addresses
Function_remove_vip ()
{
/Sbin/ifconfig eth0: VIP $ broadcast $ netmask 255.255.255.255 down
/Sbin/ifconfig eth1: VIP $ broadcast $ netmask 255.255.255.255 down
/Sbin/service crond stop
}

# Functions used by the host to push files to the slave server
Function_rsync_master_to_slave ()
{
/Usr/bin/rsync-zrtuog/data0/htdocs/$: data0_htdocs/>/dev/null 2> & 1
/Usr/bin/rsync-zrtuog/usr/local/webserver/PHP/etc/$: php_etc/>/dev/null 2> & 1
/Usr/bin/rsync-zrtuog/usr/local/webserver/nginx/CONF/$: nginx_conf/>/dev/null 2> & 1
}

# Functions used by the slave to push files to the host
Function_rsync_slave_to_master ()
{
/Usr/bin/rsync-zrtuog/data0/htdocs/$: data0_htdocs/>/dev/null 2> & 1
/Usr/bin/rsync-zrtuog/usr/local/webserver/PHP/etc/$: php_etc/>/dev/null 2> & 1
/Usr/bin/rsync-zrtuog/usr/local/webserver/nginx/CONF/$: nginx_conf/>/dev/null 2> & 1
}

# Virtual IP address arping
Function_vip_arping ()
{
/Sbin/arping-I eth0-C 3-S $>/dev/null 2> & 1
/Sbin/arping-I eth1-C 3-S $>/dev/null 2> & 1
}

While true
Do
# Use http to check virtual IP addresses
If (curl-M 30-G http: // $/>/dev/null 2> & 1) & ($-U "$"-P "$"-P "$"-h "$"-e "show slave status \ G">/dev/null 2> & 1)
Then
# Obtain the server Intranet IP address bound to the Intranet VIP
Eth0_active_server = $ ($-U "$"-P "$"-P "$"-h "$"-e "show slave status \ G" & amp; #124; grep "master_host" & #124; awk-F': ''{printf $2 }')

# If Intranet VIP = Intranet IP address of the host (master_host in MySQL displays the domain name or IP address of the backup host), and the local host is the host
If ["$" = "$"] & ["$" = "master"]
Then
Function_rsync_master_to_slave
Function_vip_arping
# If the Intranet VIP address is the Intranet IP address of the slave machine (master_host in MySQL displays the domain name or IP address of the host)
Elif ["$" = "$"]
Then
If (curl-M 30-G http: // $/>/dev/null 2> & 1) & ($-U "$"-P "$"-P "$"-h "$"-e "show slave status \ G" & amp; #124; grep "seconds_behind_master: 0">/dev/null 2> & 1)
Then
# If the host can access the database without delay and the local machine is the host, the local machine is bound with a virtual IP Address
If ["$" = "master"]
Then
# If the local host is a host
Function_bind_vip
Function_vip_arping
Echo "$ the host has been bound to a virtual IP address! (Type: 1) ">>$
Else
# If the local machine is a backup machine
Function_remove_vip
Echo "$ the virtual IP address has been removed from the backup host! (Type: 2) ">>$
Fi
Else
If ["$" = "slave"]
Then
# If the local machine is a backup machine
Function_rsync_slave_to_master
Function_vip_arping
Fi
Fi
Fi
Else
# When the virtual IP address cannot be accessed, determine whether the host can be accessed
If (curl-M 30-G http: // $/>/dev/null 2> & 1) & ($-U "$"-P "$"-P "$"-h "$"-e "show slave status \ G">/dev/null 2> & 1)
Then
# If the host can access and the local host is the host, the local host is bound with a virtual IP Address
If ["$" = "master"]
Then
Function_bind_vip
Function_vip_arping
Echo "$ the host has been bound to a virtual IP address! (Type: 3) ">>$
Else
Function_remove_vip
Echo "$ the virtual IP address has been removed from the backup host! (Type: 4) ">>$
Fi
Elif (curl-M 30-G http: // $/>/dev/null 2> & 1) & ($-U "$"-P "$"-P "$"-h "$"-e "show slave status \ G">/dev/null 2> & 1)
Then
# If the host cannot be accessed but the slave can be accessed and the local machine is a slave machine, the slave machine is bound with a virtual IP address.
If ["$" = "slave"]
Then
Function_bind_vip
Function_vip_arping
Echo "$ the backup machine is bound with a virtual IP address! (Type: 5) ">>$
Else
Function_remove_vip
Echo "$ virtual IP addresses have been removed from the host! (Type: 6) ">>$
Fi
Else
Echo "$ all hosts and slave hosts cannot be accessed! (Type: 7) ">>$
Fi
Fi
# Pause every cycle for 20 seconds (that is, detect every 20 seconds)
Sleep 20
Done

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.