NFS实现LAMP分离多服务器同步更新服务

  1. 云栖社区>
  2. 博客>
  3. 正文

NFS实现LAMP分离多服务器同步更新服务

科技小先锋 2017-11-14 19:27:00 浏览943
展开阅读全文

上一篇Linux下编译安装LAMP并分离为多台服务器写的是LAMP分离为多台主机;但是由于是各自独立;如需要更新或用户更新数据时;需要每台机器单独更新;且如httpd服务器等都是需要多台的话就会出现数据不同步的现象;现在就上一篇进行补全;实现多台httpd主机利用NFS文件系统基于RPC协议来同步更新数据。

大致规划:

网段:172.16.251.

1台DNS服务器(172.16.251.84)

2台httpd服务器(172.16.251.85/86)

1台php服务器(172.16.251.87)

1台mysql服务器(172.16.251.88)

做了四个域名解析到2台httpd服务器上:

域名:www.myadmin.cn www.soul.org www.pipi.com www.dark.net

wKioL1MygArxgza-AAFb22NJtc0777.jpg




一、NFS和RPC定义

网络文件系统Network File System,NFS),一种使用于分散式文件系统的协议,SUN公司开发,于1984年向外公布。功能是通过网络让不同的机器、不同的操作系统能够彼此分享个别的数据,让应用程序在客户端通过网络访问位于服务器磁盘中的数据,是在Unix系统间实现磁盘文件共享的一种方法。NFS的基本原则是“容许不同的客户端服务端通过一组RPC分享相同的文件系统”,它是独立于操作系统,容许不同硬件及操作系统的系统共同进行文件的分享。目前最新版本为NFSv4。

远程过程调用Remote Procedure CallRPC)是一个计算机通信协议。该协议允许运行于一台计算机的程序调用另一台计算机的子程序,而程序员无需额外地为这个交互作用编程。


二、安装配置NFS服务

NFS是由nfs-utils软件包提供的;可以先查看系统上是否安装;如未安装可以先使用yum来安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
[Linux87]#rpm -qa nfs*
nfs-utils-lib-1.1.5-6.el6.x86_64
nfs-utils-1.2.3-39.el6.x86_64
nfs4-acl-tools-0.3.3-6.el6.x86_64
[Linux87]#rpm -ql nfs-utils  查看该包安装生成了哪些文件
/etc/nfsmount.conf
/etc/rc.d/init.d/nfs
/etc/rc.d/init.d/nfslock
/etc/rc.d/init.d/rpcgssd
/etc/rc.d/init.d/rpcidmapd
/etc/rc.d/init.d/rpcsvcgssd
/etc/request-key.d/id_resolver.conf
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
安装成功后既可启动:
[Linux87]#service nfs start
Starting NFS services:                                     [  OK  ]
Starting NFS quotas:                                       [  OK  ]
Starting NFS mountd:                                       [  OK  ]
Starting NFS daemon:                                       [  OK  ]
Starting RPC idmapd:                                       [  OK  ]
#默认就启动上述这么多服务
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
[Linux87]#rpcinfo   可以查看详细信息
   program version netid     address                service    owner
    100000    4    tcp       0.0.0.0.0.111          portmapper superuser
    100000    3    tcp       0.0.0.0.0.111          portmapper superuser
    100000    2    tcp       0.0.0.0.0.111          portmapper superuser
    100000    4    udp       0.0.0.0.0.111          portmapper superuser
    100000    3    udp       0.0.0.0.0.111          portmapper superuser
    100000    2    udp       0.0.0.0.0.111          portmapper superuser
    100000    4    local     /var/run/rpcbind.sock  portmapper superuser
    100000    3    local     /var/run/rpcbind.sock  portmapper superuser
    100024    1    udp       0.0.0.0.230.98         status     29
    100024    1    tcp       0.0.0.0.144.225        status     29
    100011    1    udp       0.0.0.0.3.107          rquotad    superuser
    100011    2    udp       0.0.0.0.3.107          rquotad    superuser
    100011    1    tcp       0.0.0.0.3.107          rquotad    superuser
    100011    2    tcp       0.0.0.0.3.107          rquotad    superuser


2、配置NFS共享文件系统

需要共享的是PHP的服务器;共享给指定的两台httpd主机;所以可以直接指定IP地址;这里单独建立一个分区用于NFS;挂载至web配置文件中指定的DocumentRoot目录;当然也可以先挂载在配置;这里是由于之前已配置好服务;所以直接挂载文件为指定目录:

