Nginx+php+mysql dual-machine mutual standby, full automatic switching scheme _linux

Source: Internet
Author: User
Tags curl failover rsync
One, two-machine mutual standby, full automatic switching scheme:
1, Topology map:
  

2. Explanation:
(1), assuming the extranet domain name blog.s135.com to the external network virtual IP 72.249.146.214, LAN hosts set DB10 corresponding intranet virtual IP 192.168.146.214

(2), by default, by the host binding internal and external network virtual IP, standby as a backup, when the host of MySQL, nginx or server failure can not access, the opportunity to automatically take over the internal and external network virtual IP. Both servers start daemons responsible for monitoring, automatically switching virtual IP/usr/bin/nohup/bin/sh/usr/local/webserver/failover/failover.sh 2>&1 >/dev/ Null &

(3), host and standby MySQL server on the main from each other, synchronized. When the host is active (that is, the virtual IP is bound by the host), read and write host MySQL, write to the host data will be synchronized to the standby machine, in the standby machine is active, read and write standby MySQL, write to the standby data will sync to the host (if the host on the MySQL dead temporarily can not sync, After MySQL is restored on the host computer, the data is automatically synchronized from the standby and vice versa.

(4), the host is active, every 20 seconds will/data0/htdocs/(Web page, program, picture storage directory),/usr/local/webserver/php/etc/(PHP.ini and other profiles),/usr/local/ webserver/nginx/conf/(nginx configuration file directory) files in three directories are pushed to the corresponding directory on the standby server via rsync (incremental push, the same files on both servers are not pushed repeatedly), or if the standby is active, Every 20 seconds an attempt is made to push the file to the host. The configuration file for rsync see the Launch command for the/etc/rsyncd.conf,rsync daemon for two servers for rsync--daemon

   3. Automatic switching process
(1), host default binding inside, extranet virtual IP, when the host MySQL, nginx inaccessible or server downtime, the host of the failover.sh daemon will automatically remove themselves bound within, External network virtual IP (if the host on the failover.sh die, can not remove their own bound virtual IP also does not matter), standby on the failover.sh daemon will automatically take over the original binding internal and external network virtual IP, and send arping packet to the internal and external network gateway Update Mac, Forced to take over.

(2), the standby machine binding virtual IP, will send arping packet to the internal and external network gateway, notify the gateway to update the virtual IP MAC address for the standby MAC address, so as to ensure that the switch can be accessed through virtual IP timely access to the standby machine.

(3), if the host MySQL, nginx start up, all back to normal access, the host of the failover.sh daemon will detect the host on the MySQL data has been completely from the standby synchronization over. If the synchronization delay time is 0, the host will automatically take over the internal and external network virtual IP, and send the arping packet to the internal and external network gateway, and the standby machine will automatically remove the internal and external network virtual IP.

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

   4, note (very important):
(1), crontab in the file does not do automatic synchronization, if modified, need to manually on both servers to make changes.

(2),/data0/htdocs/directory of any use of ln-s established soft connection, rsync will not automatically sync, if a server built a soft connection, you need to manually on another server also build the same soft connection.

(3) If you want to delete some files or directories within the/data0/htdocs/directory, you need to delete the files or directories that are active (that is, the virtual IP) server, and then delete the files or directories that are on the standby state server.

(4), in addition to/data0/htdocs/(Web page, program, picture storage directory),/usr/local/webserver/php/etc/(php.ini and other configuration Files directory),/usr/local/webserver/nginx/ conf/(Nginx profile directory) configuration changes other than three directories need to be modified on both servers.
ii. configuration documents and scripts:
1, host, standby two server rsync configuration (same configuration)
(1), rsync configuration file
Copy Code code as follows:

Vi/etc/rsyncd.conf

Enter some content and save:
Copy Code code as follows:

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 mutually mainly from the configuration
This is not detailed in the configuration of the main from the process, if you do not understand the friend can search on Google. It is important to point out that my.cnf configuration file, please add skip-name-resolve parameters, using IP for MySQL account authentication.

3, host, standby machine two server load monitoring, virtual IP automatic switching failover.sh daemon process
(1), start the failover.sh daemon (to be able to automatically run the boot, add the following statement to the/etc/rc.local file):

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


(2), Stop 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/failover.sh
Root 20123 6878 0 16:16 pts/2 00:00:00 grep failover.sh
And then kill the failover.sh process:

Kill-9 15428


(3), failover.sh code content (note that the type setting, the host is set to master, the standby is set to slave):
Copy Code code as follows:

#!/bin/sh
Lang=c
date=$ (date-d "Today" + "%y-%m-%d%h:%m:%s")

#---------------configuration information (start)---------------
#类型: The host is set to master and the standby machine is set to slave
Type= "Master"

#主机, Standby switch log path
Logfile= "/var/log/failover.log"

#MySQL可执行文件地址, such as/usr/local/mysql/bin/mysql;mysql username; password; port
Mysql_bin= "/usr/local/webserver/mysql/bin/mysql"
Mysql_username= "Root"
Mysql_password= "123456"
Mysql_port= "3306"

#内网网关
gateway_eth0= "192.168.146.1"

#主机内网真实IP
Rip_eth0_master= "192.168.146.213"

#备机内网真实IP
Rip_eth0_slave= "192.168.146.215"

Virtual IP shared by #主机 and standby intranet
Vip_eth0_share= "192.168.113.214"


#外网网关
gateway_eth1= "72.249.146.193"

#主机外网真实IP
Rip_eth1_master= "72.249.146.213"

#备机外网真实IP
Rip_eth1_slave= "72.249.146.215"

#主机, the virtual IP shared by the external network of standby machine
Vip_eth1_share= "72.249.146.214"
#---------------configuration information (end)---------------

#绑定内, external network virtual IP
FUNCTION_BIND_VIP ()
{
/sbin/ifconfig ETH0:VIP ${vip_eth0_share} broadcast ${vip_eth0_share} netmask 255.255.255.255 up
/sbin/route add-host ${vip_eth0_share} dev Eth0:vip
/sbin/ifconfig ETH1:VIP ${vip_eth1_share} broadcast ${vip_eth1_share} netmask 255.255.255.255 up
/sbin/route add-host ${vip_eth1_share} 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
}

#解除内, external network virtual IP
FUNCTION_REMOVE_VIP ()
{
/sbin/ifconfig ETH0:VIP ${vip_eth0_share} broadcast ${vip_eth0_share} netmask 255.255.255.255 down
/sbin/ifconfig ETH1:VIP ${vip_eth1_share} broadcast ${vip_eth1_share} netmask 255.255.255.255 down
/sbin/service Crond Stop
}

#主机向备机推送文件的函数
Function_rsync_master_to_slave ()
{
/usr/bin/rsync-zrtuog/data0/htdocs/${rip_eth0_slave}::d ata0_htdocs/>/dev/null 2>&1
/usr/bin/rsync-zrtuog/usr/local/webserver/php/etc/${rip_eth0_slave}::p hp_etc/>/dev/null 2>&1
/usr/bin/rsync-zrtuog/usr/local/webserver/nginx/conf/${rip_eth0_slave}::nginx_conf/>/dev/null 2>&1
}

#备机向主机推送文件的函数
Function_rsync_slave_to_master ()
{
/usr/bin/rsync-zrtuog/data0/htdocs/${rip_eth0_master}::d ata0_htdocs/>/dev/null 2>&1
/usr/bin/rsync-zrtuog/usr/local/webserver/php/etc/${rip_eth0_master}::p hp_etc/>/dev/null 2>&1
/usr/bin/rsync-zrtuog/usr/local/webserver/nginx/conf/${rip_eth0_master}::nginx_conf/>/dev/null 2>&1
}

#虚拟IP arping
Function_vip_arping ()
{
/sbin/arping-i eth0-c 3-s ${vip_eth0_share} ${gateway_eth0} >/dev/null 2>&1
/sbin/arping-i eth1-c 3-s ${vip_eth1_share} ${gateway_eth1} >/dev/null 2>&1
}

While True
Todo
#用HTTP协议检查虚拟IP
if (curl-m 30-g http://${vip_eth1_share}/>/dev/null 2>&1) && (${mysql_bin}-U "${mysql_username}"-P " ${mysql_password} "-P" ${mysql_port} "-H" ${vip_eth0_share} "-e" show slave status\g ">/dev/null 2>&1)
Then
#取得与内网VIP绑定的服务器内网IP
eth0_active_server=$ (${mysql_bin}-U "${mysql_username}"-P "${mysql_password}"-P "${mysql_port}"-H "${vip_eth0_ Share} "-e" show slave Status\g | grep "Master_host" | Awk-f ': ' {printf $} ')

#如果内网VIP = Host Intranet IP (master_host in host MySQL shows the domain name of the standby machine or IP), and this machine is host
if ["${eth0_active_server}" = "${rip_eth0_slave}"] && ["${type}" = "Master"]
Then
Function_rsync_master_to_slave
Function_vip_arping
#如果内网VIP = Standby Intranet IP (Master_host in the standby MySQL displays the host's domain name or IP)
elif ["${eth0_active_server}" = "${rip_eth0_master}"]
Then
if (curl-m 30-g http://${rip_eth1_master}/>/dev/null 2>&1) && (${mysql_bin}-U "${mysql_username}"-P "${mysql_password}"-P "${mysql_port}"-H "${rip_eth0_master}"-e "show slave Status\g" | grep "seconds_behind_master:0" >/dev/null 2>&1)
Then
#如果主机能够访问, database synchronization without delay, and this machine is the host, then the native bound virtual IP
if ["${type}" = "Master"]
Then
#如果本机为主机
Function_bind_vip
Function_vip_arping
echo "${date} host is bound to virtual ip! (type:1) ">> ${logfile}"
Else
#如果本机为备机
Function_remove_vip
echo "${date} standby has removed virtual ip! (type:2) ">> ${logfile}"
Fi
Else
if ["${type}" = "slave"]
Then
#如果本机为备机
Function_rsync_slave_to_master
Function_vip_arping
Fi
Fi
Fi
Else
#虚拟IP无法访问时, to determine whether the host can access
if (curl-m 30-g http://${rip_eth1_master}/>/dev/null 2>&1) && (${mysql_bin}-U "${mysql_username}"-P "${mysql_password}"-P "${mysql_port}"-H "${rip_eth0_master}"-e "show slave status\g" >/dev/null 2>&1)
Then
#如果主机能够访问, and this machine is the host, then the native bound virtual IP
if ["${type}" = "Master"]
Then
Function_bind_vip
Function_vip_arping
echo "${date} host is bound to virtual ip! (type:3) ">> ${logfile}"
Else
Function_remove_vip
echo "${date} standby has removed virtual ip! (type:4) ">> ${logfile}"
Fi
Elif (curl-m 30-g http://${rip_eth1_slave}/>/dev/null 2>&1) && (${mysql_bin}-U "${mysql_username}"- P "${mysql_password}"-P "${mysql_port}"-H "${rip_eth0_slave}"-e "show slave status\g" >/dev/null 2>&1)
Then
#如果主机不能访问而备机能够访问, and this machine is a standby, then the standby machine binding virtual IP
if ["${type}" = "slave"]
Then
Function_bind_vip
Function_vip_arping
echo "${date} standby is bound to virtual ip! (type:5) ">> ${logfile}"
Else
Function_remove_vip
The echo "${date} host has removed the virtual ip! (type:6) ">> ${logfile}"
Fi
Else
echo "${date} host, standby all cannot access!" (type:7) ">> ${logfile}"
Fi
Fi
#每次循环暂停20秒 (at intervals of 20 second detection)
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.