Proxmox VE 配置 NFS 网络文件系统 🖥️

本指南详细介绍了在 Proxmox VE (PVE) 环境中配置 NFS 共享存储的完整流程,包括服务端和客户端的配置方法。NFS (Network File System) 允许在网络中的不同系统之间共享文件和目录,是虚拟化环境中常用的共享存储解决方案。🚀


📖 导航目录


📖 简介

NFS (Network File System) 是一种分布式文件系统协议,允许客户端计算机上的用户通过网络访问文件,就像访问本地存储一样。在 Proxmox VE 环境中,NFS 常用于:

  • 共享存储:在多个 PVE 节点间共享虚拟机磁盘映像
  • 集中备份:提供集中的存储位置用于备份
  • 模板存储:存储操作系统模板和 ISO 映像
  • 高可用性:为虚拟机高可用性(HA)提供共享存储

PVE NFS 配置


🖥️ 一、PVE 作为 NFS 服务端配置

1. 安装 NFS 服务器软件

1
2
3
4
5
6
# 更新软件包列表并安装 NFS 服务器
apt-get update
apt-get install nfs-kernel-server nfs-common -y

# 安装依赖包
apt-get install rpcbind -y

2. 检查 NFS 服务状态

1
2
3
4
5
6
7
8
9
10
11
12
# 查看 NFS 服务运行状态
systemctl status nfs-kernel-server

# 启动 NFS 服务(如果未运行)
systemctl start nfs-kernel-server

# 设置 NFS 服务开机自启
systemctl enable nfs-kernel-server

# 检查相关服务状态
systemctl status rpcbind
systemctl status nfs-mountd

3. 创建共享目录并配置 NFS 导出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 创建共享目录并设置权限
mkdir -p /mnt/ntfs
mkdir -p /mnt/mydisk
chmod -R 755 /mnt/ntfs /mnt/mydisk

# 设置所有权(根据需要调整)
chown -R nobody:nogroup /mnt/ntfs /mnt/mydisk

# 创建并配置 exports 文件
cat > /etc/exports <<'EOF'
# NFS 共享配置
# 格式: 共享目录 客户端(选项)
/mnt/ntfs *(rw,sync,no_subtree_check,no_root_squash,insecure)
/mnt/mydisk *(rw,sync,no_subtree_check,no_root_squash,insecure)

# 或者限制特定网络段访问
# /mnt/ntfs 10.10.10.0/24(rw,sync,no_subtree_check,no_root_squash,insecure)
# /mnt/mydisk 10.10.10.0/24(rw,sync,no_subtree_check,no_root_squash,insecure)
EOF

# 重新加载 NFS 配置
exportfs -rva

# 重启 NFS 服务使配置生效
systemctl restart nfs-kernel-server

4. 验证 NFS 共享配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 查看本机 NFS 共享列表
showmount -e

# 查看详细的导出列表
exportfs -v

# 检查 NFS 状态
nfsstat -s

# 检查 RPC 服务状态
rpcinfo -p

# 检查防火墙设置(如果需要)
ufw allow from 10.10.10.0/24 to any port nfs
ufw status verbose

5. 高级 NFS 服务器配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 编辑 NFS 配置文件
cat > /etc/nfs.conf <<'EOF'
[nfsd]
# 设置 NFS 线程数
threads=16

# 设置 NFS 版本
# vers2=no
# vers3=yes
# vers4=yes
# vers4.0=yes
# vers4.1=yes
# vers4.2=yes

[mountd]
# 管理端口设置
manage-gids=yes

[statd]
# 状态监控设置
EOF

# 重启服务应用配置
systemctl restart nfs-kernel-server

💻 二、PVE 作为 NFS 客户端配置

1. 查看远程 NFS 共享

1
2
3
4
5
6
7
8
9
10
11
12
# 查看指定服务器的 NFS 共享
showmount -e 10.10.10.251

# 查看本机可用的 NFS 共享
showmount -e

# 使用 rpcinfo 检查服务
rpcinfo -p 10.10.10.251