httpd配置中的路径:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[WEB85]#cat /etc/httpd24/extra/httpd-vhosts.conf
#
<VirtualHost *:80>
    ServerAdmin  admin@pipi.com
    DocumentRoot "/usr/local/apache/www/pipi" #本地路径
    ServerName www.pipi.com
    ServerAlias pipi.com
    ErrorLog "/usr/local/apache/logs/pipi.error_log"
    CustomLog "/usr/local/apache/logs/pipi.access_log" combined
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://172.16.251.87:9000/var/www/pipi/$1
                                         
#上面这个路径就是PHP服务器的存放路径;也就是需要共享的NFS路径:/var/www
                                               
    <Directory "/usr/local/apache/www/pipi">
        DirectoryIndex index.html index.php
        Options none
        Require all granted
    </Directory>
</VirtualHost>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
[PHP87]#mount
/dev/mapper/vg0-root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
/dev/mapper/vg0-usr on /usr type ext4 (rw)
/dev/mapper/vg0-var on /var type ext4 (rw)
/dev/sda3 on /var/www type ext4 (rw,acl)  #这个目录
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
nfsd on /proc/fs/nfsd type nfsd (rw)
#****注意:挂载时在fstab中写入需要acl属性****
                                                                                                                                                                                                                                                                                         
#配置NFS主要是修改/etc/exports文件
[PHP87]#vim /etc/exports
#文件格式很简单:
文件系统    客户端1(文件系统导出属性1...)  客户端2()
                                      
/var/www        172.16.251.85(rw)               172.16.251.86(rw)
                                      
#rw:读写
#async:异步
#sync:同步
#root_squash:压缩root用户;基于imapd;将root通过网络访问时转为#nfsnobody用户
#no_root_squash:不压缩root权限
#all_squash:压缩所有用户
#anonuid=[num]:指定匿名用户映射为的UID和GID
#anongid=[num]
修改完成后可以查看属性
                                      
[PHP87]#exportfs -v
/var/www        172.16.251.85(rw,wdelay,root_squash,no_subtree_check)
/var/www        172.16.251.86(rw,wdelay,root_squash,no_subtree_check)
[PHP87]#其中有很多默认属性的
现在可以去httpd服务器查看该机器的共享NFS
                                      
[WEB85]#showmount -e 172.16.251.87
Export list for 172.16.251.87:
/var/www 172.16.251.86,172.16.251.85
[WEB85]#
[WEB86]#showmount -e 172.16.251.87
Export list for 172.16.251.87:
/var/www 172.16.251.86,172.16.251.85
[WEB86]#


3、挂载配置读写权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
mount -type device dir
    -t:指定文件系统类型
[WEB85]#mount -t nfs 172.16.251.87:/var/www/ /usr/local/apache/www/
[WEB85]#mount
/dev/mapper/vg0-root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
/dev/mapper/vg0-usr on /usr type ext4 (rw)
/dev/mapper/vg0-var on /var type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
172.16.251.87:/var/www/ on /usr/local/apache/www type nfs (rw,vers=4,addr=172.16.251.87,clientaddr=172.16.251.85)
#查看以挂载;第二台httpd也需要同样挂载;挂载完成后记得写入到/etc/fstab中
                                                                                                                                                                                                                                                                       
[WEB85]#vim /etc/fstab
增加下面这行内容
172.16.251.87:/var/www  /usr/local/apache/www   nfs     defaults,_netdev        0 0
                                                                                                                                                                                                                                                             
