PV和PVC的生命周期

  PV是kubernetes集群的存储资源,而PVC则代表着资源需求。创建PVC时对PV发起的使用申请,即为“绑定”。PV和PVC是一一对应的关系,可用于响应PVC申请的PV必须要能够容纳PVC的请求条件,它们二者的交互遵循如下生命周期。

一、存储供给

  存储供给(Provisioning)是指为PVC准备可用的PV机制。kubernetes支持两种PV供给方式:静态供给和动态供给。

1. 静态供给

  静态供给是指由集群管理员手动创建一定数量的PV的资源供应方式。这些PV负责处理存储系统的细节,并将其抽象成易用的存储资源供用户使用。静态提供的PV可能属于某存储类,也可能没有存储类,这一点取决于管理员的设定。

  下面示例就是静态供给,手动创建PV,没有存储类的示例:

  创建nfs共享目录:

[root@k8s-master1 ~]# vim /etc/exports
[root@k8s-master1 ~]# export
export    exportfs
[root@k8s-master1 ~]# exportfs -arv
exporting 10.0.0.131/24:/data/volume_test/v1
exporting 10.0.0.131/24:/data/volumes

  创建pv资源:

[root@k8s-master1 pv]# cat pv-test.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-static-test
spec:
  capacity:
    storage: 1Gi
  accessModes: ["ReadWriteOnce"]
  nfs:
    path: /data/volume_test/v1
    server: 10.0.0.131
[root@k8s-master1 pv]# kubectl apply -f pv-test.yaml
persistentvolume/pv-static-test created
[root@k8s-master1 pv]# kubectl get pv -o wide
NAME             CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE   VOLUMEMODE
pv-static-test   1Gi        RWO            Retain           Available                                   15s   Filesystem

2. 动态供给

  不存在某静态的PV匹配到用户的PVC申请时,kubernetes集群会尝试为PVC动态创建符合需求的PV,此即为动态供给。这种方式依赖于存储类的辅助,PVC必须向一个事先存在的存储类发起动态分配PV的请求,没有指定存储类的PVC请求会被禁止使用动态创建PV的方式。

  下面示例就是动态供给,动态创建PV,PVC的创建依赖于存储类course-nfs-storage

[root@k8s-master1 storageclass]# cat test-pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: pvc-nfs-dynamic-001
spec:
  storageClassName: course-nfs-storage
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 1Gi

二、 存储绑定

  用户基于一系列存储需求和访问模式定义好的PVC后,kubernetes系统的控制器即会为其查找匹配的PV,并于找到之后在此二者之间创建其关联的关系,而后它们二者之间的状态即转为“绑定(Binding)”。若PV是为PVC而动态创建的,则该PV专用于其PVC。

  若是无法为PVC找到可匹配的PV,则PVC将一直处于未绑定(unbound)状态,直到有符合条件的PV出现并完全绑定方式才可用。

1. 存储使用

  Pod资源基于persistentVolumeClaim卷类型的定义,将选定的PVC关联为存储卷,而后即可为内部的容器所使用的。对于支持多种访问模式的存储卷来说,用户需要额外指定要使用的模式。一旦完成将存储卷挂载至pod对象的容器中,其应用即可使用关联的PV提供的存储空间。

  创建PVC,关联到匹配的PV,PVC的状态处于Binding状态:

[root@k8s-master1 pv]# vim pvc-demo2.yaml
[root@k8s-master1 pv]# cat pvc-demo2.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-test
spec:
  accessModes: ["ReadWriteOnce"]
  resources:
    requests:
      storage: 1Gi
You have new mail in /var/spool/mail/root
[root@k8s-master1 pv]# kubectl apply -f pvc-demo2.yaml
persistentvolumeclaim/pvc-test created
[root@k8s-master1 pv]# kubectl get pvc -o wide
NAME       STATUS   VOLUME           CAPACITY   ACCESS MODES   STORAGECLASS   AGE   VOLUMEMODE
pvc-test   Bound    pv-static-test   1Gi        RWO                           6s    Filesystem 

  创建pod使用创建的PVC

