跳到主要内容

文件共享服务:NFS

为什么需要 NFS 服务?

家里有些设备,比如电视机,支持通过 NFS 远程读取文件来看路由器磁盘中的视频文件,前提是路由器安装了 NFS 服务。

开源项目

本文部署的 NFS 服务使用这个开源项目构建的容器镜像:https://github.com/ehough/docker-nfs-server

目录结构

nfs
├── config
│   └── exports
├── daemonset.yaml
└── kustomization.yaml

准备 exports 文件

将要共享的目录写在 exports 文件中,每行一个目录,格式为:目录路径 权限设置

/data/media *(rw,no_root_squash,sync)
/data/media/movies *(rw,no_root_squash,sync)

准备 daemonset.yaml

daemonset.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
labels:
app: nfs
name: nfs
namespace: default
spec:
selector:
matchLabels:
app: nfs
template:
metadata:
labels:
app: nfs
spec:
terminationGracePeriodSeconds: 1
containers:
- image: docker.io/erichough/nfs-server:2.2.1
imagePullPolicy: IfNotPresent
name: nfs
securityContext:
privileged: true
volumeMounts:
- mountPath: /data/media
name: media
- mountPath: /lib/modules
name: mod
readOnly: true
- mountPath: /etc/exports
name: exports
subPath: exports
dnsPolicy: Default
hostNetwork: true
restartPolicy: Always
volumes:
- name: media
hostPath:
path: /data/media
type: DirectoryOrCreate
- name: mod
hostPath:
path: /lib/modules
- name: exports
configMap:
name: nfs-exports
updateStrategy:
rollingUpdate:
maxSurge: 0
maxUnavailable: 1
type: RollingUpdate

准备 kustomization.yaml

kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- daemonset.yaml

namespace: default

configMapGenerator:
- name: nfs-exports
files:
- config/exports

注意事项

要求节点有 nfsnfsd 两个内核模块:

lsmod | grep nfs
modprobe {nfs,nfsd}

如没有,ubuntu 可尝试安装:

sudo apt-get install nfs-kernel-server

需禁用节点自身启动的 nfs-server 和 rpc-statd 服务,避免冲突:

systemctl disable nfs-server
systemctl stop nfs-server
systemctl stop rpc-statd