1. 程式人生 > >十四周一次課

十四周一次課

linux

十四周一次課

14.1NFS介紹

14.2NFS服務端安裝配置

14.3NFS配置選項

14.1NFS介紹

NFS介紹

  • NFS是Network File System的縮寫;這個文件系統是基於網路層面,通過網絡層面實現數據同步

  • NFS最早由Sun公司開發,分2,3,4三個版本,2和3由Sun起草開發,4.0開始Netapp公司參與並主導開發,目前最新為4.1版本——>4.1版本是2010年出來還沒更新過

  • NFS數據傳輸基於RPC協議,RPC為Remote Procedure Call的簡寫,意思為遠程過程調用

    • 服務端和客戶端通信,A機器和B機器之間不能直接通信,需要借助RPC協議來實現

    • 例子

      • 1.服務端和客戶端通信,A機器和B機器之間不能直接通信,需要借助RPC協議來實現

  • NFS應用場景是:A,B,C三臺機器上需要保證被訪問到的文件是一樣的,A共享數據出來,B和C分別去掛載A共享的數據目錄,從而B和C訪問到的數據和A上的一致

  • NFS架構

    技術分享圖片

    • 跑了一個網站,上面傳輸了很多圖片,用戶訪問一個圖片時,需要從A機器上去請求,但A機器負載高,為了分擔負載,就多弄了兩臺機器,B機器C機器同時提供服務;正常的話,需要到A機器上才能拿到數據,但是B機器和C機器做了負載均衡,分擔了相同的服務器,那麽用戶也有可能到B機器或者C機器上;那麽用戶請求到B機器上的時候,如何才能獲取到A機器上的數據呢;要麽把A機器的數據傳輸到B機器上,同時傳輸到C機器上,但是這個不能時時更新,(用戶上傳的數據是存放在A機器上,但用戶請求的時候數據是請求到B機器上)這樣A上的數據還沒到B上面去,就會導致用戶請求獲取的數據訪問不到,訪問為空,為404;那麽NFS服務就可以解決這個問題,將A機器的數據共享到B機器和C機器,通過NFS來實現。有NFS服務以後,上傳到A機器上的數據,B機器或C機器上就能馬上看到和調用。

    • 例子:

    • 總結,NFS就是實時同步

NFS原理圖

  • 服務端需要啟動一個NFS服務,服務端要想給客戶端提供服務,需要借助RPC協議,RPC協議是由rpcbind服務所實現的;在centos 5或者之前的版本叫portmap服務,centos6及之後的版本叫rpcbind服務,這兩個都是一個服務,最終實現了RPC協議的通信,NFS服務默認不會監聽任何端口(啟動服務,但不會監聽端口),最終監聽端口,實現TCP/IP通信的過程是由rpcbind服務產生的RPC協議實現的,RPC協議默認監聽的端口是111 端口;

  • 整個流程為:服務端的NFS服務監聽一個端口通過RPC協議監聽的端口,再去告訴客戶端RPC協議,然後NFS客戶端通過本機的RPC端口回傳數據信息到服務端NFS監聽的端口,最終實現通信

技術分享圖片

  • NFS服務需要借助RPC協議實現通信

14.2NFS服務端安裝配置


NFS服務端安裝配置目錄概要

  • yum install -y nfs-utils rpcbind

  • vim /etc/exports //加入如下內容

/home/nfstestdir 192.168.133.1/24(rw,sync,all_squash,anonuid=1000,anongid=1000)

  • 保存配置文件後,執行如下操作

  • mkdir /home/nfstestdir

  • chmod 777 /home/nfstestdir

  • systemctl start rpcbind

  • systemctl start nfs

  • systemctl enable rpcbind

  • systemctl enable nfs

NFS服務端安裝配置

1.首先準備兩臺機器,一個作為服務端,一個作為客戶端我這裏準備兩臺虛擬機,A機器IP分別為192.168.11.136,B機器IP為192.168.11.138

  • A機器,查看IP

[root@tianqi-01 ~]# ifconfig

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

inet 192.168.11.136 netmask 255.255.255.0 broadcast 192.168.11.255

inet6 fe80::1eb9:8f9e:264a:7159 prefixlen 64 scopeid 0x20<link>

ether 00:0c:29:08:64:43 txqueuelen 1000 (Ethernet)

RX packets 526 bytes 51443 (50.2 KiB)

RX errors 0 dropped 0 overruns 0 frame 0