[root@k8s-master1 pv]# cat pod-pvc.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pvc-pod
spec:
  containers:
  - name: nginx
    image: nginx:latest
    imagePullPolicy: IfNotPresent
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html
  volumes:
  - name: html
    persistentVolumeClaim:
      claimName: pvc-test
[root@k8s-master1 pv]# kubectl apply -f pod-pvc.yaml
pod/pvc-pod created
[root@k8s-master1 pv]# kubectl get pods -o wide
NAME                                     READY   STATUS    RESTARTS   AGE     IP             NODE        NOMINATED NODE   READINESS GATES
nfs-client-provisioner-5d65b75f7-2f7qx   1/1     Running   3          5h15m   10.244.36.87   k8s-node1   <none>           <none>
pvc-pod                                  1/1     Running   0          6s      10.244.36.93   k8s-node1   <none>           <none>

  在NFS服务器的共享目录下创建访问页面:

[root@k8s-master1 ~]# cd /data/volume_test/v1
You have new mail in /var/spool/mail/root
[root@k8s-master1 v1]# echo "test pvc in nginx container" >>index.html
[root@k8s-master1 v1]# cat index.html
test pvc in nginx container
[root@k8s-master1 v1]#

  测试访问页面,查看数据是否已被同步到容器中

[root@k8s-master1 pv]# kubectl get pods -o wide
NAME                                     READY   STATUS    RESTARTS   AGE     IP             NODE        NOMINATED NODE   READINESS GATES
nfs-client-provisioner-5d65b75f7-2f7qx   1/1     Running   3          5h23m   10.244.36.87   k8s-node1   <none>           <none>
pvc-pod                                  1/1     Running   0          8m6s    10.244.36.94   k8s-node1   <none>           <none>
You have new mail in /var/spool/mail/root
[root@k8s-master1 pv]# curl 10.244.36.94
test pvc in nginx container
[root@k8s-master1 pv]#

  上述命令结果说明创建的PVC已被pvc-pod使用

2. PVC保护

  为了避免使用中的存储卷被移除而导致数据丢失,kubernetes 自1.9版本起引入了PVC保护机制。启用了此特性后,万一有用户删除了仍处于某pod资源使用中的pvc时,kubernetes不会立即予以移除,而是推迟到不再被任何pod资源使用后方才执行删除操作。处于此阶段的PVC资源的status字段为“Termination”,并且其Finalizers字段中包含“kubernetes.io/pvc-protection”。

  测试PVC的保护机制,删除pvc,查看pvc的状态变为Terminating:

[root@k8s-master1 pv]# kubectl get pvc -o  wide
NAME       STATUS   VOLUME           CAPACITY   ACCESS MODES   STORAGECLASS   AGE   VOLUMEMODE
pvc-test   Bound    pv-static-test   1Gi        RWO                           16m   Filesystem
[root@k8s-master1 pv]# kubectl delete pvc pvc-test
persistentvolumeclaim "pvc-test" deleted

[root@k8s-master1 ~]# kubectl get pvc -o wide
NAME       STATUS        VOLUME           CAPACITY   ACCESS MODES   STORAGECLASS   AGE   VOLUMEMODE
pvc-test   Terminating   pv-static-test   1Gi        RWO                           16m   Filesystem
[root@k8s-master1 ~]# kubectl describe pvc pvc-test
Name:          pvc-test
Namespace:     default
StorageClass:
Status:        Terminating (lasts 77s)
Volume:        pv-static-test
Labels:        <none>
Annotations:   pv.kubernetes.io/bind-completed: yes
               pv.kubernetes.io/bound-by-controller: yes
Finalizers:    [kubernetes.io/pvc-protection]
Capacity:      1Gi
Access Modes:  RWO
VolumeMode:    Filesystem
Used By:       pvc-pod
Events:        <none>
[root@k8s-master1 ~]#

  删除pod, 再次查看pvc,发现pvc已被删除

