[文章作者:张宴 本文版本:v1.0 最后修改:2008.11.19 转载请注明原文链接:http://blog.zyan.cc/post/379/]

  在生产应用中,某台“Nginx+PHP+MySQL”接口数据服务器,扮演的角色十分重要,如果服务器硬件或Nginx、MySQL发生故障,而短时间内无法恢复,后果将非常严重。为了避免单点故障,我设计了此套方案,编写了failover.sh脚本,实现了双机互备、全自动切换,故障转移时间只需几十秒。

  一、双机互备、全自动切换方案:
  1、拓扑图:
  点击在新窗口中浏览此图片

  2、解释:
  (1)、假设外网域名blog.zyan.cc解析到外网虚拟IP 72.249.146.214上,内网hosts设置db10对应内网虚拟IP 192.168.146.214

  (2)、默认情况下,由主机绑定内、外网虚拟IP,备机作为备份,当主机的MySQL、Nginx或服务器出现故障无法访问时,备机会自动接管内、外网虚拟IP。两台服务器都启动负责监控、自动切换虚拟IP的守护进程/usr/bin/nohup /bin/sh /usr/local/webserver/failover/failover.sh 2>&1 > /dev/null &

  (3)、主机和备机上的MySQL服务器互为主从,互相同步。在主机处于活动状态(即由主机绑定虚拟IP)时,读写主机的MySQL,写到主机的数据会同步到备机;在备机处于活动状态时,读写备机的MySQL,写到备机的数据会同步到主机(如果主机上的MySQL死掉暂时无法同步,主机上的MySQL恢复后,数据会自动从备机上同步过来,反之亦然)。

  (4)、主机处于活动状态时,每20秒会把/data0/htdocs/(网页、程序、图片存放目录)、/usr/local/webserver/php/etc/(php.ini等配置文件目录)、/usr/local/webserver/nginx/conf/(Nginx配置文件目录)三个目录下的文件通过rsync推送到备机服务器上的对应目录(增量推送,两台服务器上一样的文件不会重复推送),反之如果备机处于活动状态时,每20秒会尝试把文件推送到主机。rsync的配置文件见两台服务器的/etc/rsyncd.conf,rsync守护进程的启动命令为rsync --daemon

  3、自动切换流程
  (1)、主机默认绑定内、外网虚拟IP,当主机的MySQL、Nginx无法访问或服务器宕机,主机上的failover.sh守护进程会自动摘除自己绑定的内、外网虚拟IP(如果主机上的failover.sh死掉,无法摘除自己绑定的虚拟IP也没关系),备机上的failover.sh守护进程会自动接管备机原来绑定的内、外网虚拟IP,并发送ARPing包给内、外网网关更新MAC,强行接管。

  (2)、备机绑定虚拟IP后,会发送ARPing包给内、外网网关,通知网关更新虚拟IP的MAC地址为备机的MAC地址,从而保证了切换后能够通过虚拟IP及时访问到备机。

  (3)、如果主机的MySQL、Nginx启动起来,全部恢复正常访问,主机上的failover.sh守护进程会检测主机上的MySQL数据是否已经完全从备机上同步过来。如果同步延迟时间为0,主机会自动接管内、外网虚拟IP,并发送ARPing包给内、外网网关,而备机也会自动摘除内、外网虚拟IP。

  (4)、整个切换流程均由failover.sh自动完成,无需人工处理。

  4、注意事项(很重要):
  (1)、crontab里的文件没有做自动同步,如果修改,需要手工在两台服务器上都做修改。

  (2)、/data0/htdocs/目录内任何用ln -s建立的软连接,rsync不会自动同步,如果在一台服务器上建了软连接,需要手工在另外一台服务器上也建相同的软连接。

  (3)、如果要删除/data0/htdocs/目录内的某些文件或目录,需要先删除处于活动状态(即绑定了虚拟IP)服务器上的文件或目录,再删除处于备用状态服务器上的文件或目录。

  (4)、除了/data0/htdocs/(网页、程序、图片存放目录)、/usr/local/webserver/php/etc/(php.ini等配置文件目录)、/usr/local/webserver/nginx/conf/(Nginx配置文件目录)三个目录之外的其他配置修改,需要在两台服务器上都做修改。



  二、配置文档与脚本:
  1、主机、备机两台服务器的rsync配置(配置相同)
  (1)、rsync配置文件
vi /etc/rsyncd.conf

  输入一些内容并保存:
引用
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)、启动rsync守护进程
/usr/bin/rsync --daemon


  2、两台MySQL互为主从的配置
  这里就不详细写出互为主从的配置过程了,如果不懂的朋友可以在Google上搜一下。有一点需要指出,my.cnf配置文件中请加上skip-name-resolve参数,使用IP来进行MySQL帐号验证。

  3、主机、备机两台服务器负载监控、虚拟IP自动切换的failover.sh守护进程
  (1)、启动failover.sh守护进程(为了开机能够自动运行,请将以下语句添加到/etc/rc.local文件中):
