• Kubernetes PV与PVC 持久卷应用


    持久卷概述

    • PersistentVolume(PV):持久数据卷,对存储资源的抽象,使得存储作为集群中的资源管理。
    • PersistentVolumeClaim(PVC):持久数据卷申请,用户定义使用的存储容量,使得用户不需要关心后端存储实现。
    Pod申请PVC作为卷来使用,Kubernetes通过PVC查找绑定的PV,并挂载到Pod中供程序使用。

    PV与PVC使用流程

    # 容器应用
    apiVersion: v1
    kind: Pod
    metadata:
     name: my-pod
    spec:
     containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
        volumeMounts: 
         - name: www
           mountPath: /usr/share/nginx/html
     volumes:
      - name: www
        persistentVolumeClaim:
         claimName: my-pvc
    ---
    # 卷需求模板
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
     name: my-pvc
    spec:
     accessModes:      #访问模式
      - ReadWriteMany
     resources:
      requests:
       storage: 5Gi   #需要的储存容量
    ---
    # 数据卷定义
    apiVersion: v1
    kind: PersistentVolume
    metadata:
     name: my-pv
    spec:
     capacity:
      storage: 5Gi
     accessModes:
      - ReadWriteMany
     nfs:             #NFS 地址
      path: /ifs/kubernetes
      server: 192.168.95.206
    
    • 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

    在这里插入图片描述

    PV 生命周期

    ACCESS MODES(访问模式):
    AccessModes 是用来对 PV 进行访问模式的设置,用于描述用户应用对存储资源的访问权限,访问权限包括下面几种方式:

    • ReadWriteOnce(RWO):读写权限,但是只能被单个节点挂载
    • ReadOnlyMany(ROX):只读权限,可以被多个节点挂载。
    • ReadWriteMany(RWX):读写权限,可以被多个节点挂载。

    RECLAIM POLICY(回收策略):
    目前 PV 支持的策略有三种:
    • Retain(保留): 保留数据,需要管理员手工清理数据,删除PVC,PV改变为Released状态。
    • Recycle(回收):清除 PV 中的数据,效果相当于执行 rm -rf /nfs/kuberneres/* ,删除PVC,PV改变为 Available状态。
    • Delete(删除):与 PV 相连的后端存储同时删除。
    修改回收策略:persistentVolumeReclaimPolicy: Retain

    STATUS(状态):
    一个 PV 的生命周期中,可能会处于4中不同的阶段:
    • Available(可用):表示可用状态,还未被任何 PVC 绑定。
    • Bound(已绑定):表示 PV 已经被 PVC 绑定。
    • Released(已释放):PVC 被删除,但是资源还未被集群重新声明。
    • Failed(失败): 表示该 PV 的自动回收失败。

    现在PV使用方式称为静态供给,需要K8s运维工程师提前创
    建一堆PV,供开发者使用。
    在这里插入图片描述

    PV 动态供给(StorageClass)

    PV静态供给明显的缺点是维护成本太高了!因此,K8s开始支持PV动态供给,使用StorageClass对象实现。
    在这里插入图片描述

    支持动态供给的存储插件:
    https://kubernetes.io/docs/concepts/storage/storage-classes/
    在这里插入图片描述
    K8s默认不支持NFS动态供给,需要单独部署社区开发的插件。
    项目地址:https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner

    部署:

    kubectl apply -f rbac.yaml # 授权访问apiserver
    kubectl apply -f deployment.yaml # 部署插件,需修改里面NFS服务器地址与共享目录
    kubectl apply -f class.yaml # 创建存储类
    kubectl get sc # 查看存储类
    
    • 1
    • 2
    • 3
    • 4
    # class.yaml 
    piVersion: storage.k8s.io/v1
    kind: StorageClass
    metadata:
      name: managed-nfs-storage
    provisioner: k8s-sigs.io/nfs-subdir-external-provisioner # or choose another name, must match deployment's env PROVISIONER_NAME'
    parameters:
      archiveOnDelete: "false"
    
    # deployment.yaml 
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nfs-client-provisioner
      labels:
        app: nfs-client-provisioner
      # replace with namespace where provisioner is deployed
      namespace: default
    spec:
      replicas: 1
      strategy:
        type: Recreate
      selector:
        matchLabels:
          app: nfs-client-provisioner
      template:
        metadata:
          labels:
            app: nfs-client-provisioner
        spec:
          serviceAccountName: nfs-client-provisioner
          containers:
            - name: nfs-client-provisioner
              image: lizhenliang/nfs-subdir-external-provisioner:v4.0.1
              volumeMounts:
                - name: nfs-client-root
                  mountPath: /persistentvolumes
              env:
                - name: PROVISIONER_NAME
                  value: k8s-sigs.io/nfs-subdir-external-provisioner
                - name: NFS_SERVER
                  value: 192.168.95.206
                - name: NFS_PATH
                  value: /nfs/kubernetes
          volumes:
            - name: nfs-client-root
              nfs:
                server: 192.168.95.206
                path: /nfs/kubernetes
    # rbac.yaml
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: nfs-client-provisioner
      # replace with namespace where provisioner is deployed
      namespace: default
    ---
    kind: ClusterRole
    apiVersion: rbac.authorization.k8s.io/v1
    metadata:
      name: nfs-client-provisioner-runner
    rules:
      - apiGroups: [""]
        resources: ["persistentvolumes"]
        verbs: ["get", "list", "watch", "create", "delete"]
      - apiGroups: [""]
        resources: ["persistentvolumeclaims"]
        verbs: ["get", "list", "watch", "update"]
      - apiGroups: ["storage.k8s.io"]
        resources: ["storageclasses"]
        verbs: ["get", "list", "watch"]
      - apiGroups: [""]
        resources: ["events"]
        verbs: ["create", "update", "patch"]
    ---
    kind: ClusterRoleBinding
    apiVersion: rbac.authorization.k8s.io/v1
    metadata:
      name: run-nfs-client-provisioner
    subjects:
      - kind: ServiceAccount
        name: nfs-client-provisioner
        # replace with namespace where provisioner is deployed
        namespace: default
    roleRef:
      kind: ClusterRole
      name: nfs-client-provisioner-runner
      apiGroup: rbac.authorization.k8s.io
    ---
    kind: Role
    apiVersion: rbac.authorization.k8s.io/v1
    metadata:
      name: leader-locking-nfs-client-provisioner
      # replace with namespace where provisioner is deployed
      namespace: default
    rules:
      - apiGroups: [""]
        resources: ["endpoints"]
        verbs: ["get", "list", "watch", "create", "update", "patch"]
    ---
    kind: RoleBinding
    apiVersion: rbac.authorization.k8s.io/v1
    metadata:
      name: leader-locking-nfs-client-provisioner
      # replace with namespace where provisioner is deployed
      namespace: default
    subjects:
      - kind: ServiceAccount
        name: nfs-client-provisioner
        # replace with namespace where provisioner is deployed
        namespace: default
    roleRef:
      kind: Role
      name: leader-locking-nfs-client-provisioner
      apiGroup: rbac.authorization.k8s.io
    
    • 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

    在这里插入图片描述
    测试:在创建pvc时指定存储类名称。

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
    name: my-pvc
    spec:
    storageClassName: "managed-nfs-storage"
    accessModes:
    - ReadWriteMany
    resources:
    requests:
    storage: 1Gi
    ---
    apiVersion: v1
    kind: Pod
    metadata:
    name: test-pod
    spec:
    containers:
    - name: test-pod
    image: nginx
    volumeMounts:
    - name: nfs-pvc
    mountPath: "/usr/share/nginx/html"
    volumes:
    - name: nfs-pvc
    persistentVolumeClaim:
    claimName: my-pvc
    
    • 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

    在这里插入图片描述

  • 相关阅读:
    轻量封装WebGPU渲染系统示例<22>- 渲染到纹理(RTT)(源码)
    使用4G模块(EC200T)发送UDP数据到内网PC端(内网穿透)
    进制转换数学函数之[CISCN 2019 初赛]Love Math
    linux的基本指令(中)
    python django 小程序博客源码
    STM学习记录(四)———中断及NVIC
    蓝牙设备在智能家居控制系统中的应用
    云原生实战课大纲
    HarmonyOS鸿蒙开发常用4种布局详细说明
    文本纠错易语言代码
  • 原文地址:https://blog.csdn.net/weixin_48190863/article/details/126471775