[root@k8s-master1 pv]# kubectl get pvc -o  wide
NAME       STATUS        VOLUME           CAPACITY   ACCESS MODES   STORAGECLASS   AGE   VOLUMEMODE
pvc-test   Terminating   pv-static-test   1Gi        RWO                           20m   Filesystem
[root@k8s-master1 pv]# kubectl get pods -o wide
NAME                                     READY   STATUS    RESTARTS   AGE     IP             NODE        NOMINATED NODE   READINESS GATES
nfs-client-provisioner-5d65b75f7-2f7qx   1/1     Running   3          5h31m   10.244.36.87   k8s-node1   <none>           <none>
pvc-pod                                  1/1     Running   0          16m     10.244.36.94   k8s-node1   <none>           <none>
[root@k8s-master1 pv]# kubectl delete pods pvc-pod
pod "pvc-pod" deleted
[root@k8s-master1 pv]# kubectl get pvc -o  wide
No resources found in default namespace.
[root@k8s-master1 pv]#

三、存储回收

  完成存储卷的使用目标后之后,即可删除PVC对象以便进行资源回收。不过,至于如何操作则取决于pv的回收策略。目前,可用的回收策略有三种:Retain,Recycle(已被废弃)和Delete。

[root@k8s-master1 pv]# kubectl explain pv.spec.persistentVolumeReclaimPolicy
KIND:     PersistentVolume
VERSION:  v1

FIELD:    persistentVolumeReclaimPolicy <string>

DESCRIPTION:
     What happens to a persistent volume when released from its claim. Valid
     options are Retain (default for manually created PersistentVolumes), Delete
     (default for dynamically provisioned PersistentVolumes), and Recycle
     (deprecated). Recycle must be supported by the volume plugin underlying
     this PersistentVolume. More info:
     https://kubernetes.io/docs/concepts/storage/persistent-volumes#reclaiming

1. Retain(留存)

  Retain策略意味着在删除PVC之后,kubernetes系统不会自动删除PV,而仅仅是将它置于“释放”状态。不过,此种状态的PV尚且不能被其他PVC申请所绑定,因为此前申请生成的数据依然存在,需要管理员手动决定其后续处理方案。这就意味着,如果想要再次使用此类的PV资源,则需要管理员按下面步骤手动执行删除操作。

  1)删除PV后,此PV的数据依然留存在外部的存储之上

  2)手工清理存储系统上的依然留存的数据

  3)手动删除存储系统级的存储卷以释放空间,以便再次创建,或者直接将其重建为PV

  查看上述删除名为pvc-test的pvc后,pv的状态

[root@k8s-master1 pv]# kubectl get pvc -o wide
No resources found in default namespace.
# 查看pv的状态变为释放状态
[root@k8s-master1 pv]# kubectl get pv -o wide
NAME             CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS     CLAIM              STORAGECLASS   REASON   AGE   VOLUMEMODE
pv-static-test   1Gi        RWO            Retain           Released   default/pvc-test                           41m   Filesystem
[root@k8s-master1 pv]# cat pv-test.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-static-test
spec:
  capacity:
    storage: 1Gi
  accessModes: ["ReadWriteOnce"]
  nfs:
    path: /data/volume_test/v1
    server: 10.0.0.131

  由于是手动创建的pv,默认的回收策略为Retain,删除与该pv绑定的pvc后,系统不会自动删除pv,而是将pv的状态变为Released状态。

2. Recycle(回收)

  如果可被底层存储插件支持,资源回收策略会在存储卷上执行数据删除操作并让PV资源再次变为可被Claim。但是,此种回收策略已被废弃。

3. Delete(删除)

  对于支持Delete回收策略的存储插件来说,在PVC被删除后会直接移除PV对象,同时移除的还有PV相关的外部存储系统上的存储资产。支持这种操作的存储系统有AWS EBS, GCE PD, Azure Disk, or Cinder。动态创建的PV资源回收策略取决于相关存储类上的定义,存储类上相关的默认策略为Delete,大多数情况下,管理员都需要按用户期望的处理机制修改此默认策略,以免导致数据非计划内的误删除。

  存储类的回收策略默认为Delete:

[root@k8s-master1 storageclass]# kubectl explain storageclass.reclaimPolicy
KIND:     StorageClass
VERSION:  storage.k8s.io/v1

FIELD:    reclaimPolicy <string>

DESCRIPTION:
     Dynamically provisioned PersistentVolumes of this storage class are created
     with this reclaimPolicy. Defaults to Delete.

  创建默认回收策略为Delete的存储类,在此存储类基础上创建pvc,查看pv,storageclass,nfs共享目录存储位置的文件等详细情况如下:

