前提条件:

  • k8s集群(已搭建好)

  • nfs服务器(已搭建好)

  • nfs客户端(pod可调度节点作为客户端,已搭建好)

Kubernetes 推荐使用 PersistentVolume (PV)PersistentVolumeClaim (PVC) 来管理存储。

静态PV配置

该方法需要管理员手动配置PV

#测试pod使用nfs存储
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv
  namespace: test-ng
spec:
  capacity:
    storage: 1Gi   # 你想要的存储大小
  accessModes:
    - ReadWriteMany  # 允许多个节点读写
  nfs:
    path: /data/k8s_nfs   # NFS 共享的路径
    server: x.x.x.x  # NFS 服务器地址
  persistentVolumeReclaimPolicy: Retain

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nfs-pvc
  namespace: test-ng
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nfsdemo-v1
  namespace: test-ng
  labels:
    app: nfsdemo
    version: v1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nfsdemo
      version: v1
  template:
    metadata:
      labels:
        app: nfsdemo
        version: v1
    spec:
      containers:
      - name: nfsdemo
        image: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx:stable-alpine
        ports:
        - containerPort: 80
        volumeMounts:
        - name: nfs-storage
          mountPath: "/usr/share/nginx/html"# 容器内的挂载路径
      volumes:
      - name: nfs-storage
        persistentVolumeClaim:
          claimName: nfs-pvc
---
apiVersion: v1
kind: Service
metadata:
  name: nfsdemo-service
  namespace: test-ng
spec:
type: NodePort  # 设置为 NodePort 类型
  selector:
    app: nfsdemo  # 与 Deployment 中的 Pod 匹配的标签
  ports:
  - port: 80          # 服务的端口
    targetPort: 80    # Pod 中容器的端口

动态PV配置

该方法无需手动创建 PV,当用户创建 PVC 时,自动创建对应的 PV。

这需要部署一个 NFS Subdir External Provisioner(例如 nfs-subdir-external-provisioner Helm Chart)。这个 Provisioner 会监视 PVC 的创建,并自动在指定的 NFS 共享目录下创建以命名空间和 PVC 名称命名的子目录作为新的 PV。

使用helm配置

使用helm添加nfs-subdir-external-provisioner

$ helm repo add nfs-subdir-external-provisioner https://kubernetes-sigs.github.io/nfs-subdir-external-provisioner/
$ helm install nfs-subdir-external-provisioner nfs-subdir-external-provisioner/nfs-subdir-external-provisioner \
    --set nfs.server=x.x.x.x \
    --set nfs.path=/data/k8s_nfs

卸载 nfs-subdir-external-provisioner

helm uninstall nfs-subdir-external-provisioner

动态PV测试

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: test-claim
spec:
  storageClassName: "nfs-sc"
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
---
apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  containers:
  - name: test-pod
    image: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx:stable-alpine
    volumeMounts:
    - name: nfs-pvc
      mountPath: "/usr/share/nginx/html"
  volumes:
  - name: nfs-pvc
    persistentVolumeClaim:
      claimName: test-claim

手动配置资源

确保能够连接到nfs server

获取 NFS Subdir External Provisioner 文件,编辑它们以添加您的 NFS 服务器的连接信息,然后使用 kubectl / oc 命令应用每个文件

配置nfs-rbac, kubectl apply -f rbac.yaml

配置 NFS subdir 外部提供程序, kubectl apply -f deployment.yaml

部署存储类 kubectl apply -f class.yaml

完成测试 kubectl apply -f test-claim.yaml kubectl apply -f test-pod.yaml

参考链接
https://blog.csdn.net/qq_49288154/article/details/143956580
https://www.cnblogs.com/nb-blog/p/17970547
https://blog.csdn.net/ltgsoldier1/article/details/142534752
https://mp.weixin.qq.com/s/0EDSZYN_uePKr7mP43gCAw