挂载完成后;是无法写入数据的
[WEB85]#cd /usr/local/apache/www/
[WEB85]#mkdir aa
mkdir: cannot create directory `aa': Permission denied
[WEB85]#
                                                                                                                                                                                                                                                             
需要到php服务器配置NFS的权限;为了安全;新建立一个账户;httpd服务器也建立同样的账户
[PHP87]#useradd -u 600 web
[PHP87]#id web
uid=600(web) gid=600(web) groups=600(web)
[PHP87]#setfacl -R -m u:600:rwx /var/www/  -R是因为其下面有其他的目录
[PHP87]#getfacl /var/www/
getfacl: Removing leading '/' from absolute path names
# file: var/www/
# owner: root
# group: root
user::rwx
user:web:rwx
group::r-x
mask::rwx
other::r-x
                                                                                                                                                                                                                                                          
#httpd两台服务器建立相应的账户
[WEB85]#useradd -u 600 web
[WEB85]#su - web
[web@localhost ~]$ cd /usr/local/apache/www/
[web@localhost www]$ mkdir aaa
[web@localhost www]$ ll
total 52
drwxrwxr-x  2 nobody nobody  4096 Mar 26 16:09 aaa
#测试创建成功


三、配置DNS服务

1、域名系统英文Domain Name System,DNS)是因特网的一项服务。它作为将域名IP地址相互映射的一个分布式数据库,能够使人更方便的访问互联网。DNS 使用TCPUDP端口53。目前最常用的软件为BIND;先查看是否安装;如未安装直接yum安装即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[DNS84]#rpm -qa bind*
bind-devel-9.8.2-0.23.rc1.el6_5.1.x86_64
bind-libs-9.8.2-0.23.rc1.el6_5.1.x86_64
bind-9.8.2-0.23.rc1.el6_5.1.x86_64
bind-utils-9.8.2-0.23.rc1.el6_5.1.x86_64
[DNS84]#
查看具体安装了哪些文件
[DNS84]#rpm -ql bind
/etc/NetworkManager/dispatcher.d/13-named
/etc/logrotate.d/named
/etc/named
/etc/named.conf
/etc/named.iscdlv.key
/etc/named.rfc1912.zones
/etc/named.root.key
/etc/portreserve/named
/etc/rc.d/init.d/named
/etc/rndc.conf
/etc/rndc.key
/etc/sysconfig/named
/usr/lib64/bind
/usr/sbin/arpaname


2、配置域名解析至web服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
[DNS84]#vim /etc/named.conf
这是主配置文件;只需要注释掉其中部分内容即可;注意//为注释符
// Provided by Red Hat bind package to configure the ISC BIND named(8) DNS
// server as a caching only nameserver (as a localhost DNS resolver only).
//
// See /usr/share/doc/bind*/sample/ for example named configuration files.
//
options {
//      listen-on port 53 127.0.0.1; }; #注释
//      listen-on-v6 port 53 { ::1; };    #注释
        directory       "/var/named";
        dump-file       "/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
//      allow-query     { localhost; };  #注释
        recursion yes;
//      dnssec-enable yes;                #注释
//      dnssec-validation yes;            #注释
//      dnssec-lookaside auto;            #注释
        /* Path to ISC DLV key */
//      bindkeys-file "/etc/named.iscdlv.key";    #注释
//      managed-keys-directory "/var/named/dynamic";    #注释
};
logging {
        channel default_debug {
                file "data/named.run";
                severity dynamic;
        };
};
zone "." IN {
        type hint;
        file "named.ca";
};
include "/etc/named.rfc1912.zones";    注意需要去这个文件内修改
include "/etc/named.root.key";
                                                                                                                                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                                                                                                                  
[DNS84]#vim /etc/named.rfc1912.zones
#添加如下几个区域文件;都是对应的域名
zone "soul.org." IN {
        type master;      #类型 主
        file "soul.org.zone"#文件位置;相对主配置文件中directory的相对路径
};
zone "myadmin.cn." IN {
        type master;
        file "myadmin.com.zone";
};
zone "dark.net." IN {
        type master;
        file "dark.net.zone";
};
zone "pipi.com." IN {
        type master;
        file "pipi.com.zone";
};
                                                                                                                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                                                                                                            
上述内容添加好;文件还是没有的;配置文件中指定的目录创建这些文件;注意权限
[DNS84]#pwd
/var/named
[DNS84]#ll
total 44
-rw-r--r-- 1 root  root     0 Mar 26 13:38 a.out
-rw-r----- 1 root  named  227 Mar 26 15:04 dark.net.zone
drwxrwx--- 2 named named 4096 Mar 26 11:34 data
drwxrwx--- 2 named named 4096 Mar 26 14:12 dynamic
-rw-r----- 1 root  named  233 Mar 26 14:52 myadmin.cn.zone
-rw-r----- 1 root  named 1892 Feb 18  2008 named.ca
-rw-r----- 1 root  named  152 Dec 15  2009 named.empty
-rw-r----- 1 root  named  152 Jun 21  2007 named.localhost
-rw-r----- 1 root  named  168 Dec 15  2009 named.loopback
-rw-r----- 1 root  named  227 Mar 26 15:05 pipi.com.zone
drwxrwx--- 2 named named 4096 Jan 21 01:40 slaves
-rw-r----- 1 root  named  227 Mar 26 14:48 soul.org.zone
[DNS84]#
[DNS84]#vim soul.org.zone
#具体内容如下
$TTL 600
@       IN SOA  dns.soul.org.   admin.soul.org. (
                                        20140326000     ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        IN      NS      dns    #ns记录
dns     IN      A       172.16.251.84 #ns对应的A记录
www     IN      A       172.16.251.85 #解析到85的域名
www     IN      A       172.16.251.86 #解析到86的域名
#同理;其他几个文件也是这样设置的
配置完成后需要检查下语法
[DNS84]#named-check
named-checkconf  named-checkzone
为主配置文件和区域文件的检查命令
设置完成后把其他几台机器的DNS指向该DNS服务器的IP即可
        
        
记得把其他主机的DNS指向DNS服务器
[WEB85]#cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 172.16.251.84
其他几台也是一样的。
        
然后进行测试
[PHP87]#ping www.soul.org
PING www.soul.org (172.16.251.8556(84) bytes of data.
64 bytes from 172.16.251.85: icmp_seq=1 ttl=64 time=1.00 ms
64 bytes from 172.16.251.85: icmp_seq=2 ttl=64 time=0.591 ms
^C
--- www.soul.org ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1326ms
rtt min/avg/max/mdev = 0.591/0.797/1.004/0.208 ms
[PHP87]#ping www.soul.org
PING www.soul.org (172.16.251.8656(84) bytes of data.
64 bytes from 172.16.251.86: icmp_seq=1 ttl=64 time=0.712 ms
64 bytes from 172.16.251.86: icmp_seq=2 ttl=64 time=0.460 ms
64 bytes from 172.16.251.86: icmp_seq=3 ttl=64 time=0.486 ms

完成后记得添加一个windows的DNS地址为配置的DNS服务器IP地址。


四、测试配置一个discuz论坛和phpmyadmin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[web@86 myadmin]$ pwd
/usr/local/apache/www/myadmin
[web@86 myadmin]$ unzip phpMyAdmin-4.0.5-all-languages.zip
[web@86 myadmin]$ mv phpMyAdmin-4.0.5-all-languages/* ./
[web@86 myadmin]$ cp config.sample.inc.php config.inc.php
[web@86 myadmin]$ vi config.inc.php
/* Authentication type */
$cfg['Servers'][$i]['auth_type'= 'cookie';
/* Server parameters */
$cfg['Servers'][$i]['host'= '172.16.251.88';  #更改该项
$cfg['Servers'][$i]['connect_type'= 'tcp';
$cfg['Servers'][$i]['compress'= false;
/* Select mysql if your server does not have mysqli */
$cfg['Servers'][$i]['extension'= 'mysqli';
$cfg['Servers'][$i]['AllowNoPassword'= false;
[web@86 myadmin]
#完成后保存即可测试

wKioL1MylTOhynWcAAIt08MKEfE614.jpg

测试登陆成功。


2、下面使用httpd 85 机器安装discuz论坛

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[web@85 pipi]$
[web@85 pipi]$ ls
Discuz_X3.1_SC_UTF8.zip
[web@85 pipi]$ pwd
/usr/local/apache/www/pipi
[web@85 pipi]$ unzip Discuz_X3.1_SC_UTF8.zip
[web@85 pipi]$ chmod -777 upload/
[web@85 pipi]$ mv upload/* ./
到此以完成,可以直接在浏览器中访问安装了
也可以先去86上看下目录下的类容
[web@86 pipi]$ ls
Discuz_X3.1_SC_UTF8.zip  config           favicon.ico  install     readme      template     utility
admin.php                connect.php      forum.php    member.php  robots.txt  uc_client
api                      cp.php           group.php    misc.php    search.php  uc_server
api.php                  crossdomain.xml  home.php     plugin.php  source      upload
archiver                 data             index.php    portal.php  static      userapp.php
都是同时操作更新的。

wKiom1MymAeDQXi8AAOjV7Rq8BE485.jpg

wKiom1MymBnQy1PTAADlvCYFncY590.jpg

wKiom1MymCmwlrwCAADxE4aRw9U076.jpg

数据库安装时注意指定数据库主机的IP

wKiom1MymJ7AN8dnAAKIcO312ds209.jpg

数据库也是同步更新的。到此配置以完成。其他几个都可以相应的放入其他域名网站。



本文转自Mr_陈 51CTO博客,原文链接:http://blog.51cto.com/chenpipi/1384661,如需转载请自行联系原作者

网友评论

登录后评论
0/500
评论
科技小先锋
+ 关注