[root@k8s-master1 storageclass]# vim nfs-client-class.yaml
[root@k8s-master1 storageclass]# cat nfs-client-class.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: nfs-storage
provisioner: example.com/nfs
[root@k8s-master1 storageclass]# kubectl apply -f nfs-client-class.yaml
storageclass.storage.k8s.io/nfs-storage created
[root@k8s-master1 storageclass]# kubectl get storageclass -o wide
NAME          PROVISIONER       RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
nfs-storage   example.com/nfs   Delete          Immediate           false                  10s
[root@k8s-master1 storageclass]# vim test-pvc.yaml
[root@k8s-master1 storageclass]# cat test-pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: pvc-nfs-dynamic
spec:
  storageClassName: nfs-storage
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
[root@k8s-master1 storageclass]# kubectl apply -f test-pvc.yaml
persistentvolumeclaim/pvc-nfs-dynamic created
[root@k8s-master1 storageclass]# kubectl get pvc -o wide
NAME              STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE   VOLUMEMODE
pvc-nfs-dynamic   Bound    pvc-b57c0ba1-2547-4b11-b6fc-cff25e2223c9   1Gi        RWX            nfs-storage    9s    Filesystem
[root@k8s-master1 storageclass]# kubectl get pv -o wide
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                     STORAGECLASS   REASON   AGE   VOLUMEMODE
pvc-b57c0ba1-2547-4b11-b6fc-cff25e2223c9   1Gi        RWX            Delete           Bound    default/pvc-nfs-dynamic   nfs-storage             12s   Filesystem
[root@k8s-master1 storageclass]# ll /data/volumes/
total 0
drwxrwxrwx 2 root root 6 Sep 25 00:25 default-pvc-nfs-dynamic-pvc-b57c0ba1-2547-4b11-b6fc-cff25e2223c9
[root@k8s-master1 storageclass]# ll /data/volumes/default-pvc-nfs-dynamic-pvc-b57c0ba1-2547-4b11-b6fc-cff25e2223c9/
total 0
[root@k8s-master1 storageclass]#

  删除创建的pvc,查看pv,storageclass,nfs共享目录存储位置的文件情况

[root@k8s-master1 storageclass]# kubectl delete pvc pvc-nfs-dynamic
persistentvolumeclaim "pvc-nfs-dynamic" deleted
[root@k8s-master1 storageclass]# kubectl get pvc -o wide
No resources found in default namespace.
[root@k8s-master1 storageclass]# kubectl get pv -o wide
No resources found
[root@k8s-master1 storageclass]# kubectl get storageclass -o wide
NAME          PROVISIONER       RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
nfs-storage   example.com/nfs   Delete          Immediate           false                  4m39s
[root@k8s-master1 storageclass]# ll /data/volumes/
total 0
drwxrwxrwx 2 root root 6 Sep 25 00:25 archived-pvc-b57c0ba1-2547-4b11-b6fc-cff25e2223c9
[root@k8s-master1 storageclass]# ll /data/volumes/archived-pvc-b57c0ba1-2547-4b11-b6fc-cff25e2223c9/
total 0
[root@k8s-master1 storageclass]#

  从上述命令结果可以看出,删除pvc后,pv资源自动被删除,同时在nfs服务器上存储的文件也发生了变化,由原来的default-pvc-nfs-dynamic-pvc-b57c0ba1-2547-4b11-b6fc-cff25e2223c9变为了archived-pvc-b57c0ba1-2547-4b11-b6fc-cff25e2223c9。

  定义一个回收策略为Retain的存储类,在此存储类的基础上创建PVC,再创建pod挂载该PVC,查看nfs共享目录存储位置的存储资源情况:

