服务器大型集群部署文件同步方案

下面给出ecstore集群部署文件同步方案示例(三选一)

一.rsync+lsyncd文件同步

1. 目录与内网地址


web1:192.168.1.1  dir=/data/www/ecstore
web2:192.168.1.2  dir=/data/www/ecstore

安装服务:lsyncd阿里云yum源epel有该服务,没有的话需要安装(yum -y install epel-release)。


yum -y install rsync lsyncd xinetd

chkconfig rsync on

chkconfig lsyncd on

chkconfig xinetd on

2.rsync配置
web1:


log file = /data/logs/sync/rsyncd.log

pid file = /data/logs/sync/rsyncd.pid

lock file = /var/run/rsync.lock

use chroot = yes

[www]

path = /data/www/ecstore hosts allow = 192.168.1.2 uid = www gid = www read only = false

web2:


log file = /data/logs/sync/rsyncd.log

pid file = /data/logs/sync/rrsyncd.pid

lock file = /var/run/rsync.lock

use chroot = yes

[www]

path = /data/www/ecstore hosts allow = 192.168.1.1 uid = www gid = www read only = false

3.lsync配置

web1:


settings = {
logfile = "/data/logs/sync/lsyncd.log",
statusFile = "/data/logs/sync/lsyncd.stat",
statusInterval =1,
}
sync{
default.rsync,
source="/data/www/ecstore",
target="192.168.1.2::wwroot/",
excludeFrom = "/etc/lsyncd.exclude",
init=false,
rsync     = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
verbose   = true
}
}

web2:


settings = {
logfile = "/data/logs/sync/lsyncd.log",
statusFile = "/data/logs/sync/lsyncd.stat",
statusInterval =1,
}
sync{
default.rsync,
source="/data/www/ecstore",
target="192.168.1.1::www/",
excludeFrom = "/etc/lsyncd.exclude",
init=false,
rsync     = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
verbose   = true
}
}

重启服务:


service xinetd restart

service lsyncd restart

测试:

rsync -v rsync://192.168.1.1

二.nfs文件共享方案

安装nfs服务 并启用
yum -y install nfs-utils rpcbind
配置开机启动


chkconfig nfs on

chkconfig rpcbind on

启动nfs


service rpcbind start(先启动,再启动nfs,有依赖关系)

service nfs start

配置nfs信息vim /etc/exports写入除网站data以外的目录(注意配置,尽量写内网ip,如:192.168.1.1,不要写127.0.0.1,否则不要同步config目录)注意:下例子是ERP的配置,不同版本系列可能目录不同,注意规避细节,501和502是www用户的gid和uid不同系统不同


/data/www/app           *(rw,sync,all_squash,anonuid=501,anongid=502)

/data/www/public        *(rw,sync,all_squash,anonuid=501,anongid=502)

/data/www/custom        *(rw,sync,all_squash,anonuid=501,anongid=502)

/data/www/upgrade       *(rw,sync,all_squash,anonuid=501,anongid=502)

/data/www/config        *(rw,sync,all_squash,anonuid=501,anongid=502)

/data/www/crontab       *(rw,sync,all_squash,anonuid=501,anongid=502)

/data/www/script        *(rw,sync,all_squash,anonuid=501,anongid=502)

/data/www/tools         *(rw,sync,all_squash,anonuid=501,anongid=502)

查看挂载NFS用另外一台机器(web2:192.168.1.2)
查看挂载信息showmount -e 192.168.1.1
blob.png
如果能显示以上信息,说明NFS配置是没问题的
复制web1的网站目录
tar -zcvf web.tar.gz /data/www/web1(最好打包)scp web.tar.gz 192.168.1.2:/data/www
解压网站文件删除 以上目录里面的信息
rm -rf /data/www/app/* /data/www/public/* /data/www/custom/* /data/www/upgrade/* /data/www/config/* /data/www/crontab/* /data/www/script/* /data/www/tools/*
(删除操作,请仔细核对,不要复制粘贴,删除目录等同挂载目录,留下空文件夹)
挂载远端nfs程序
编辑/etc/fstab,设置开机自动挂载


192.168.1.1:/data/www/tools   /data/www/tools nfs     defaults        0 0

192.168.1.1:/data/www/script  /data/www/script nfs    defaults        0 0

192.168.1.1:/data/www/crontab /data/www/crontab nfs   defaults        0 0

192.168.1.1:/data/www/config  /data/www/config  nfs   defaults        0 0

192.168.1.1:/data/www/upgrade /data/www/upgrade nfs   defaults        0 0

192.168.1.1:/data/www/custom  /data/www/custom  nfs   defaults        0 0

192.168.1.1:/data/www/public  /data/www/public  nfs   defaults        0 0

192.168.1.1:/data/www/app     /data/www/app   nfs     defaults        0 0

先进web文件夹看下是否有信息残留,

服务器大型集群部署文件同步方案-搬砖酱的笔记

没有的话,执行挂载

mount -a(如果没报错,就说明成功了,有报错请baidu查找NFS官方文档进行配置)

再次查阅app目录

服务器大型集群部署文件同步方案-搬砖酱的笔记

touch 个文件试试,如果正常写入,则表示成功

三.rsync同步方案(该方案需要网站后台锁定到其中一台服务器,适合2台以上集群)

1.多台web 都要配置如下:


yum -y install xinetd

chkconfig xinetd on

chkconfig rsync on

/etc/init.d/xinetd start

查看是否开机启动


chkconfig --list|grep rsync

chkconfig --list|grep xinetd
服务器大型集群部署文件同步方案-搬砖酱的笔记

vim /etc/rsync.conf


log file = /data/logs/sync/rsyncd/rsyncd.log(需要有相应目录)

pid file = /data/logs/sync/rsyncd/rsyncd.pid

lock file = /var/run/rsync.lock

use chroot = yes

[www]

path = /data/www/ecstore hosts allow = 10.0.1.0/24(内网网段,可以只允许主服务器) comment = update ignore errors uid = www gid = www read only = false

重启服务
/etc/init.d/xinetd restart

2.其他机器不需要网站目录程序,创建个/data目录,直接把整个网站推送过去


rsync -vzrtopg --numeric-ids --delete --exclude=data/logs --exclude=data/cache --progress  /data/www/ecstore/public www@10.0.1.2::www

rsync -vzrtopg --numeric-ids --delete --exclude=data/logs --exclude=data/cache --progress  /data/www/ecstore/public www@10.0.1.3::www

如果需要保持数据一致,需要定时推送,推送服务器IP为10.0.0.1,公网IP:x.x.x.x

重新执行下该命令,估算下时间,然后创建计划任务,计划任务执行时间尽量大于执行的时间

3.登录后台操作的,配置hosts文件,域名为绑定激活的域名

windows下:

C:\Windows\System32\drivers\etc\hosts

x.x.x.x www.ceshi.com

mac:

sudo vim /etc/hosts

x.x.x.x www.ceshi.com

该方案必须锁定,必须执行第3步

注:文件同步之后,还需要kvstore,cache,以及数据库同步,config.php有相关配置

相关缓存同步:

点我我是redis缓存同步链接

点我我是mecached缓存同步链接

点我我是mongodb缓存同步链接