关于 nfs-storage
nfs-storage 是面向在 Linux 系统间配置网络共享存储的管理员技能,涵盖 NFSv4 服务端导出与客户端挂载、按需挂载、安全、性能调优和 Kubernetes 集成。
主要功能
- 01配置 NFSv4 导出目录
- 02手动或持久挂载共享目录
- 03设置 autofs 按需挂载
- 04配置 Kerberos 安全
- 05调优 NFS 性能及 Kubernetes 存储
适用场景
- 01在多台 Linux 服务器间共享目录
- 02为 Kubernetes 工作负载提供共享存储
- 03集中管理主目录或应用数据
- 04从本地存储迁移到网络存储
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ bagelhole/ devops-security-agent-skills --skill nfs-storage安装后在 Claude Code 中输入/nfs-storage确认已加载。
详细介绍
NFS 存储
配置 NFS 服务端和客户端,以便在 Linux 系统之间共享文件。涵盖 NFSv4 服务端设置、导出选项、客户端挂载、按需挂载的 autofs、Kerberos 安全性、性能调优以及 Kubernetes 集成。
适用场景
- 在多台 Linux 服务端之间共享目录(Web 集群、构建集群)
- 为容器化工作负载提供共享存储(Kubernetes ReadWriteMany)
- 在整个服务器群中集中管理主目录或应用数据
- 搭建共享项目文件的开发环境
- 从本地存储迁移到网络附加存储
前提条件
- NFS 服务端:
nfs-kernel-server(Debian/Ubuntu)或nfs-utils(RHEL/CentOS) - NFS 客户端:
nfs-common(Debian/Ubuntu)或nfs-utils(RHEL/CentOS) - 服务端与客户端之间具备网络连接(NFSv4 使用 TCP/UDP 2049 端口)
- 防火墙规则允许 NFS 流量
- 使用 NFSv4 Kerberos 时:需要
krb5-user和可正常运行的 KDC
NFS 服务端设置
安装
# Debian / Ubuntu
apt update && apt install -y nfs-kernel-server
# RHEL / CentOS
dnf install -y nfs-utils
# Enable and start the NFS server
systemctl enable --now nfs-server
# Verify NFS is running
systemctl status nfs-server
rpcinfo -p | grep nfs
导出配置(/etc/exports)
# /etc/exports
# Syntax: <directory> <client-spec>(options)
# Share /data to a specific subnet with read-write access
/data 10.0.0.0/24(rw,sync,no_subtree_check,no_root_squash)
# Share /shared read-only to everyone
/shared *(ro,sync,no_subtree_check)
# Share /home to specific hosts
/home server01.example.com(rw,sync,no_subtree_check)
/home server02.example.com(rw,sync,no_subtree_check)
# Share /var/nfs/projects with root squash (default, map root to nobody)
/var/nfs/projects 10.0.0.0/24(rw,sync,no_subtree_check,root_squash)
# NFSv4 pseudo-root export (recommended for NFSv4)
/srv/nfs 10.0.0.0/24(rw,sync,fsid=0,crossmnt,no_subtree_check)
/srv/nfs/data 10.0.0.0/24(rw,sync,no_subtree_check,no_root_squash)
/srv/nfs/shared 10.0.0.0/24(ro,sync,no_subtree_check)
导出选项说明
| 选项 | 说明 |
|---|---|
rw | 读写访问 |
ro | 只读访问 |
sync | 回复前将数据写入磁盘(更安全,但速度较慢) |
async | 数据写入磁盘前即回复(速度快,但有数据损坏风险) |
no_subtree_check | 禁用子树检查(提高可靠性) |
root_squash | 将远程 root(UID 0)映射为 nobody(默认设置,更安全) |
no_root_squash | 允许远程 root 在服务端以 root 身份操作(谨慎使用) |
all_squash | 将所有远程 UID/GID 映射为 nobody |
anonuid=1000 | 将匿名用户映射到指定的 UID |
anongid=1000 | 将匿名组映射到指定的 GID |
crossmnt | 允许客户端访问子挂载点 |
fsid=0 | 将其标记为 NFSv4 伪根目录 |
应用导出配置变更
# Apply changes to exports (no server restart needed)
exportfs -ra
# Show current exports
exportfs -v
# Export a new directory on the fly (temporary, not persistent)
exportfs -o rw,sync,no_subtree_check 10.0.0.0/24:/tmp/share
# Unexport a directory
exportfs -u 10.0.0.0/24:/tmp/share
服务端防火墙配置
# UFW (Ubuntu)
ufw allow from 10.0.0.0/24 to any port nfs
ufw allow from 10.0.0.0/24 to any port 111 # rpcbind (NFSv3)
# firewalld (RHEL/CentOS)
firewall-cmd --permanent --add-service=nfs
firewall-cmd --permanent --add-service=rpc-bind
firewall-cmd --permanent --add-service=mountd
firewall-cmd --reload
# NFSv4 only needs TCP 2049 (no rpcbind or mountd)
firewall-cmd --permanent --add-port=2049/tcp
firewall-cmd --reload
NFS 客户端配置
手动挂载
# Install NFS client
apt install -y nfs-common # Debian/Ubuntu
dnf install -y nfs-utils # RHEL/CentOS
# Discover exports from the server
showmount -e nfs-server.example.com
# Mount an NFS share manually
mkdir -p /mnt/data
mount -t nfs nfs-server.example.com:/data /mnt/data
# Mount with specific NFS version and options
mount -t nfs -o vers=4.2,tcp,hard,intr nfs-server.example.com:/data /mnt/data
# Verify the mount
mount | grep nfs
df -hT /mnt/data
# Unmount
umount /mnt/data
通过 /etc/fstab 设置持久挂载
# /etc/fstab entries for NFS
# Basic NFSv4 mount
nfs-server.example.com:/data /mnt/data nfs4 defaults,_netdev 0 0
# Mount with performance and reliability options
nfs-server.example.com:/data /mnt/data nfs4 hard,intr,rsize=1048576,wsize=1048576,timeo=600,retrans=3,_netdev 0 0
# Read-only mount
nfs-server.example.com:/shared /mnt/shared nfs4 ro,_netdev 0 0
# Mount with specific UID/GID mapping (useful for containers)
nfs-server.example.com:/data /mnt/data nfs4 defaults,_netdev,uid=1000,gid=1000 0 0
# Mount all fstab entries
mount -a
# Test fstab entry without actually mounting
mount --fake -a -v
挂载选项说明
| 选项 | 说明 |
|---|---|
hard | 无限重试 NFS 请求(建议使用,以确保数据完整性) |
soft | 在重试 retrans 次后返回错误(有数据损坏风险) |
intr | 允许中断硬挂载的 NFS 请求 |
rsize=1048576 | 读取缓冲区大小,单位为字节(1 MB,NFSv4 的最大值) |
wsize=1048576 | 写入缓冲区大小,单位为字节(1 MB) |
timeo=600 | 超时时间,单位为十分之一秒(60 秒) |
retrans=3 | 出错前(soft)或发出提示前(hard)的重试次数 |
_netdev | 挂载前等待网络就绪(对启动过程至关重要) |
noatime | 不更新访问时间(提高性能) |
nconnect=8 | 使用多个 TCP 连接(内核 5.3 及以上版本可用,可提高吞吐量) |
Autofs(按需挂载)
# Install autofs
apt install -y autofs # Debian/Ubuntu
dnf install -y autofs # RHEL/CentOS
# Configure the master map
# /etc/auto.master or /etc/auto.master.d/nfs.autofs
/mnt/nfs /etc/auto.nfs --timeout=300
/etc/auto.nfs
# Format: mount-point options location
# Mounts will appear under /mnt/nfs/<mount-point>
data -rw,hard,intr,rsize=1048576,wsize=1048576 nfs-server.example.com:/data
shared -ro,hard,intr nfs-server.example.com:/shared
home -rw,hard,intr nfs-server.example.com:/home/&
# Wildcard: mount any subdirectory from the server automatically
# /etc/auto.master entry: /mnt/nfs /etc/auto.nfs
* -rw,hard,intr nfs-server.example.com:/srv/nfs/&
# Enable and start autofs
systemctl enable --now autofs
# Test: simply cd into the mount point and it appears
ls /mnt/nfs/data # Triggers auto-mount
# The share unmounts automatically after the timeout (300 seconds idle)
# Check autofs status
systemctl status autofs
automount -v # Verbose debugging mode (foreground)
性能调优
服务端调优
# Increase the number of NFS daemon threads (default is 8)
# /etc/default/nfs-kernel-server (Debian) or /etc/sysconfig/nfs (RHEL)
RPCNFSDCOUNT=32
# Or set at runtime
echo 32 > /proc/fs/nfsd/threads
# Restart NFS to apply
systemctl restart nfs-server
# Tune NFS server read/write sizes in sysctl
# These are auto-negotiated but can be adjusted
echo 1048576 > /proc/fs/nfsd/max_block_size
# Kernel network buffer tuning (see performance-tuning skill)
sysctl -w net.core.rmem_max=134217728
sysctl -w net.core.wmem_max=134217728
客户端调优
# Use large read/write buffer sizes in mount options
mount -t nfs4 -o rsize=1048576,wsize=1048576,noatime nfs-server:/data /mnt/data
# Use multiple TCP connections (Linux kernel 5.3+)
mount -t nfs4 -o nconnect=8 nfs-server:/data /mnt/data
# Check current mount options and NFS statistics
nfsstat -c # Client NFS statistics
nfsstat -s # Server NFS statistics
mountstats /mnt/data # Detailed per-mount stats
# Test NFS throughput with dd
dd if=/dev/zero of=/mnt/data/testfile bs=1M count=1024 oflag=direct
dd if=/mnt/data/testfile of=/dev/null bs=1M iflag=direct
rm /mnt/data/testfile
# Test with fio for more realistic workloads
fio --name=nfs-test --directory=/mnt/data --ioengine=libaio --direct=1 \
--rw=randrw --bs=4k --numjobs=4 --size=1G --runtime=60 --group_reporting
Kubernetes NFS 集成
# nfs-pv.yaml -- Static PersistentVolume
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-pv
spec:
capacity:
storage: 100Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
storageClassName: nfs
nfs:
server: nfs-server.example.com
path: /data
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nfs-pvc
spec:
accessModes:
- ReadWriteMany
storageClassName: nfs
resources:
requests:
storage: 100Gi
# For dynamic provisioning, install the NFS CSI driver via Helm
helm repo add csi-driver-nfs https://raw.githubusercontent.com/kubernetes-csi/csi-driver-nfs/master/charts
helm install csi-driver-nfs csi-driver-nfs/csi-driver-nfs --namespace kube-system
# Then create a StorageClass pointing to your NFS server and share path.
故障排查
| 症状 | 诊断命令 | 常见修复方法 |
|---|---|---|
| mount:访问被拒绝 | showmount -e server, exportfs -v | 检查 /etc/exports,运行 exportfs -ra,验证子网 |
| mount 卡住 | mount -v,检查网络 | 确认防火墙允许 TCP 2049;使用 bg 挂载选项 |
| 文件句柄已失效 | ls /mnt/data 返回失效错误 | 卸载并重新挂载:umount -f /mnt/data && mount -a |
| 文件访问被拒绝 | ls -la,检查 UID 映射 | 匹配 UID,或使用 all_squash,anonuid=1000,anongid=1000 |
| NFS 性能缓慢 | nfsstat -c, mountstats /mnt/data | 增大 rsize/wsize,添加 nconnect=8,调整 NFS 线程 |
| Autofs 未挂载 | systemctl status autofs, automount -v | 检查 /etc/auto.master 语法,确认服务器可访问 |
| NFSv4 ID 映射错误 | 在两端运行 id username | 确保 /etc/idmapd.conf 中的域名一致 |
| 启动时等待 NFS 导致卡住 | 检查 fstab 选项 | 在 fstab 条目中添加 _netdev 和 bg 选项 |
| Docker 卷挂载失败 | docker volume inspect, dmesg | 确认 Docker 主机已安装 NFS 客户端软件包 |
相关技能
linux-administration-- 服务器设置和网络配置block-storage-- NFS 服务器数据目录所用的底层存储performance-tuning-- 针对 NFS 吞吐量调整内核和网络backup-recovery-- 备份由 NFS 托管的数据object-storage-- 适用于云原生工作负载的替代存储模型
评论
支持 Markdown