[root@k8s-master1 storageclass]# vim nfs-client-class.yaml
[root@k8s-master1 storageclass]# cat nfs-client-class.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: nfs-storage
provisioner: example.com/nfs
reclaimPolicy: Retain
[root@k8s-master1 storageclass]# kubectl apply -f nfs-client-class.yaml
storageclass.storage.k8s.io/nfs-storage created
[root@k8s-master1 storageclass]# kubectl get storageclass
NAME          PROVISIONER       RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
nfs-storage   example.com/nfs   Retain          Immediate           false                  6s
[root@k8s-master1 storageclass]# vim test-pvc.yaml
[root@k8s-master1 storageclass]# cat test-pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: pvc-nfs-dynamic
spec:
  storageClassName: nfs-storage
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
[root@k8s-master1 storageclass]# kubectl apply -f test-pvc.yaml
persistentvolumeclaim/pvc-nfs-dynamic created
[root@k8s-master1 storageclass]# kubectl get pvc -o wide
NAME              STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE   VOLUMEMODE
pvc-nfs-dynamic   Bound    pvc-1fac1477-5d10-4dc8-9d2a-d70ae6ad2cb9   1Gi        RWX            nfs-storage    6s    Filesystem
[root@k8s-master1 storageclass]# kubectl get pv -o wide
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                     STORAGECLASS   REASON   AGE   VOLUMEMODE
pvc-1fac1477-5d10-4dc8-9d2a-d70ae6ad2cb9   1Gi        RWX            Retain           Bound    default/pvc-nfs-dynamic   nfs-storage             11s   Filesystem
[root@k8s-master1 storageclass]# ll /data/volumes/
total 0
drwxrwxrwx 2 root root 6 Sep 25 00:49 default-pvc-nfs-dynamic-pvc-1fac1477-5d10-4dc8-9d2a-d70ae6ad2cb9
[root@k8s-master1 storageclass]# ll /data/volumes/default-pvc-nfs-dynamic-pvc-1fac1477-5d10-4dc8-9d2a-d70ae6ad2cb9/
total 0
[root@k8s-master1 storageclass]# cat storageclass-pod.yaml
kind: Pod
apiVersion: v1
metadata:
  name: storageclass-pod
spec:
  containers:
  - name: test-pod
    image: nginx:latest
    imagePullPolicy: IfNotPresent
    command:
    - "/bin/sh"
    args:
    - "-c"
    - "echo 'hello' >>/usr/share/nginx/html/index.html && exit 0 || exit 1"
    volumeMounts:
      - name: nfs-pvc
        mountPath: /usr/share/nginx/html
  restartPolicy: "Never"
  volumes:
  - name: nfs-pvc
    persistentVolumeClaim:
      claimName: pvc-nfs-dynamic
[root@k8s-master1 storageclass]# kubectl apply -f storageclass-pod.yaml
pod/storageclass-pod created
[root@k8s-master1 storageclass]# kubectl get pods -o wide
NAME                                     READY   STATUS      RESTARTS   AGE     IP             NODE        NOMINATED NODE   READINESS GATES
nfs-client-provisioner-5d65b75f7-2f7qx   1/1     Running     3          6h41m   10.244.36.87   k8s-node1   <none>           <none>
storageclass-pod                         0/1     Completed   0          9s      10.244.36.98   k8s-node1   <none>           <none>
[root@k8s-master1 storageclass]# ll /data/volumes/default-pvc-nfs-dynamic-pvc-1fac1477-5d10-4dc8-9d2a-d70ae6ad2cb9/
total 4
-rw-r--r-- 1 root root 6 Sep 25 00:49 index.html
[root@k8s-master1 storageclass]# cat /data/volumes/default-pvc-nfs-dynamic-pvc-1fac1477-5d10-4dc8-9d2a-d70ae6ad2cb9/index.html
hello
[root@k8s-master1 storageclass]#

  删除PVC,查看各个资源变化情况:只有PVC状态变为Terminating,其他资源保持正常不变,这是由于系统采用了PVC保护机制。

[root@k8s-master1 storageclass]# kubectl delete pvc pvc-nfs-dynamic
persistentvolumeclaim "pvc-nfs-dynamic" deleted

