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

浙公网安备 33010602011771号