1、准备

角色 外网ip 内网ip 安装工具
web 10.0.0.7 172.16.1.7 httpd、php
nfs 10.0.0.31 172.16.1.31 nfs-utils、lsyncd
backup 10.0.0.41 172.16.1.41 nfs-utils、rsync

2、web服务器搭建

搭建web服务器,部署项目,然后测试上传。

(1)安装软件
    [root@web ~]# yum install  -y httpd php

(2)创建用户
    [root@web ~]# groupadd -g 666 www
    [root@web ~]# useradd -u666 -g666 www

(3)修改httpd.conf的配置,将Apache进程运行用户修改为www用户
    [root@web ~]# sed -i '/^User/c User www' /etc/httpd/conf/httpd.conf
    [root@web ~]# sed -i '/^Group/c Group www' /etc/httpd/conf/httpd.conf

(4)准备代码(测试代码不需要关注,本文关注的是实时同步,只是为了架构完整而已。不搭建web,随便创建个目录来做实时同步也可以)
    [root@web ~]# unzip test.zip -d /var/www/html/

(5)启动httpd,关闭防火墙和selinux
    [root@web ~]# systemctl restart httpd
    [root@web ~]# systemctl enable httpd
    [root@web ~]# systemctl stop firewalld
    [root@web ~]# systemctl disable firewalld
    [root@web ~]# setenforce 0

(6)针对项目创建存放静态资源的文件夹
    [root@web ~]# mkdir /var/www/html/upload
    [root@web ~]# chown -R www.www /var/www/html/

3、NFS服务器搭建

安装配置NFS服务,将web静态资源存放的位置,挂载至NFS的共享目录中。

(1)两个节点都安装nfs
    [root@nfs ~]# yum install -y nfs-utils
    [root@web ~]# yum install -y nfs-utils

(2)创建用户
    [root@nfs ~]# groupadd -g 666 www
    [root@nfs ~]# useradd -u666 -g666 www

(3)配置nfs服务端
    [root@nfs ~]# vim /etc/exports
    /data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)

(4)创建对外共享的目录
    [root@nfs ~]# mkdir /data
    [root@nfs ~]# chown -R www.www /data

(5)启动NFS服务,关闭防火墙和selinux
    [root@nfs ~]# systemctl restart nfs rpcbind
    [root@nfs ~]# systemctl enable nfs rpcbind
    [root@web ~]# systemctl stop firewalld
    [root@web ~]# systemctl disable firewalld
    [root@web ~]# setenforce 0

(6) 在web节点上,测试nfs服务端是否正常,将web静态资源存放的位置,挂载至NFS的共享目录中。
    [root@web ~]# showmount -e 172.16.1.31
    [root@web ~]# mount -t nfs 172.16.1.31:/data /var/www/html/upload

4、backup服务器搭建

  为什么要搭建backup服务器, 因为NFS如果一旦故障,那么资源就会丢失形成单点故障,因此我们需要给 NFS 增加一个备用节点。我们要考虑以下几点:
    • backup服务器需要配置rsync服务端。
    • 需要实时监控NFS服务器的/var/www/html/upload目录,一旦发生变化,就马上同步到Backup服务器的/data中。这里采用lsyncd服务,也可以使用sersync服务,但是推荐使用lsyncd服务。
   • 假设NFS故障,如何快速将Backup顶用起来,需要Backup也做成一个NFS服务端,共享/data目录

(1)backup服务器配置rsync服务端

[root@backup ~]# yum install rsync -y
[root@backup ~]# vim /etc/rsyncd.conf
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log

[data]
path = /data

根据配置完成对应初始化操作

[root@backup ~]# groupadd -g 666 www
[root@backup ~]# useradd -u666 -g666 www
[root@backup ~]# mkdir /data
[root@backup ~]# chown -R www.www /data/
[root@backup ~]# echo "rsync_backup:000000" > /etc/rsync.passwd
[root@backup ~]# chmod 600 /etc/rsync.passwd
[root@backup ~]# systemctl restart rsyncd
[root@backup ~]# systemctl enable rsyncd
[root@backup ~]# systemctl stop firewalld
[root@backup ~]# systemctl disable firewalld
[root@backup ~]# setenforce 0

基础功能测试

[root@nfs ~]# rsync -avz 1.txt rsync_backup@172.16.1.41::data
Password: 
sending incremental file list
1.txt

sent 88 bytes  received 43 bytes  37.43 bytes/sec
total size is 0  speedup is 0.00

[root@backup data]# ll
total 0
-rw-r--r--. 1 www www 0 Aug 14 20:27 1.txt

(2)NFS服务器利用lsyncd服务实现实时监控同步

lsyncd服务配置文件详解入口

[root@nfs ~]# yum -y install -y lsyncd          //会同时依赖安装rsync
[root@nfs ~]# vim /etc/lsyncd.conf 
settings {
 logfile = "/var/log/lsyncd/lsyncd.log",
 statusFile = "/var/log/lsyncd/lsyncd.status",
 inotifyMode = "CloseWrite",
 maxProcesses = 8,
}
sync {
 default.rsync,
 source = "/data",
 target = "rsync_backup@172.16.1.41::data",
 delete= true,
 delay = 1,
rsync = {
 binary = "/usr/bin/rsync",
 archive = true,
 compress = true,
 verbose = true,
 password_file = "/etc/rsync.password",
}
}

[root@nfs ~]# echo "000000" > /etc/rsync.password
[root@nfs ~]# chmod 600 /etc/rsync.password
[root@nfs ~]# systemctl restart lsyncd
[root@nfs ~]# systemctl enable lsyncd

实时同步测试,此时我们可以看到,nfs服务器上data目录的数据同步到了backup服务器的data目录上了。我们在网页上创建一个名为test的文件来测试是否实时同步。

[root@web ~]# ll /var/www/html/upload
total 0
[root@web ~]# touch /var/www/html/upload/test.txt

[root@nfs ~]# ll /data/
total 0
-rw-r--r--. 1 www www 0 Aug 14 21:23 test.txt

[root@backup ~]# ll /data/
total 0
-rw-r--r--. 1 www www 0 Aug 14 21:23 test.txt

(3)backup服务器配置nfs服务

[root@backup ~]# yum install -y nfs-utils 
[root@backup ~]# vim /etc/exports
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
[root@backup ~]# systemctl restart nfs rpcbind
[root@backup ~]# systemctl enable nfs rpcbind

[root@web ~]# showmount -e 172.16.1.41
Export list for 172.16.1.41:
/data 172.16.1.0/24

发表评论

验证码: 41 + = 42