/usr/bin/nohup /bin/sh /usr/local/webserver/failover/failover.sh 2>&1 > /dev/null &


  (2)、停止failover.sh守护进程:
ps -ef | grep failover.sh

  会显示以下信息:
  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

  然后杀死failover.sh的进程:
kill -9 15428


  (3)、failover.sh代码内容(请注意其中的type设置,主机设为master,备机设为slave):
#!/bin/sh
LANG=C
date=$(date -d "today" +"%Y-%m-%d %H:%M:%S")

#---------------配置信息(开始)---------------
#类型:主机设为master,备机设为slave
type="master"

#主机、备机切换日志路径
logfile="/var/log/failover.log"

#MySQL可执行文件地址,例如/usr/local/mysql/bin/mysql;MySQL用户名;密码;端口
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"

#主机、备机内网共用的虚拟IP
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"

#主机、备机外网共用的虚拟IP
vip_eth1_share="72.249.146.214"
#---------------配置信息(结束)---------------

#绑定内、外网虚拟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
}

#解除内、外网虚拟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}::data0_htdocs/ > /dev/null 2>&1
    /usr/bin/rsync -zrtuog /usr/local/webserver/php/etc/ ${rip_eth0_slave}::php_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}::data0_htdocs/ > /dev/null 2>&1
    /usr/bin/rsync -zrtuog /usr/local/webserver/php/etc/ ${rip_eth0_master}::php_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
do
    #用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 $2}')
    
    #如果内网VIP=主机内网IP(主机MySQL中的Master_Host显示的是备机的域名或IP),且本机为主机
    if [ "${eth0_active_server}" = "${rip_eth0_slave}" ] && [ "${type}" = "master" ]
    then
      function_rsync_master_to_slave
      function_vip_arping    
    #如果内网VIP=备机内网IP(备机MySQL中的Master_Host显示的是主机的域名或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
        #如果主机能够访问,数据库同步无延迟,且本机就是主机,那么由本机绑定虚拟IP
        if [ "${type}" = "master" ]
        then
          #如果本机为主机
       function_bind_vip
          function_vip_arping
          echo "${date} 主机已绑定虚拟IP!(Type:1)" >> ${logfile}
        else
          #如果本机为备机
       function_remove_vip
          echo "${date} 备机已去除虚拟IP!(Type:2)" >> ${logfile}
        fi
      else
        if [ "${type}" = "slave" ]
        then
          #如果本机为备机
       function_rsync_slave_to_master        
          function_vip_arping
        fi      
      fi
    fi
    else
        #虚拟IP无法访问时,判断主机能否访问
        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
            #如果主机能够访问,且本机就是主机,那么由本机绑定虚拟IP
            if [ "${type}" = "master" ]
            then
                function_bind_vip
                function_vip_arping
                echo "${date} 主机已绑定虚拟IP!(Type:3)" >> ${logfile}
            else
                function_remove_vip
                echo "${date} 备机已去除虚拟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
            #如果主机不能访问而备机能够访问,且本机就是备机,那么由备机绑定虚拟IP
            if [ "${type}" = "slave" ]
            then
                function_bind_vip
                function_vip_arping
                echo "${date} 备机已绑定虚拟IP!(Type:5)" >> ${logfile}
            else
                function_remove_vip
                echo "${date} 主机已去除虚拟IP!(Type:6)" >> ${logfile}
            fi
        else
            echo "${date} 主机、备机全部无法访问!(Type:7)" >> ${logfile}
        fi
    fi
    #每次循环暂停20秒(即间隔20秒检测一次)
    sleep 20
done


