Proxmox VE 配置 NFS 网络文件系统 🖥️
本指南详细介绍了在 Proxmox VE (PVE) 环境中配置 NFS 共享存储的完整流程,包括服务端和客户端的配置方法。NFS (Network File System) 允许在网络中的不同系统之间共享文件和目录,是虚拟化环境中常用的共享存储解决方案。🚀
📖 导航目录
📖 简介 NFS (Network File System) 是一种分布式文件系统协议,允许客户端计算机上的用户通过网络访问文件,就像访问本地存储一样。在 Proxmox VE 环境中,NFS 常用于:
共享存储 :在多个 PVE 节点间共享虚拟机磁盘映像
集中备份 :提供集中的存储位置用于备份
模板存储 :存储操作系统模板和 ISO 映像
高可用性 :为虚拟机高可用性(HA)提供共享存储
🖥️ 一、PVE 作为 NFS 服务端配置 1. 安装 NFS 服务器软件 1 2 3 4 5 6 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 systemctl status nfs-kernel-server systemctl start nfs-kernel-server 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/ntfsmkdir -p /mnt/mydiskchmod -R 755 /mnt/ntfs /mnt/mydiskchown -R nobody:nogroup /mnt/ntfs /mnt/mydiskcat > /etc/exports <<'EOF' /mnt/ntfs *(rw,sync ,no_subtree_check,no_root_squash,insecure) /mnt/mydisk *(rw,sync ,no_subtree_check,no_root_squash,insecure) EOF exportfs -rva systemctl restart nfs-kernel-server
4. 验证 NFS 共享配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 showmount -e exportfs -v nfsstat -s 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 cat > /etc/nfs.conf <<'EOF' [nfsd] threads=16 [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 showmount -e 10.10.10.251 showmount -e rpcinfo -p 10.10.10.251 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/ntfsmkdir -p /mnt/nfs/mydiskmount -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 nfsmount | grep nfs touch /mnt/nfs/ntfs/test_fileecho "test" > /mnt/nfs/ntfs/test_filecat /mnt/nfs/ntfs/test_filerm /mnt/nfs/ntfs/test_file
3. 配置开机自动挂载 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 cp /etc/fstab /etc/fstab.backup.$(date +%Y%m%d)cat >> /etc/fstab <<'EOF' 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 mount -a df -hT | grep nfssystemctl 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
📊 三、PVE Web 界面配置 NFS
通过 Web 界面添加 NFS 存储:
登录 PVE Web 管理界面
选择数据中心 → 存储
点击”添加” → “NFS”
填写配置信息 :
ID : 自定义存储名称(如: nfs-storage)
服务器 : NFS 服务器 IP (10.10.10.251)
导出 : 共享目录路径 (/mnt/ntfs 或 /mnt/mydisk)
内容类型 : 选择需要的类型(镜像、磁盘、容器等)
选项 :
版本 : NFSv4(推荐) 或 NFSv3
预分配 : 关闭(thin)或开启(full)
格式化 : 如果需要格式化磁盘
点击”添加”保存配置
命令行添加 NFS 存储 1 2 3 4 5 6 7 8 9 10 11 12 13 14 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' RED='\033[1;31m' GREEN='\033[1;32m' YELLOW='\033[1;33m' BLUE='\033[1;34m' NC='\033[0m' LOG_FILE="/var/log/nfs_check.log" 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 " } 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 " 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 " } 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 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 -e */30 * * * * /usr/local/bin/check_nfs_shares.sh >> /var/log/nfs_check.log 2>&1 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 cat > /usr/local/bin/nfs_performance.sh <<'EOF' echo "=== NFS 性能监控 ===" echo "时间: $(date) " echo "====================" 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/serverscat /proc/fs/nfsfs/volumesecho -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 systemctl status nfs-kernel-server journalctl -u nfs-kernel-server -f tail -f /var/log/syslog | grep nfstail -f /var/log/messages | grep nfsufw status verbose iptables -L -n ping 10.10.10.251 telnet 10.10.10.251 2049 nc -zv 10.10.10.251 2049 umount -f /mnt/nfs/ntfs mount -a rpcinfo -u 10.10.10.251 nfs 3 rpcinfo -u 10.10.10.251 nfs 4 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 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 echo "options nfs nfs4_disable_idmapping=0" >> /etc/modprobe.d/nfs.confecho "options nfsd threads=16" >> /etc/modprobe.d/nfs.confecho "net.core.rmem_max = 16777216" >> /etc/sysctl.confecho "net.core.wmem_max = 16777216" >> /etc/sysctl.confecho "net.core.rmem_default = 16777216" >> /etc/sysctl.confecho "net.core.wmem_default = 16777216" >> /etc/sysctl.confsysctl -p echo "options sunrpc tcp_slot_table_entries=128" >> /etc/modprobe.d/sunrpc.confecho "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 /mnt/ntfs 10.10.10.0/24(rw,sync ,no_subtree_check,no_root_squash,insecure) /mnt/ntfs 10.10.10.0/24(rw,sync ,fsid=0,sec=sys,no_subtree_check) 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 nfs4_getfacl /mnt/ntfs 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 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 - cp /etc/fstab /backup/fstab.backup.$(date +%Y%m%d)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 tar -xzf /backup/nfs_config_20230101.tar.gz -C / exportfs -rva systemctl restart nfs-kernel-server cp /backup/fstab.backup.20230101 /etc/fstabmount -a
灾难恢复计划 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' echo "开始 NFS 灾难恢复..." systemctl stop nfs-kernel-server systemctl stop rpcbind if [ -f "/backup/nfs_config_latest.tar.gz" ]; then tar -xzf /backup/nfs_config_latest.tar.gz -C / fi if [ -f "/backup/fstab.backup.latest" ]; then cp /backup/fstab.backup.latest /etc/fstab fi systemctl start rpcbind systemctl start nfs-kernel-server mount -a showmount -e df -hT | grep nfsecho "NFS 灾难恢复完成!" EOF chmod +x /usr/local/bin/nfs_disaster_recovery.sh
通过以上配置和指南,您可以在 PVE 环境中成功搭建和管理 NFS 共享存储,实现高效的文件共享和数据管理!📁 记得定期检查系统状态和维护备份哦!😊
💡 提示 : 在生产环境中进行任何更改前,请务必在测试环境中验证,并确保有完整的数据备份。
Proxmox VE 配置 NFS 网络文件系统 🖥️