最近学习kubernetes,需要搭建NFS服务器,就查了下如何安装NFS服务器,具体步骤如下。
关闭防火墙,并设置开机禁止启动
systemctl stop firewalld
systemctl disable firewalld
安装NFS服务器,需要安装nfs-utils和rpcbind两个包,安装nfs-utils时会自动安装rpcbind
yum install nfs-utils -y
启动NFS服务,并设置开机自启
启动rpcbind
systemctl start rpcbind
systemctl enable rpcbind
systemctl status rpcbind
确认rpcbind状态,running表示启动成功
# systemctl status rpcbind
● rpcbind.service - RPC bind service
Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2020-06-01 17:31:08 CST; 2 days ago
Main PID: 973 (rpcbind)
CGroup: /system.slice/rpcbind.service
└─973 /sbin/rpcbind -w
Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.
启动nfs
systemctl start nfs
systemctl enable nfs
systemctl status nfs
确认nfs状态,running表示启动成功
# 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@k8s-master ~]# systemctl status nfs
● nfs-server.service - NFS server and services
Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; vendor preset: disabled)
Active: active (exited) since Thu 2020-06-04 16:36:25 CST; 6s ago
Main PID: 9628 (code=exited, status=0/SUCCESS)
CGroup: /system.slice/nfs-server.service
Jun 04 16:36:25 k8s-master systemd[1]: Starting NFS server and services...
Jun 04 16:36:25 k8s-master systemd[1]: Started NFS server and services.
创建nfs共享目录/nfs/nfsdisk1,修改该目录权限为777,
mkdir -p /nfs/nfsdisk
chmod 777 /nfs/nfsdisk
修改nfs配置文件/etc/exports
# vi /etc/exports
/nfs/nfsdisk *(rw,sync,no_root_squash)
配置文件说明
重启nfs并确认状态
systemctl restart rpcbind
systemctl status rpcbind
systemctl restart nfs
systemctl status nfs
showmount -e
挂载nfs磁盘,可以在该安装NFS服务器的主机上挂载磁盘,也可以另外找一台和该主机通信的主机挂载磁盘
mkdir -p /mnt/nfsdisk
mount -t nfs 192.168.92.201:/nfs/nfsdisk /mnt/nfsdisk
测试文件写入
在挂载该磁盘的机器上写入hellonfs.txt
echo "Hello nfsdisk" > /mnt/nfsdisk/hellonfs.txt
# ls -l /mnt/nfsdisk
total 4
-rw-r--r-- 1 root root 14 Jun 4 17:08 hellonfs.txt
在安装NFS服务器的主机上,确认hellonfs.txt是否写入到nfs共享目录
# ll /nfs/nfsdisk/
total 4
-rw-r--r-- 1 root root 14 Jun 4 17:08 hellonfs.txt
# cat /nfs/nfsdisk/hellonfs.txt
Hello nfsdisk
卸载nfs磁盘
umount /mnt/nfsdisk