# 检查 NFS 版本支持
rpcinfo -u 10.10.10.251 nfs 3
rpcinfo -u 10.10.10.251 nfs 4

2. 手动挂载 NFS 共享

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 创建本地挂载点
mkdir -p /mnt/nfs/ntfs
mkdir -p /mnt/nfs/mydisk

# 挂载 NFS 共享(使用 NFSv4 以获得更好的性能和安全)
mount -t nfs4 -o vers=4.1,async,noatime,nodiratime 10.10.10.251:/mnt/ntfs /mnt/nfs/ntfs
mount -t nfs4 -o vers=4.1,async,noatime,nodiratime 10.10.10.251:/mnt/mydisk /mnt/nfs/mydisk

# 验证挂载结果
df -hT | grep nfs
mount | grep nfs

# 测试读写权限
touch /mnt/nfs/ntfs/test_file
echo "test" > /mnt/nfs/ntfs/test_file
cat /mnt/nfs/ntfs/test_file
rm /mnt/nfs/ntfs/test_file

3. 配置开机自动挂载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 备份原 fstab 文件
cp /etc/fstab /etc/fstab.backup.$(date +%Y%m%d)

# 编辑 /etc/fstab 文件
cat >> /etc/fstab <<'EOF'
# NFS 共享配置
10.10.10.251:/mnt/ntfs /mnt/nfs/ntfs nfs4 vers=4.1,async,noatime,nodiratime,_netdev,auto 0 0
10.10.10.251:/mnt/mydisk /mnt/nfs/mydisk nfs4 vers=4.1,async,noatime,nodiratime,_netdev,auto 0 0
EOF

# 测试 fstab 配置
mount -a

# 检查挂载结果
df -hT | grep nfs

# 如果使用 systemd,可以启用自动挂载服务
systemctl daemon-reload

4. 高级挂载选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 性能优化挂载选项
mount -t nfs4 -o \
vers=4.1,\
async,\
noatime,\
nodiratime,\
rsize=131072,\
wsize=131072,\
hard,\
intr,\
timeo=600,\
retrans=2 \
10.10.10.251:/mnt/ntfs /mnt/nfs/ntfs

# 参数说明:
# vers=4.1 - 使用 NFSv4.1 协议
# async - 异步写入,提高性能
# noatime - 不更新文件访问时间,减少磁盘操作
# nodiratime - 不更新目录访问时间
# rsize/wsize - 读写缓冲区大小(通常设置为 131072 或 65536)
# hard - 硬挂载,服务器无响应时重试
# intr - 允许中断 NFS 操作
# timeo - 超时时间(十分之一秒)
# retrans - 重试次数
# _netdev - 等待网络就绪后再挂载

📊 三、PVE Web 界面配置 NFS

PVE NFS 配置界面

通过 Web 界面添加 NFS 存储:

  1. 登录 PVE Web 管理界面
  2. 选择数据中心 → 存储
  3. 点击”添加” → “NFS”
  4. 填写配置信息

    • ID: 自定义存储名称(如: nfs-storage)
    • 服务器: NFS 服务器 IP (10.10.10.251)
    • 导出: 共享目录路径 (/mnt/ntfs 或 /mnt/mydisk)
    • 内容类型: 选择需要的类型(镜像、磁盘、容器等)
    • 选项:
      • 版本: NFSv4(推荐) 或 NFSv3
      • 预分配: 关闭(thin)或开启(full)
      • 格式化: 如果需要格式化磁盘
  5. 点击”添加”保存配置

命令行添加 NFS 存储

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 使用 pvesm 命令添加 NFS 存储
pvesm add nfs nfs-storage \
--server 10.10.10.251 \
--export /mnt/ntfs \
--options vers=4.1 \
--content images,iso,backup

# 查看存储状态
pvesm status
pvesm list

# 设置存储选项
pvesm set nfs-storage --nodes pve-node1,pve-node2
pvesm set nfs-storage --shared 1

🔍 四、NFS 共享监控脚本

创建 NFS 共享检查脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# 创建检查脚本
cat > /usr/local/bin/check_nfs_shares.sh <<'EOF'
#!/bin/bash