Tags: , , , ,
技术大类 » Web服务器 | 评论(78) | 引用(123) | 阅读(132761)
louis vuitton uk Email Homepage
2011-11-23 09:05
This louis vuitton uk for sale belongs to the sounding just what are termed as Louis Vuitton vintage best sellers, many other products and services for the reason that range appearing companies.You will easily notice the unfold zippers of this coach outlet store online. That is the decoration. There are some inside pockets for you as well. They are easy to match your clothes and to carry.Let us inspire your inner beauty with fine christian louboutin sale. Purse the elegance in bridal wedding. Enjoy the fashion.
zhijun
2011-12-20 16:50
为什么不用keepalived实现双机主备呢?
Hogan
2012-5-10 18:12
Ci sono diversi motivi principali per cui si può sicuramente trovare sul proprio volere le soluzioni contabili che coinvolgono tutta Stansted, anche se alcune persone potrebbero uomini e donne, probabilmente si dovrà disporre di soluzioni contabili rispetto con la gente. Stai con me per scoprire di più su molti dei motivi per prendere in considerazione l'impiego contabili in tutta Hogan Stansted. Costruire un businessIf vi capita di essere in funzione, è abbastanza possibile che si può fare uso di soluzioni contabili. Non date per scontato tutte le società hanno abbastanza soldi per utilizzare a tempo pieno il personale fiscali, così utilizzando una conditi insieme con l'agenzia affidabile che coinvolge tutta commercialisti Stansted potrebbe rendere meno difficili problemi.
coach factory outlet Email Homepage
2012-5-17 11:50
Today, following half a century, mentor leather-bases coach factory outlet continues to be the delicate craft of leather-based master is accountable for,Would you like to meet more friends, or go with the times? If yes, coach factory online is opening welcome doors to you.in the market you definitely can find various colorways that are designed in as well as the high quality that applied in. For most of you would like to come. So just come to our coach factory outlet online store to choose one.
louis vuitton sale Email Homepage
2012-5-17 11:50
Louis Vuitton belt at louis vuitton sale is one kind of fashion accessory with high cost performance among the Louis Vuitton accessories.Offering quality LV products with favorable prices, louis vuitton outlet store is at your service. Hurry up, or you can not seize the chance.in fact, louis vuitton is one of the most famous fashion design master.he opened the fist suitcase shop called after his name.
coach outlet online Email Homepage
2012-5-17 11:50
I heard of coach outlet online through the advertisement when I was shopping. And now I often brow the webpage and buy Coach bags online.It is a symbol regarding position not to mention nature.Here I would like to launch a excellent bags pertaining to business men.Which may be coach outlet store.coach outlet has always been simple,durable style features to win consumers.The products are more flexible,with easy bleaching,wear characteristics,and simply use a damp cloth.
coach outlet, Email Homepage
2012-5-17 11:50
Remember the coach outlet provide coach bags which won't be deteriorated into its overall styles by any means. It will maintain its looks, colors, and uniqueness for long time.coach outlet store online has been voted by Hour Detroit magazine readers as the Best of Detroit in their 12th annual readers'poll.Lots of women like which usually amount normally include a coach outlet online ,it provides coziness to many girls that don't even think it is a great bushel of great interest directly to them.
xujie777 Email
2012-5-18 16:52
We aim to make all our customers satisfy with our products. You will find a variety of Men's fashion louis vuitton uk, fashion Women's cheap Louis Vuitton bags in our store at affordable price.Thinking of interesting ways to cost a milestone birthday? louis vuitton online shop had one of the most distinctive distinctive celebrations.bakery along with living room operated by means of about three moment louis vuitton online Most effective Pastry Chef’s of the year Rammy Nominee Chef’s.
xujie777 Email
2012-5-18 16:53
The choices are likely to be basically countless seeing that louis vuitton outlet occurs with the help of completely new and also incredible concepts once in a while.Louis vuitton Wholesale Monogram Canvas HandbagsLouis Vuitton Collection Beach Handbags louis vuitton bags outlet Damier Canvas HandbagsLouis vuitton Mahina HandbagsLouis Vuitton Monogram Mini Lin HandbagsLouis Vuitton Monogram Multicolore HandbagsLouis vuitton Monogram Vernis HandbagsLouis Vuitton Wholesale Epi Leather HandbagsLouis Vuitton For Men HandbagsLouis Vuitton Damier Canvas WalletsLouis Vuitton Epi Leather WalletsLouis Vuitton Monogram Canvas WalletsLouis Vuitton Monogram Vernis WalleLouis Vuitton ShoesLouis Vuitton Men wallets.As the Authentic Louis Vuitton are so high-priced, so came the louis vuitton handbags outlet.
xujie777 Email
2012-5-18 16:53
coach outlet has become a popular shopping experience for consumers around the world, and a desirable distribution channel for manufacturer's and retailers.Getting your hands on coach outlet store online can be a hefty investment of hundreds of dollars.But do not despair,the Coach Outlet store could be the answer to your prayers.coach outlet online is your smart choice when you want to get the discount Coach accessory. You can find the exact Coach Bags and other accessory you want at a low price that's right for you. My dear friends, let the coach outlet online pave your way into the world of high fashion with their ultimate fashion factory.
xujie123
2012-5-21 11:26
Our online store offers you discounted Designer louis vuitton replica wallet at present. You could find them in desirable quality and price. If you don't mind high class louis vuitton uk, have a good time here.louis vuitton Store Online Handbags can also bring great accuracy as well as practical applicability and fashionable.Have you ever dreamed of being as charming as Madonna? Have you ever thought of becoming an envy of all your friends? If so, come to louis vuitton outlet.<br/>
xujie555
2012-5-21 13:52
coach factory outlet uses graceful accessories to match the classical logo of coach, which is the best combination of coach. The handmade coach products?can make you more charming and graceful.coach factory online provides people many coach goods. If you wish to snatch the coach handbag, then this best method is made for that you like for coach discount.coach factory outlet online is a fashion brand to ensure its quality. With designer coach shoulder bags, you will always attract people's attention. The bags will emphasize your personal style and taste.<br/>
xujie333
2012-5-21 15:16
coach outlet online Store guarantee that all the coach handbags offered are own high quality. In addition , all of them are sold at an unexpected low price.If you want to purchase, just visit their website.coach factory outlet is really sizzling kinds of shopping way for you. With the usage of the replica designer coach bags, you can surely be able to change your individual looks in a stunning manner.Coach bags on sale from the coach outlet are cheap or discount prices that you certainly will stand out from the crowd on your next camping trip!<br/>
coach88888888 Email
2012-8-13 17:53
If you desire to go to Coach Factory Outlet, but have no idea in which to go, you can research online. It is no doubt that there is drastically information and details about it for the reference.Coach Factory Online are loved by many people, when you walk in the street, you could see many people take coach styles.hat experts claim Coach Outlet Online shopping is in the changes they are available in.Coach Outlet will send you a coupon in the post to use in their upon only after you type a achieve. The Coach bags are customarily somewhat many than the ones in the stores.
North Face Clearance Email Homepage
2012-11-8 08:39
modern and the superior outdoor North Face Pink Ribbon for you, buy now and save off. High quality, stylish and fantastic design, Cheap North Face Jackets For Women with full protection offers high performance fit for you. Valuable styling at low price, the North Face Clearance do the perfect to service you 100% satisfaction. Waterproof and breathable, zipped jackets allows increased ventilation
North Face Jackets On Sale Email Homepage
2012-11-9 14:30
North Face Jackets Clearance  To play ball movement of the athletes in sports wear. North Face Clearance Generally includes the upper part of the body of the short sleeve and para shorts. Material generally use has good permeability hydrophobic fabrics, will not affect the athletes' physical activity, not easy the creation electrostatic, light weight. Good shirt will help athletes improveperformance.North Face Jackets On Sale
goodsinchina Homepage
2012-11-17 18:05
我的网站就是按照你的思路进行架构的。10美金的VPS运行速度非常快http://www.goodsinchina.comhttp://www.goodsinchina.com
HAWER
2013-1-29 17:25
买轴承就到:www.jkzhoucheng.cn
junly Email
2013-3-25 10:08
請問一下,使用腳本和使用lvs+keepalive 有什么區別.另外誰的性能會好點
便宜点卡card.mmycw.com_殴飞数卡特卖频道_殴飞网上商城便宜
[2013-9-3 22:16]
来源:田状考克
内容:便宜点卡card.mmycw.com_殴飞数卡特卖频道_殴飞网上商城便宜...
提供手机话费充值,游戏点卡充值,公共事业缴费等,本站全部商品为欧飞官方货源,客服为欧飞官方7x24小时客服...游戏点卡充值,欧飞数卡,ofcard
分页: 3/11 第一页 上页 1 2 3 4 5 6 7 8 9 10 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]