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