# 颜色定义
RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
BLUE='\033[1;34m'
NC='\033[0m' # No Color

# 日志文件
LOG_FILE="/var/log/nfs_check.log"

# NFS 服务器列表
declare -A nfs_servers=(
["10.10.10.242"]="PVE-Node-1"
["10.10.10.252"]="PVE-Node-2"
["10.10.10.243"]="PVE-Node-3"
["10.10.10.254"]="PVE-Node-4"
["10.10.10.251"]="PVE-NAS-1"
["10.10.10.245"]="PVE-NAS-2"
["10.10.10.246"]="PVE-Backup-1"
["10.10.10.247"]="PVE-Backup-2"
)

# 函数:记录日志
log_message() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE"
}

# 函数:检查 NFS 服务器
check_nfs_server() {
local server_ip=$1
local server_name=$2

echo -e "\n${BLUE}检查服务器: $server_name ($server_ip)${NC}" | tee -a "$LOG_FILE"

# 检查服务器是否在线
if ping -c 1 -W 1 "$server_ip" &> /dev/null; then
echo -e "${GREEN}✓ 服务器在线${NC}" | tee -a "$LOG_FILE"

# 检查 NFS 服务
if rpcinfo -u "$server_ip" nfs &> /dev/null; then
echo -e "${GREEN}✓ NFS 服务正常${NC}" | tee -a "$LOG_FILE"

# 获取共享列表
local shares
shares=$(showmount -e "$server_ip" 2>/dev/null)
if [ $? -eq 0 ]; then
echo -e "${GREEN}✓ NFS 共享列表:${NC}" | tee -a "$LOG_FILE"
echo "$shares" | sed '1d' | while read -r line; do
echo -e " ${GREEN}$line${NC}" | tee -a "$LOG_FILE"
done

# 检查挂载点
check_nfs_mounts "$server_ip"
else
echo -e "${YELLOW}⚠ 无法获取共享列表${NC}" | tee -a "$LOG_FILE"
fi
else
echo -e "${RED}✗ NFS 服务异常${NC}" | tee -a "$LOG_FILE"
log_message "ERROR: NFS service down on $server_name ($server_ip)"
fi
else
echo -e "${RED}✗ 服务器离线${NC}" | tee -a "$LOG_FILE"
log_message "ERROR: Server $server_name ($server_ip) is offline"
fi
echo "-----------------------" | tee -a "$LOG_FILE"
}

# 函数:检查 NFS 挂载点
check_nfs_mounts() {
local server_ip=$1
mount | grep "nfs" | grep "$server_ip" | while read -r line; do
local mount_point
mount_point=$(echo "$line" | awk '{print $3}')
local device
device=$(echo "$line" | awk '{print $1}')

# 检查挂载点是否可访问
if touch "$mount_point/.nfs_test" 2>/dev/null; then
echo -e " ${GREEN}✓ 挂载点 $mount_point 正常${NC}" | tee -a "$LOG_FILE"
rm -f "$mount_point/.nfs_test"
else
echo -e " ${RED}✗ 挂载点 $mount_point 异常${NC}" | tee -a "$LOG_FILE"
log_message "ERROR: Mount point $mount_point ($device) is not accessible"

# 尝试重新挂载
umount -f "$mount_point" 2>/dev/null
mount "$device" "$mount_point" 2>/dev/null
if [ $? -eq 0 ]; then
echo -e " ${GREEN}✓ 挂载点 $mount_point 已恢复${NC}" | tee -a "$LOG_FILE"
log_message "INFO: Mount point $mount_point recovered"
fi
fi
done
}

# 主函数
main() {
echo "=== NFS 共享状态检查 ===" | tee -a "$LOG_FILE"
echo "开始时间: $(date)" | tee -a "$LOG_FILE"
echo "==========================" | tee -a "$LOG_FILE"

for server_ip in "${!nfs_servers[@]}"; do
server_name="${nfs_servers[$server_ip]}"
check_nfs_server "$server_ip" "$server_name"
done

# 检查本地 NFS 服务状态
echo -e "\n${BLUE}检查本地 NFS 服务状态${NC}" | tee -a "$LOG_FILE"
systemctl status nfs-kernel-server --no-pager | tee -a "$LOG_FILE"

echo -e "\n检查完成时间: $(date)" | tee -a "$LOG_FILE"
echo "详细日志请查看: $LOG_FILE" | tee -a "$LOG_FILE"
}