[root@k8s-master1 storageclass]# kubectl get pvc -o wide
NAME              STATUS        VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE    VOLUMEMODE
pvc-nfs-dynamic   Terminating   pvc-1fac1477-5d10-4dc8-9d2a-d70ae6ad2cb9   1Gi        RWX            nfs-storage    5m2s   Filesystem
[root@k8s-master1 storageclass]# kubectl get pv -o wide
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                     STORAGECLASS   REASON   AGE     VOLUMEMODE
pvc-1fac1477-5d10-4dc8-9d2a-d70ae6ad2cb9   1Gi        RWX            Retain           Bound    default/pvc-nfs-dynamic   nfs-storage             5m11s   Filesystem
[root@k8s-master1 storageclass]# kubectl get pods -o wide
NAME                                     READY   STATUS      RESTARTS   AGE     IP             NODE        NOMINATED NODE   READINESS GATES
nfs-client-provisioner-5d65b75f7-2f7qx   1/1     Running     3          6h45m   10.244.36.87   k8s-node1   <none>           <none>
storageclass-pod                         0/1     Completed   0          4m46s   10.244.36.98   k8s-node1   <none>           <none>
[root@k8s-master1 storageclass]# ll /data/volumes/
total 0
drwxrwxrwx 2 root root 24 Sep 25 00:49 default-pvc-nfs-dynamic-pvc-1fac1477-5d10-4dc8-9d2a-d70ae6ad2cb9
You have new mail in /var/spool/mail/root
[root@k8s-master1 storageclass]# ll /data/volumes/default-pvc-nfs-dynamic-pvc-1fac1477-5d10-4dc8-9d2a-d70ae6ad2cb9/
total 4
-rw-r--r-- 1 root root 6 Sep 25 00:49 index.html
[root@k8s-master1 storageclass]#

  删除pod资源,再次查看各个资源变化情况

[root@k8s-master1 storageclass]# kubectl delete pods storageclass-pod
pod "storageclass-pod" deleted
[root@k8s-master1 storageclass]# kubectl get pvc -o wide
No resources found in default namespace.
[root@k8s-master1 storageclass]# kubectl get pv -o wide
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS     CLAIM                     STORAGECLASS   REASON   AGE     VOLUMEMODE
pvc-1fac1477-5d10-4dc8-9d2a-d70ae6ad2cb9   1Gi        RWX            Retain           Released   default/pvc-nfs-dynamic   nfs-storage             8m16s   Filesystem
[root@k8s-master1 storageclass]# kubectl get storageclass -o wide
NAME          PROVISIONER       RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
nfs-storage   example.com/nfs   Retain          Immediate           false                  9m50s
[root@k8s-master1 storageclass]# ll /data/volumes/
total 0
drwxrwxrwx 2 root root 24 Sep 25 00:49 default-pvc-nfs-dynamic-pvc-1fac1477-5d10-4dc8-9d2a-d70ae6ad2cb9
[root@k8s-master1 storageclass]# ll /data/volumes/default-pvc-nfs-dynamic-pvc-1fac1477-5d10-4dc8-9d2a-d70ae6ad2cb9/
total 4
-rw-r--r-- 1 root root 6 Sep 25 00:49 index.html
[root@k8s-master1 storageclass]# cat /data/volumes/default-pvc-nfs-dynamic-pvc-1fac1477-5d10-4dc8-9d2a-d70ae6ad2cb9/index.html
hello
You have new mail in /var/spool/mail/root
[root@k8s-master1 storageclass]#

  通过上述命令结果可以看到:pvc立即被删除,而PV,storageClass,已经NFS服务器上的共享目录下的数据目录依然存在,这就是使用了Retain回收策略的结果。

四、扩展PVC

  kubernetes自1.8版本起增加了扩展PV空间的特性,截止目前,当下层 StorageClass 的 allowVolumeExpansion 字段设置为 true 时,以下类型的卷支持卷扩展:

  注:此功能仅可用于扩容卷,不能用于缩小卷

  将StorageClass 的 allowVolumeExpansion 字段设置为 true 时,即可动态扩展存储卷空间,允许用户通过编辑相应的 PVC 对象来调整卷大小,即能触发底层PV空间扩展从而带来PVC存储卷的扩展。一般,对于包含文件系统的存储卷来说,只有在有新的pod资源基于读写模式开始使用PVC时才会执行文件系统的大小调整操作。也就是说,如果某被扩展的存储卷已经由pod资源所使用,则需要重建此pod对象才能触发文件系统大小的调整操作。支持空间调整的文件系统仅有XFS和EXT3/EXT4。 

posted @ 2022-09-24 23:44  出水芙蓉·薇薇  阅读(262)  评论(0编辑  收藏  举报