【来自技术部】

通过前端lvs+keepalived 实现负载和高可用

后端通过lnmp 实现web 集群两天web 之间使用mysql主从同步数据库

使用sersync实现网站文件的实时更新

lvs+keepalived master 192.168.10.9 eth0

lvs+keepalived backup 192.168.10.10 eth0

lnmp1 192.168.10.11 eth0 mysql 从

lnmp2 192.168.10.12 eth0 msyql 主

lnmp1 rsync客户端

lnmp2 rsync 服务端

通过客户端向服务端单向推送更新

 

安装所需的依赖关系

yum install gcc gcc-c++ openssl-devel kernel-devel -y
连接内核文件
ln -sv /usr/src/kernel/2.6.18-238.e15-i686/ /usr/src/linux
tar -zxvf keepavlived-1.1.17.tar.gz -C /usr/src/
cd /usr/src/keepalived-1.1.17
./configure
make && make install

复制keepalived 命令到本地默认程序目录 创建keepalived 配置文件目录

cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/sbin/keepalived /usr/sbin/
mkdir /etc/keepalived

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

 
global_defs {
 
   notification_email {
 
       
 
   }
 
   notification_email_from
 
   smtp_server 127.0.0.1
 
   router_id LVS_DEVEL
 
}
 
vrrp_instance VI_1 {
 
    state MASTER (backup 服务器上 修改为 BACKUP)
 
    interface eth0
 
    virtual_router_id 51
 
    priority 100 (backup 服务器上的优先级 要低于 master 的服务器上的优先级)
 
    advert_int 1
 
    authentication {
 
        auth_type PASS
 
        auth_pass 1111
 
    }
 
    virtual_ipaddress {
 
        192.168.1.100
 
    }
 
}
 
virtual_server 192.168.1.100 80 {
 
    delay_loop 6
 
    lb_algo rr
 
    lb_kind DR
 
    persistence_timeout 50
 
    protocol TCP
 

    real_server 192.168.1.3 80 {
 
        weight 3
 
        TCP_CHECK {
 
        connect_timeout 10
 
        nb_get_retry 3
 
        delay_before_retry 3
 
        connect_port 80
 
        }
 
    }
 
    real_server 192.168.1.4 80 {
 
        weight 3
 
        TCP_CHECK {
 
        connect_timeout 10
 
        nb_get_retry 3
 
        delay_before_retry 3
 
        connect_port 80
 
        }
 
    }
 
}

 

【】

在backup 服务器上的操作和 master 服务器上的操作一样
只需要把 keepalived.conf 配置文件做部分修改就可以了

service keepalived start 启动 keepalived 服务

在 后端web 集群服务其上执行 下面脚本

#!/bin/bash

vip=192.168.1.110
/etc/rc.d/init.d/functions
case "$1" in
start)
/sbin/ifconfig lo:0 $vip netmask 255.255.255.255 broadcast $vip
/sbin/route add -host $vip dev lo:0
echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p > /dev/null 2>&1
echo "RealServer Start OK"
;;
stop)
/sbin/ifconfig lo:0 down
/sbin/route del $vip > /dev/null 2>&1
echo "0" > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" > /proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" > /proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" > /proc/sys/net/ipv4/conf/all/arp_announce
echo "RealServer Stoped"
;;
*)
echo "Usage: $0{start|stop}"
exit 1
esac
exit 0

 

 

 【】

 

 

 

在后端服务上搭建 mysql 主从实现mysql服务器的备份和缓解mysql服务器的读写压力

在主mysql服务器上修改 mysql 的主配置文件/etc/my.cnf

log-bin=mysql-bin
server-id  =  1

mysql -u root -p123456

进入mysql服务

为从客户机创建 backup 用户 和密码 backup 为从服务器授权

grant replication slave on *.* to identified by 'backup';

show master status;

查看 主数据库的状态和 FILE 及 Position的值

在从服务其上/etc/my.cnf
log-bin=mysql-bin
server-id   = 2

mysql -u root -p123456

change master to master_host='192.168.10.12',master_user='backup',master_password='backup',master_log_file='mysql-bin.000003',master_log_pos=401;

start slave;
开始从服务器

show slave status\G;

查看服务状态
确保
slave_IO_Running: yes
slave_SQL_Running: yes

这两项 是 yes

 

 

在rsync 服务端
rpm -qa | grep rsync
确认 rsync 服务安装
创建 vim/etc/rsyncd.conf 主配置文件

 uid = root

    gid = root
    use chroot = no
    max connections = 5
    pid file = /var/run/rsyncd.pid
    lock file = /var/run/rsync.lock
    log file = /var/log/rsyncd.log
 
   [http]
    path=/home/wwwroot/
    exclude = wap/
    comment = backup
    ignore errors
    read only = no
    list = no
 
    #hosts allow = 192.168.10.0/255.255.255.0
    auth users = rsync
    secrets file = /etc/rsyncd.secrets

修改/etc/rsyncd.secrests de 权限

chmod 600 /etc/rsyncd.secrests
echo "rsync:123456" >> /etc/rsyncd.secrests

rsync --daemon 启动 rsync 服务

netstat -atplun | grep rsync
查看873 端口

在rsync 客户端安装sersync 软件包 实现客户端实时想服务端单向推送服务

tar -zxvf sersync2.5beta1_32bit_binary.tar.gz -C /usr/src/

cd /usr/src/GNU_Linux-x86

修改confxml.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.4">
    <host hostip="192.168.10.11" port="8008"></host>  {本机Ip}
    <filter start="false">
 <exclude expression="(.*)\.gz"></exclude>
 <exclude expression="^info/*"></exclude>
    </filter>
    <inotify>
 <delete start="true"/>
    </inotify>
    <sersync>
 <localpath watch="/home/wwwroot" debug="false">
     <remote ip="192.168.10.12" name="http"/>  远程ip 和rsync服务确定的模块
     <!--<remote ip="192.168.8.39" name="tongbu"/>-->
     <!--<remote ip="192.168.8.40" name="tongbu"/>-->
 </localpath>
 <rsync>
     <auth start="true" users="rsync" passwordfile="/etc/rsync.pas"/> 指定rsync同步的账户 和 密码认证文件 /etc/rsync.pas
  </rsync>
 <crontab start="false" schedule="600">
     <crontabfilter start="false">
  <exclude expression="*.php"></exclude>
  <exclude expression="info/*"></exclude>
     </crontabfilter>
 </crontab>
 <plugin start="false" name="refreshCDN"/>
    </sersync>

    <plugin name="refreshCDN">
 <localpath watch="/data0/htdocs/cms.xoyo.com/site/">
     <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
     <sendurl base=">
     <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/p_w_picpaths"/>
 </localpath>
    </plugin>
    <plugin name="socket">
 <localpath watch="/opt/tongbu">
     <deshost ip="192.168.138.20" port="8009"/>
 </localpath>
    </plugin>
</head>

 权限 chmod 600 /etc/rsync.pas  echo "123456" >> /etc/rsync.pas 只输入密码

 

执行

./sersync2 -d -r