一、部署NFS服务端 安装NFS服务 Ubuntu/Debian 1 2 sudo apt updatesudo apt install nfs-kernel-server -y
Centos/RHEL / Rocky / AlmaLinux 1 sudo dnf install -y nfs-utils
创建共享目录 配置NFS配置文件 1 2 3 sudo vim /etc/exports增加以下内容 /opt/nfs x.x.x.x/xx(rw,sync ,no_subtree_check,no_root_squash)
参数说明: 参数 说明 rw 客户端可读写 ro 客户端只读 sync 数据同步写入磁盘,更安全 async 数据异步写入,性能好但是有一定风险 no_subtree_check 不检查子目录,推荐开启 root_squash 客户端 root 用户映射为匿名用户 no_root_squash 客户端 root 用户保留 root 权限,风险较高 all_squash 所有用户都映射为匿名用户 anonuid/anongid 指定匿名用户 UID/GID
应用配置 重启服务并设置开机自启动 Ubuntu/Debian 1 2 3 sudo systemctl restart nfs-kernel-serversudo systemctl enable nfs-kernel-serversudo systemctl status nfs-kernel-server
CentOS / RHEL / Rocky / AlmaLinux 1 2 sudo systemctl enable --now nfs-serversudo systemctl status nfs-server
安全配置(内网可忽略) 放通防火墙 Ubuntu/Debian 1 2 3 4 5 6 sudo ufw allow from 192.168.1.0/24 to any port 2049 proto tcpsudo ufw allow from 192.168.1.0/24 to any port 2049 proto udpsudo ufw allow from 192.168.1.0/24 to any port 111 proto tcpsudo ufw allow from 192.168.1.0/24 to any port 111 proto udpsudo ufw allow from 192.168.1.0/24 to any port 20048 proto tcpsudo ufw allow from 192.168.1.0/24 to any port 20048 proto udp
或者
1 2 3 4 sudo ufw allow proto tcp to any port nfssudo ufw allow proto udp to any port nfssudo ufw allow proto tcp to any port rpcbindsudo ufw allow proto udp to any port rpcbind
CentOS / RHEL / Rocky / AlmaLinux 1 2 3 4 sudo firewall-cmd --permanent --add-service=nfssudo firewall-cmd --permanent --add-service=mountdsudo firewall-cmd --permanent --add-service=rpc-bindsudo firewall-cmd --reload
云服务器安全组中要放开以下端口(可选) 1 2 3 4 5 6 TCP 2049 UDP 2049 TCP 111 UDP 111 TCP 20048 UDP 20048
二、K8S接入NFS存储 所有K8S节点安装NFS客户端 Ubuntu / Debian 1 2 sudo apt updatesudo apt install -y nfs-common
CentOS / RHEL / Rocky / AlmaLinux 1 sudo dnf install -y nfs-utils
测试节点是否可以挂载 输出内容类似下面这些
Export list for 192.168.1.10:
/srv/nfs/k8s 192.168.1.0/24
Master节点安装helm 1 curl -fsSL https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | sudo bash
如果下载超时可以先在外面下载好再上传到服务器上
https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
然后执行bash
测试是否安装成功
Master节点下载chart包 下载地址:
https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner/releases/download/nfs-subdir-external-provisioner-4.0.18/nfs-subdir-external-provisioner-4.0.18.tgz # 由于github下载需要再外网下载好传输到服务器中
安装chart 1 2 3 4 5 6 7 8 helm install nfs-provisioner ./nfs-subdir-external-provisioner-4.0.18.tgz \ --namespace nfs-provisioner --create-namespace \ --set nfs.server=nfs-ip \ --set nfs.path=nfs-filename \ --set storageClass.name=nfs \ --set storageClass.defaultClass=true \ --set storageClass.reclaimPolicy=Retain \ --set storageClass.archiveOnDelete=true
验证状态 1 2 kubectl get pods -n nfs-provisioner kubectl get storageclass
创建PVC,新建yaml文件添加以下内容
1 2 3 4 5 6 7 8 9 10 11 apiVersion: v1 kind: PersistentVolumeClaim metadata: name: tmp-pvc namespace: default spec: accessModes: - ReadWriteOnce resources: requests: storage: 1Gi
执行yaml文件
1 2 3 kubectl apply -f xxx.yaml kubectl get pvc kubectl delete pvc pvc-name