Linux-搭建NFS服務器
- 前言
- 一、網絡配置
- 二、在nfs服務器上安裝nfs-utils軟件包
- 三、設置共享目錄
- 四、掛載NFS共享目錄
前言
NFS(Network File System,網絡文件系統
) 是一種分布式文件系統協議,最初由 Sun Microsystems 于 1984 年開發。它允許客戶端計算機通過網絡訪問遠程
服務器上的文件,就像訪問本地文件一樣。NFS 是 Unix 和 Linux 系統中常用的文件共享協議,支持跨平臺文件共享
NFS解決了數據一致性
的問題,實現了共享文件功能,所有web服務器都到NFS服務器里去獲取網頁文件、圖片、音視頻文件、css文件、js文件等,隨便訪問哪臺web服務器,看到的內容都是一樣的
一、網絡配置
配置靜態ip地址,防止今后服務器ip地址的變化,導致服務不能訪問
vim /etc/NetworkManager/system-connections/ens33.nmconnection
禁用selinux功能
setenforce 0
vim /etc/selinux/config SELINUX=disabled
關閉firewalld服務和設置開機不啟動
systemctl stop firewalld
systemctl disable firewalld
二、在nfs服務器上安裝nfs-utils軟件包
[root@dns-nfs ~]#
yum install nfs-utils -y
建議web集群內的在web1和web2上也需要安裝nfs-utils軟件,因為節點服務器里需要支持nfs網絡文件系統,需要掛載共享目錄
在web1和web2上都安裝nfs-utils軟件,不需要啟動nfs服務,主要是使用nfs服務器共享的文件夾,需要去掛載nfs文件系統
如果web服務器不安裝nfs-utils軟件,在mount掛載服務器的共享目錄的時候,不能識別nfs文件系統,導致掛載不成功。
[root@web-1 ~]# yum install nfs-utils -y
[root@web-2 ~]# yum install nfs-utils -y
在nfs服務器上啟動nfs服務并且設置開機啟動就可以了(服務名稱操作系統間可能會有不同)
- centos中為nfs
- rocky中為nfs-server
[root@dns-nfs ~]#
service nfs-server start
Redirecting to /bin/systemctl start nfs-server.service
[root@dns-nfs named]#systemctl enable nfs-server
Created symlink /etc/systemd/system/multi-user.target.wants/nfs-server.service → /usr/lib/systemd/system/nfs-server.service.
查看nfs相關的進程
[root@dns-nfs ~]# ps aux|grep nfs
root 2399 0.0 0.0 0 0 ? S< 10:31 0:00 [nfsd4_callbacks]
root 2405 0.0 0.0 0 0 ? S 10:31 0:00 [nfsd]
root 2406 0.0 0.0 0 0 ? S 10:31 0:00 [nfsd]
root 2407 0.0 0.0 0 0 ? S 10:31 0:00 [nfsd]
root 2408 0.0 0.0 0 0 ? S 10:31 0:00 [nfsd]
root 2409 0.0 0.0 0 0 ? S 10:31 0:00 [nfsd]
root 2410 0.0 0.0 0 0 ? S 10:31 0:00 [nfsd]
root 2411 0.0 0.0 0 0 ? S 10:31 0:00 [nfsd]
root 2412 0.0 0.0 0 0 ? S 10:31 0:00 [nfsd]
root 2443 0.0 0.0 112824 972 pts/0 S+ 10:32 0:00 grep --color=auto nfs
root@dns-nfs ~]# netstat -anplut|grep nfs
[root@dns-nfs ~]#
查看是否有相關的nfs進程去監聽某些端口,發現沒有,為什么呢?
-> 因為nfsd進程將監聽某個端口的功能外包
給了rpc相關的進程
三、設置共享目錄
新建提供web網頁的目錄/web/html文件夾
[root@dns-nfs ~]# mkdir /web/html -p
創建首頁文件index.html,然后上傳feng.jpg圖片
[root@dns-nfs html]# ls
feng.jpg index.html
配置共享文件夾,允許其他人過來讀寫,/etc/exports
文件是nfs共享目錄的配置文件
[root@nfs-server ~]# vim /etc/exports
/web/html 192.168.168.0/24(rw,sync,all_squash)
刷新服務,輸出共享目錄
[root@nfs-server ~]#
exportfs -rv
exporting 192.168.168.0/24:/web/html
四、掛載NFS共享目錄
[root@web-1 ~]# mount 192.168.168.139:/web/html /usr/local/nginx1/html/
# 查看linux系統里的磁盤分區使用情況
[root@web-1 ~]# df -Th
文件系統 類型 容量 已用 可用 已用% 掛載點
devtmpfs devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs tmpfs 1.8G 0 1.8G 0% /dev/shm
tmpfs tmpfs 725M 9.0M 716M 2% /run
/dev/mapper/rl-root xfs 17G 2.2G 15G 13% /
/dev/sda1 xfs 960M 242M 719M 26% /boot
tmpfs tmpfs 363M 0 363M 0% /run/user/0
192.168.168.139:/web/html nfs4 17G 1.5G 16G 9% /usr/local/nginx1/html# 進入查看,發現能看nfs服務器共享的文件夾里的內容
[root@web-1 ~]# cd /usr/local/nginx1/html/
[root@web-1 html]# ls
feng.jpg index.html
在web2上掛著nfs共享目錄
[root@web-2 ~]# mount 192.168.168.139:/web/html /usr/local/nginx1/html/
[root@web-2 ~]# df -Th
文件系統 類型 容量 已用 可用 已用% 掛載點
devtmpfs devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs tmpfs 1.8G 0 1.8G 0% /dev/shm
tmpfs tmpfs 725M 9.0M 716M 2% /run
/dev/mapper/rl-root xfs 17G 2.6G 15G 16% /
/dev/sda1 xfs 960M 242M 719M 26% /boot
tmpfs tmpfs 363M 0 363M 0% /run/user/0
192.168.168.139:/web/html nfs4 17G 1.5G 16G 9% /usr/local/nginx8/html
[root@web-2 ~]# cd /usr/local/nginx8/html/
[root@web-2 html]# ls
feng.jpg index.html
訪問網站看是否網頁內容都是一樣,訪問LB和web1、web2,驗證內容是否一樣
卸載的命令umount
[root@web-2 html]# umount /usr/local/nginx1/html/
umount.nfs4: /usr/local/nginx1/html: device is busy
[root@web-2 html]# pwd
/usr/local/nginx1/html
[root@web-2 html]# cd
[root@web-2 ~]# umount /usr/local/nginx1/html/
[root@web-2 ~]# df
文件系統 1K-塊 已用 可用 已用% 掛載點
devtmpfs 485796 0 485796 0% /dev
tmpfs 497852 0 497852 0% /dev/shm
tmpfs 497852 7776 490076 2% /run
tmpfs 497852 0 497852 0% /sys/fs/cgroup
/dev/mapper/centos-root 17811456 2259608 15551848 13% /
/dev/sda1 1038336 153860 884476 15% /boot
tmpfs 99572 0 99572 0% /run/user/0
設置web1和web2開機自動掛著nfs共享目錄
[root@web-1 html]# vim /etc/rc.local
#添加下面的配置
mount 192.168.168.139:/web/html /usr/local/nginx1/html/