TX packets 76 bytes 8818 (8.6 KiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0


ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

inet 192.168.11.139 netmask 255.255.255.0 broadcast 192.168.11.255

ether 00:0c:29:08:64:43 txqueuelen 1000 (Ethernet)


ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

inet 192.168.233.130 netmask 255.255.255.0 broadcast 192.168.233.255

inet6 fe80::8834:1ebf:d84b:7dc9 prefixlen 64 scopeid 0x20<link>

ether 00:0c:29:08:64:4d txqueuelen 1000 (Ethernet)

RX packets 464 bytes 47165 (46.0 KiB)

RX errors 0 dropped 0 overruns 0 frame 0

TX packets 768 bytes 133542 (130.4 KiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0


lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536

inet 127.0.0.1 netmask 255.0.0.0

inet6 ::1 prefixlen 128 scopeid 0x10<host>

loop txqueuelen 1 (Local Loopback)

RX packets 16 bytes 1424 (1.3 KiB)

RX errors 0 dropped 0 overruns 0 frame 0

TX packets 16 bytes 1424 (1.3 KiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0


[root@tianqi-01 ~]#

  • B機器,查看IP

[root@tianqi-01 ~]# ifconfig

eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

inet 192.168.11.138 netmask 255.255.255.0 broadcast 192.168.11.255

inet6 fe80::20c:29ff:feaa:97e3 prefixlen 64 scopeid 0x20<link>

ether 00:0c:29:aa:97:e3 txqueuelen 1000 (Ethernet)

RX packets 743172 bytes 1002507382 (956.0 MiB)

RX errors 0 dropped 0 overruns 0 frame 0

TX packets 262681 bytes 20781578 (19.8 MiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0


lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536

inet 127.0.0.1 netmask 255.0.0.0

inet6 ::1 prefixlen 128 scopeid 0x10<host>

loop txqueuelen 0 (Local Loopback)

RX packets 36 bytes 1968 (1.9 KiB)

RX errors 0 dropped 0 overruns 0 frame 0

TX packets 36 bytes 1968 (1.9 KiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0


[root@tianqi-01 ~]#


2.將A機器作為服務端,並安裝兩個包,分別為 nfs-utils 和 rpcbind

  • yum install -y nfs-utils rpcbind

[root@tianqi-01 ~]# yum install -y nfs-utils rpcbind

3.將B機器作為客戶端,安裝 nfs-utils 包

  • 每次剛開機運行yum的時候都會很慢,因為需要重新生成一個緩存文件

  • 有時yum安裝的時候很慢,我們可以先禁掉,進入到/etc/yum.repos.d/目錄下,將目錄下的epel.repo文件改個名字,重新安裝即可

  • yum install -y nfs-utils

[root@tianqi-01 ~]# yum install -y nfs-utils

4.在安裝完成後,需要去A機器中 /etc/exports 編輯配置文件

  • vim /etc/exports //加入如下內容

[root@tianqi-01 ~]# vim /etc/exports

/home/nfstestdir 192.168.11.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)

5.下面就可以啟動服務了,啟動服務前需要先創建分享的目錄,並設置權限

6.在A機器上創建分享的目錄

[root@tianqi-01 ~]# mkdir /home/nfstestdir

[root@tianqi-01 ~]#

7.並設置成777的權限,這裏設置777權限是為了方便接下裏的實驗

[root@tianqi-01 ~]# chmod 777 !$

chmod 777 /home/nfstestdir

[root@tianqi-01 ~]#

8.啟動 rpcbind ,再啟動前查看A機器上監聽的端口,就會看到啟動了1/systemd,這是centos7系統的一個特性——>111端口是rpcbind服務監聽的

  • 用ps可以查看服務已經啟動,因為有這個服務,所以監聽了111端口

[root@tianqi-01 ~]# netstat -lntp

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name

tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 802/nginx: master p

tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 788/sshd

tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 890/master

tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 802/nginx: master p

tcp6 0 0 :::111 :::* LISTEN 1/systemd

tcp6 0 0 :::22 :::* LISTEN 788/sshd

tcp6 0 0 ::1:25 :::* LISTEN 890/master

tcp6 0 0 :::3306 :::* LISTEN 1037/mysqld

[root@tianqi-01 ~]# ps aux |grep rpc

root 378 0.0 0.0 0 0 ? S< 12:38 0:00 [rpciod]

root 1324 0.0 0.0 112660 980 pts/0 R+ 19:38 0:00 grep --color=auto rpc

[root@tianqi-01 ~]#

9.再到B機器上查看下端口,會看到也啟動了rpcbind

[root@tianqi-01 ~]# netstat -lntp

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name

tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2420/master

tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd

tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1275/sshd

tcp6 0 0 ::1:25 :::* LISTEN 2420/master

tcp6 0 0 :::111 :::* LISTEN 1/systemd

tcp6 0 0 :::22 :::* LISTEN 1275/sshd

[root@tianqi-01 ~]# ps aux |grep rpc

root 378 0.0 0.0 0 0 ? S< 12:38 0:00 [rpciod]

root 1324 0.0 0.0 112660 980 pts/0 R+ 19:38 0:00 grep --color=auto rpc

[root@tianqi-01 ~]#

10.在A機器啟動了rpcbind,下面就可以啟動nfs服務,在使用ps查看

  • systemctl start nfs

[root@tianqi-01 ~]# systemctl start nfs

[root@tianqi-01 ~]# ps aux |grep nfs

root 1352 0.0 0.0 0 0 ? S< 19:40 0:00 [nfsd4_callbacks]

root 1358 0.0 0.0 0 0 ? S 19:40 0:00 [nfsd]

root 1359 0.0 0.0 0 0 ? S 19:40 0:00 [nfsd]

root 1360 0.0 0.0 0 0 ? S 19:40 0:00 [nfsd]

root 1361 0.0 0.0 0 0 ? S 19:40 0:00 [nfsd]

root 1362 0.0 0.0 0 0 ? S 19:40 0:00 [nfsd]

root 1363 0.0 0.0 0 0 ? S 19:40 0:00 [nfsd]

root 1364 0.0 0.0 0 0 ? S 19:40 0:00 [nfsd]

root 1365 0.0 0.0 0 0 ? S 19:40 0:00 [nfsd]

root 1369 0.0 0.0 112660 980 pts/0 R+ 19:40 0:00 grep --color=auto nfs

[root@tianqi-01 ~]#

11.同時也可以在A機器上ps aux |grep rpc查詢,在啟動nfs服務的時候,它會自動幫你啟動rpc相關的一些服務

[root@tianqi-01 ~]# ps aux |grep rpc

root 378 0.0 0.0 0 0 ? S< 12:38 0:00 [rpciod]

rpcuser 1338 0.0 0.1 42376 1756 ? Ss 19:40 0:00 /usr/sbin/rpc.statd

rpc 1340 0.0 0.1 64956 1356 ? Ss 19:40 0:00 /sbin/rpcbind -w

root 1341 0.0 0.0 42564 948 ? Ss 19:40 0:00 /usr/sbin/rpc.mountd

root 1342 0.0 0.0 43816 540 ? Ss 19:40 0:00 /usr/sbin/rpc.idmapd

root 1371 0.0 0.0 112664 980 pts/0 R+ 19:41 0:00 grep --color=auto rpc

[root@tianqi-01 ~]#

12.而在客戶端B機器上是沒有的這些服務的

[root@tianqi-01 ~]# !ps

ps aux |grep rpc

root 535 0.0 0.0 0 0 ? S< 00:23 0:00 [rpciod]

rpc 2642 0.0 0.1 64896 1040 ? Ss 03:37 0:00 /sbin/rpcbind -w

root 2649 0.0 0.0 112660 976 pts/0 R+ 03:42 0:00 grep --color=auto rpc

[root@tianqi-01 ~]#

13.如果想讓nfs開機啟動,還需要執行systemctl enable nfs 命令,在服務端A機器上調用

  • systemctl enable nfs 開機啟動nfs

  • systemctl disable nfs 關閉開機啟動

[root@tianqi-01 ~]# systemctl enable nfs

Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.

[root@tianqi-01 ~]#

14.3NFS配置選項

NFS配置選項

  • rw 讀寫

  • ro 只讀

  • sync 同步模式,內存數據實時寫入磁盤,相應的就會降低磁盤效率

  • async 非同步模式,它會每隔一段時間才會將數據刷新到磁盤

    • 優勢:能夠保證磁盤的效率

    • 劣勢:萬一斷電,就會有可能丟失一部分數據

  • no_root_squash 客戶端掛載NFS共享目錄後,root用戶不受約束,權限很大

    • NFS,要想在客戶端上去使用服務端上共享的目錄,需要去把它掛載到客戶端上的一個掛載點,那就跟本地上的目錄是一樣的,在操作本地的目錄時候,肯定會有一些權限設置,如果加上no_root_squash,這樣root用戶去共享目錄下讀寫文件的時候,就不會受到限制(就相當於root用戶在本地上讀寫)

  • root_squash 與上面選項相對,客戶端上的root用戶受到約束,被限定成某個普通用戶

  • all_squash 客戶端上所有用戶(包括root用戶)在使用NFS共享目錄時都被限定為一個普通用戶

  • anonuid/anongid 和上面幾個選項搭配使用,定義被限定用戶的uid和gid

客戶端上掛載

1.首先在客戶端B機器上安裝了rpcbind,它是由nfs-utils 這個包自動安裝的

[root@tianqi-01 ~]# yum install -y nfs-utils

2.在B機器上安裝完成後不需要啟動任何服務

3.在B機器上執行 showmount -e 命令

  • showmount -e 192.168.11.136 //查看在NFS服務端是否有權限

    • 該ip為NFS服務端ip

[root@tianqi-01 ~]# showmount -e 192.168.11.136

clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)

[root@tianqi-01 ~]#

4.這時候會看到報錯了,RPC: Port mapper failure說明網絡不通,無法與192.168.11.136端口通信

  • 不能通信的兩種情況

    • 要麽是對方沒有開啟rpcbind的服務,沒有監聽111端口

    • 要麽是防火墻導致的原因

5.因為之前在服務端已經開啟了rpcbind服務,那這時只能說明是防火墻導致的,所以要想讓NFS實現正常的通信,還需要把防火墻關閉,因為NFS服務比較特殊,雖然rpc可以通信,就算iptables把111端口放行但NFS也不一定能正常通信,因為它們使用了一個不固定的端口

6.這時先把A機器服務端和B機器客戶端,防火墻關閉,命令systemctl stop firewalld

A機器關閉防火墻

[root@tianqi-01 ~]# systemctl stop firewalld

[root@tianqi-01 ~]# getenforce

Disabled

A機器清空iptables規則

[root@tianqi-01 ~]# iptables -F

[root@tianqi-01 ~]#

B機器關閉防火墻

[root@tianqi-01 ~]# systemctl stop firewalld

[root@tianqi-01 ~]# getenforce

Enforcing

[root@tianqi-01 ~]# setenforce 0

[root@tianqi-01 ~]# getenforce

Permissive

[root@tianqi-01 ~]#

7.這時再去B機器客戶端來showmount -e 192.168.11.136 ,這時會看到可以能show到遠程服務端192.168.11.136這臺機器,它共享的目錄是/home/nfstestdir

[root@tianqi-01 ~]# showmount -e 192.168.11.136

Export list for 192.168.11.136:

/home/nfstestdir 192.168.11.0/24

[root@tianqi-01 ~]#

8.這時候在B機器客戶端上來掛載了,然後查看——>掛載的時間可能有點慢

[root@tianqi-01 ~]# mount -t nfs 192.168.11.136:/home/nfstestdir /mnt/

[root@tianqi-01 ~]# df -h

Filesystem Size Used Avail Use% Mounted on

/dev/sda3 18G 4.8G 14G 27% /

devtmpfs 485M 0 485M 0% /dev

tmpfs 490M 0 490M 0% /dev/shm

tmpfs 490M 6.7M 484M 2% /run

tmpfs 490M 0 490M 0% /sys/fs/cgroup

/dev/sda1 197M 75M 122M 39% /boot

192.168.11.136:/home/nfstestdir 16G 6.7G 9.1G 43% /mnt

[root@tianqi-01 ~]#

9.這個就是遠程NFS服務端共享的目錄

10.這時到B機器客戶端共享的目錄下創建文件

[root@tianqi-01 ~]# cd /mnt/

[root@tianqi-01 mnt]# ls

[root@tianqi-01 mnt]# touch aminglinux.111

[root@tianqi-01 mnt]# ll

total 0

-rw-r--r--. 1 tianqi tianqi 0 Mar 26 20:29 aminglinux.111

[root@tianqi-01 mnt]#

11.到A機器的服務端查看,可以看到文件的屬主和屬組都為1000

[root@tianqi-01 ~]# ll /home/nfstestdir/

total 0

-rw-r--r-- 1 mysql mysql 0 Mar 26 20:29 aminglinux.111

[root@tianqi-01 ~]# tail /etc/passwd

polkitd:x:999:997:User for polkitd:/:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

ntp:x:38:38::/etc/ntp:/sbin/nologin

mysql:x:1000:1000::/home/mysql:/bin/bash

tianqi:x:1001:1001::/home/tianqi:/bin/bash

php-fpm:x:1002:1002::/home/php-fpm:/bin/bash

rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin

rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin

nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

[root@tianqi-01 ~]#

  • 原因是配置 /etc/exports文件時,配置了anonuid=1000,anongid=1000

  • 客戶端顯示1000用戶是因為客戶端機器上並沒有1000權限的用戶

  • 同樣,服務端顯示mysql,是因為服務端上的機器mysql 就是1000權限的用戶

總結

  • 在/etc/exports文件中的/home/nfstestdir 192.168.11.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000) ,其中ip與(rw之間不能有空格, 否則客戶端掛載的目錄會變成只讀..

友情推薦:http://www.apelearn.com阿銘linux

十四周一次課