通过Docker容器部署NFS服务器,权限问题(NAS)
工作中有一个需求,要在Linux服务器上部署一个NFS服务器,但是系统安装软件包有问题,所以希望通过Docker容器部署一个NFS服务器。
参考:
服务器连接DockerHub官方镜像源有问题,所以在国内镜像源中搜索,只能找到itsthenetwork/nfs-server-alpine
镜像的源。
启动命令:
sudo docker run -d --name nfs-server \
-e SHARED_DIRECTORY=/srv/nfs_share \
-v /共享目录的绝对路径/:/srv/nfs_share[容器内路径] \
-p 2049:2049 \
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/itsthenetwork/nfs-server-alpine:12
通过docker logs nfs-server[自定义的镜像名称]
查看容器日志:
$ docker logs nfs-server
Writing SHARED_DIRECTORY to /etc/exports file
The PERMITTED environment variable is unset or null, defaulting to '*'.
This means any client can mount.
The READ_ONLY environment variable is unset or null, defaulting to 'rw'.
Clients have read/write access.
The SYNC environment variable is set, using 'sync' mode.
Writes will be immediately written to disk.
Displaying /etc/exports contents:
/srv/nfs_share *(rw,fsid=0,sync,no_subtree_check,no_auth_nlm,insecure,no_root_squash)
Starting rpcbind...
Displaying rpcbind status...
program version netid address service owner
100000 4 tcp6 ::.0.111 - superuser
100000 3 tcp6 ::.0.111 - superuser
100000 4 udp6 ::.0.111 - superuser
100000 3 udp6 ::.0.111 - superuser
100000 4 tcp 0.0.0.0.0.111 - superuser
100000 3 tcp 0.0.0.0.0.111 - superuser
100000 2 tcp 0.0.0.0.0.111 - superuser
100000 4 udp 0.0.0.0.0.111 - superuser
100000 3 udp 0.0.0.0.0.111 - superuser
100000 2 udp 0.0.0.0.0.111 - superuser
100000 4 local /var/run/rpcbind.sock - superuser
100000 3 local /var/run/rpcbind.sock - superuser
Starting NFS in the background...
rpc.nfsd: Unable to access /proc/fs/nfsd errno 2 (No such file or directory).
Please try, as root, 'mount -t nfsd nfsd /proc/fs/nfsd' and then restart rpc.nfsd to correct the problem
Exporting File System...
exporting *:/srv/nfs_share
/srv/nfs_share <world>
Starting Mountd in the background...These
Startup successful.
是权限的问题,容器内没有用到root权限。镜像的官方说明(https://hub.docker.com/r/itsthenetwork/nfs-server-alpine) 中使用sudo
,但是挂载的时候出现connection refused
拒绝连接的问题,一直尝试通过防火墙解决,失败。最终发现需要使用--privileged
而不是sudo
。
修改后的启动命令:
docker run -d --name nfs-server \
--privileged \
-e SHARED_DIRECTORY=/srv/nfs_share \
-v /共享目录的绝对路径/:/srv/nfs_share[容器内路径] \
-p 2049:2049 \
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/itsthenetwork/nfs-server-alpine:12
挂载命令:
sudo mount -v -o vers=4,loud 10.5.24.127:/ /nfs_mount[提前创建的目录用于挂载,绝对路径]
因为设置了fsid=0
,所以NFS服务端的目录在命令中无需指出。