# 执行主函数
main "$@"
EOF

# 设置脚本权限
chmod +x /usr/local/bin/check_nfs_shares.sh

# 运行检查脚本
/usr/local/bin/check_nfs_shares.sh

设置定时检查任务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 编辑 crontab
crontab -e

# 添加以下内容(每30分钟检查一次)
*/30 * * * * /usr/local/bin/check_nfs_shares.sh >> /var/log/nfs_check.log 2>&1

# 或者创建 systemd timer
cat > /etc/systemd/system/nfs-check.service <<'EOF'
[Unit]
Description=NFS Share Check Service
After=network.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/check_nfs_shares.sh
User=root
EOF

cat > /etc/systemd/system/nfs-check.timer <<'EOF'
[Unit]
Description=Run NFS check every 30 minutes

[Timer]
OnBootSec=5min
OnUnitActiveSec=30min

[Install]
WantedBy=timers.target
EOF

systemctl enable nfs-check.timer
systemctl start nfs-check.timer

NFS 性能监控脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 创建 NFS 性能监控脚本
cat > /usr/local/bin/nfs_performance.sh <<'EOF'
#!/bin/bash

echo "=== NFS 性能监控 ==="
echo "时间: $(date)"
echo "===================="

# 查看 NFS 统计信息
echo -e "\nNFS 客户端统计:"
nfsstat -c

echo -e "\nNFS 服务器统计:"
nfsstat -s

echo -e "\nNFS 挂载点统计:"
mount | grep nfs

echo -e "\nNFS I/O 统计:"
cat /proc/fs/nfsfs/servers
cat /proc/fs/nfsfs/volumes

echo -e "\n网络连接统计:"
ss -t -a | grep :nfs
EOF

chmod +x /usr/local/bin/nfs_performance.sh

🛠️ 五、故障排除和优化

常见问题解决

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 1. 检查 NFS 服务状态
systemctl status nfs-kernel-server
journalctl -u nfs-kernel-server -f

# 2. 查看 NFS 日志
tail -f /var/log/syslog | grep nfs
tail -f /var/log/messages | grep nfs

# 3. 检查防火墙设置
ufw status verbose
iptables -L -n

# 4. 测试网络连通性
ping 10.10.10.251
telnet 10.10.10.251 2049
nc -zv 10.10.10.251 2049

# 5. 重新挂载 NFS 共享
umount -f /mnt/nfs/ntfs
mount -a

# 6. 检查 NFS 版本兼容性
rpcinfo -u 10.10.10.251 nfs 3
rpcinfo -u 10.10.10.251 nfs 4

# 7. 检查文件锁状态
nfs4_getfacl /mnt/nfs/ntfs

性能优化建议

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 1. 调整 NFS 挂载参数(在 /etc/fstab 中)
10.10.10.251:/mnt/ntfs /mnt/nfs/ntfs nfs4 rw,vers=4.1,async,noatime,nodiratime,rsize=131072,wsize=131072,hard,intr,timeo=600,retrans=2,_netdev 0 0

# 2. 增加 NFS 线程数
echo "options nfs nfs4_disable_idmapping=0" >> /etc/modprobe.d/nfs.conf
echo "options nfsd threads=16" >> /etc/modprobe.d/nfs.conf

# 3. 调整网络参数
echo "net.core.rmem_max = 16777216" >> /etc/sysctl.conf
echo "net.core.wmem_max = 16777216" >> /etc/sysctl.conf
echo "net.core.rmem_default = 16777216" >> /etc/sysctl.conf
echo "net.core.wmem_default = 16777216" >> /etc/sysctl.conf
sysctl -p

# 4. 使用更快的网络连接
# 考虑使用 10GbE 或更高速度的网络

# 5. 使用高性能存储后端
# 如 SSD 或 NVMe 存储

# 6. 调整 NFS 服务器配置
echo "options sunrpc tcp_slot_table_entries=128" >> /etc/modprobe.d/sunrpc.conf
echo "options sunrpc tcp_max_slot_table_entries=128" >> /etc/modprobe.d/sunrpc.conf

🔒 六、安全配置建议

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 1. 限制 NFS 访问权限(修改 /etc/exports)
/mnt/ntfs 10.10.10.0/24(rw,sync,no_subtree_check,no_root_squash,insecure)

# 2. 使用更安全的 NFSv4
/mnt/ntfs 10.10.10.0/24(rw,sync,fsid=0,sec=sys,no_subtree_check)

# 3. 配置防火墙规则
ufw allow from 10.10.10.0/24 to any port nfs
ufw allow from 10.10.10.0/24 to any port mountd
ufw allow from 10.10.10.0/24 to any port nlockmgr

# 4. 使用 Kerberos 认证(高级)
# /mnt/ntfs 10.10.10.0/24(rw,sync,sec=krb5p)

# 5. 定期检查 NFS 权限
nfs4_getfacl /mnt/ntfs

# 6. 使用 VPN 或专用网络
# 将 NFS 流量隔离到专用网络

# 7. 监控 NFS 访问日志
cat > /etc/rsyslog.d/nfs.conf <<'EOF'
if $programname == 'nfsd' then /var/log/nfs.log
& stop
EOF

systemctl restart rsyslog

💾 七、备份和恢复

NFS 配置备份

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 备份 NFS 配置文件
tar -czf /backup/nfs_config_$(date +%Y%m%d).tar.gz \
/etc/exports \
/etc/nfs.conf \
/etc/nfsmount.conf \
/etc/modprobe.d/nfs.conf \
/etc/sysctl.d/nfs.conf

# 备份共享目录结构
find /mnt/ntfs /mnt/mydisk -type f -name "*.conf" -o -name "*.cfg" | \
tar -czf /backup/nfs_shared_config_$(date +%Y%m%d).tar.gz -T -

# 备份 fstab 配置
cp /etc/fstab /backup/fstab.backup.$(date +%Y%m%d)

# 备份 PVE 存储配置
pvesm status > /backup/pve_storage_status_$(date +%Y%m%d).txt
pvesm list --output-format json > /backup/pve_storage_list_$(date +%Y%m%d).json

恢复 NFS 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
# 恢复 NFS 配置文件
tar -xzf /backup/nfs_config_20230101.tar.gz -C /

# 重新加载配置
exportfs -rva
systemctl restart nfs-kernel-server

# 恢复 fstab 配置
cp /backup/fstab.backup.20230101 /etc/fstab
mount -a

# 恢复 PVE 存储配置
# 需要根据备份重新添加存储

灾难恢复计划

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 创建灾难恢复脚本
cat > /usr/local/bin/nfs_disaster_recovery.sh <<'EOF'
#!/bin/bash

echo "开始 NFS 灾难恢复..."

# 1. 停止相关服务
systemctl stop nfs-kernel-server
systemctl stop rpcbind

# 2. 恢复配置文件
if [ -f "/backup/nfs_config_latest.tar.gz" ]; then
tar -xzf /backup/nfs_config_latest.tar.gz -C /
fi

# 3. 恢复 fstab
if [ -f "/backup/fstab.backup.latest" ]; then
cp /backup/fstab.backup.latest /etc/fstab
fi

# 4. 启动服务
systemctl start rpcbind
systemctl start nfs-kernel-server

# 5. 重新挂载
mount -a

# 6. 验证恢复
showmount -e
df -hT | grep nfs

echo "NFS 灾难恢复完成!"
EOF

chmod +x /usr/local/bin/nfs_disaster_recovery.sh

通过以上配置和指南,您可以在 PVE 环境中成功搭建和管理 NFS 共享存储,实现高效的文件共享和数据管理!📁 记得定期检查系统状态和维护备份哦!😊

💡 提示: 在生产环境中进行任何更改前,请务必在测试环境中验证,并确保有完整的数据备份。