Kubernetes进阶实战读书笔记:持久化存储卷(pvc详解)

一、什么是pvc?pvc字段详解

1、什么是pvc

pvc是存储卷类型的资源、它通过申请占用某个pv而创建,它于pv是一对一的关系、用户无需关心底层实现细节。申请时、用户只需指定目标空间的大小、访问模式、PV标签选择器和STORAGECLASS等相关信息即可

2、pvc的sepc字段详解

pvc的spec字段的可嵌套字段具体如下:

[root@master chapter7]# kubectl explain pvc.spec
KIND:     PersistentVolumeClaim
VERSION:  v1

RESOURCE: spec <Object>

DESCRIPTION:
     Spec defines the desired characteristics of a volume requested by a pod
     author. More info:
     https://kubernetes.io/docs/concepts/storage/persistent-volumes#persistentvolumeclaims

     PersistentVolumeClaimSpec describes the common attributes of storage
     devices and allows a Source for provider-specific attributes

FIELDS:
   accessModes	<[]string>
   #PVC也可以设置访问模式,用于描述用户应用对存储资源的访问权限。其三种访问模式的设置与PV的设置相同。
     AccessModes contains the desired access modes the volume should have. More
     info:
     https://kubernetes.io/docs/concepts/storage/persistent-volumes#access-modes-1

   dataSource	<Object>
     This field can be used to specify either: * An existing VolumeSnapshot
     object (snapshot.storage.k8s.io/VolumeSnapshot - Beta) * An existing PVC
     (PersistentVolumeClaim) * An existing custom resource/object that
     implements data population (Alpha) In order to use VolumeSnapshot object
     types, the appropriate feature gate must be enabled
     (VolumeSnapshotDataSource or AnyVolumeDataSource) If the provisioner or an
     external controller can support the specified data source, it will create a
     new volume based on the contents of the specified data source. If the
     specified data source is not supported, the volume will not be created and
     the failure will be reported as an event. In the future, we plan to support
     more data source types and the behavior of the provisioner may change.

   resources	<Object>
   #描述对存储资源的请求,目前仅支持request.storage的设置,即存储空间大小
     Resources represents the minimum resources the volume should have. More
     info:
     https://kubernetes.io/docs/concepts/storage/persistent-volumes#resources

   selector	<Object>
   #通过对Label  Selector的设置,可使PVC对于系统中已存在的各种PV进行筛选。系统将根据标签选出合适的PV与该PVC进行绑定。选择条件可以使用matchLabels和matchExpressions进行设置,如果两个字段都设置了,则Selector的逻辑将是两组条件同时满足才能完成匹配。
     A label query over volumes to consider for binding.

   storageClassName	<string>
     Name of the StorageClass required by the claim. More info:
     https://kubernetes.io/docs/concepts/storage/persistent-volumes#class-1

   volumeMode	<string>
   #PVC也可以设置存储卷模式,用于描述希望使用的PV存储卷模式,包括文件系统和块设备。
     volumeMode defines what type of volume is required by the claim. Value of
     Filesystem is implied when not included in claim spec.

   volumeName	<string>
   #用于直接指定要绑定的pv的卷名
     VolumeName is the binding reference to the PersistentVolume backing this
     claim.

二、示例演示

1、配置清单

下面的配置清单定义了一个pvc资源示例,其选择pv的挑选机制是使用了标签选择器,适配的标签是:release: "stable"、存储类为slow、这关联到前面创建的pv资源pv-nfs-001

[root@master chapter7]# cat pvc-nfs-0001.yaml 
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-nfs-0001
spec:
  accessModes:
    - ReadWriteMany
  volumeMode: Filesystem
  resources:
    requests:
      storage: 5Gi
  storageClassName: slow
  selector:
    matchLabels:
      release: "stable"

2、创建PVC-NFS并验证

使用资源创建命令完整资源创建、而后即可查看其绑定pv资源的相关信息

[root@master chapter7]# kubectl apply -f pvc-nfs-0001.yaml 
persistentvolumeclaim/pvc-nfs-0001 created 
[root@master chapter7]# kubectl get pvc
NAME           STATUS   VOLUME        CAPACITY   ACCESS MODES   STORAGECLASS   AGE
pvc-nfs-0001   Bound    pv-nfs-0001   5Gi        RWX            slow           2m26s

3、创建PVC-RBD并验证

如果需要绑定此前创建的pv资源pv-rbd-0001、那么创建类似如下的资源配置即可、他将保存于配置文件pvc-rbd-0001

[root@master chapter7]# cat pvc-rbd-0001.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-rbd-0001
spec:
  accessModes:
    - ReadWriteOnce
  volumeMode: Filesystem
  resources:
    requests:
      storage: 2Gi
  storageClassName: fast
  selector:
    matchLabels:
      release: "stable"

运行并验证

[root@master chapter7]# kubectl apply -f pvc-rbd-0001.yaml 
persistentvolumeclaim/pvc-rbd-0001 unchanged
[root@master chapter7]# kubectl get pvc
NAME           STATUS   VOLUME        CAPACITY   ACCESS MODES   STORAGECLASS   AGE
pvc-nfs-0001   Bound    pv-nfs-0001   5Gi        RWX            slow           45m
pvc-rbd-0001   Bound    pv-rbd-0001   2Gi        RWO            fast           13h

创建好pvc资源之后、即可在pod资源中通过persistenVolumeClain存储卷引用它而后挂载于容器中进行数据持久化

4、注意事项

PVC和PV都受限于Namespace,PVC在选择PV时受到Namespace的限制,只有相同Namespace中的PV才可能与PVC绑定。Pod在引用PVC时同样受Namespace的限制,只有相同Namespace中的PVC才能挂载到Pod内。当Selector和Class都进行了设置时,系统将选择两个条件同时满足的PV与之匹配。

另外,如果资源供应使用的是动态模式,即管理员没有预先定义PV,仅通过StorageClass交给系统自动完成PV的动态创建,那么PVC再设定Selector时,系统将无法为其供应任何存储资源。

在启用动态供应模式的情况下,一旦用户删除了PVC,与之绑定的PV也将根据其默认的回收策略“Delete”被删除。如果需要保留PV(用户数据),则在动态绑定成功后,用户需要将系统自动生成PV的回收策略从“Delete”改成“Retain”。

三、在pod中使用PVC

在POD资源中调用PVC资源、只需要在定义volumes时使用persistentVolumeClaim字段嵌套指定两个字段即可、具体如下

1、字段详解

[root@master chapter7]# kubectl explain pod.spec.volumes.persistentVolumeClaim
KIND:     Pod
VERSION:  v1

RESOURCE: persistentVolumeClaim <Object>

DESCRIPTION:
     PersistentVolumeClaimVolumeSource represents a reference to a
     PersistentVolumeClaim in the same namespace. More info:
     https://kubernetes.io/docs/concepts/storage/persistent-volumes#persistentvolumeclaims

     PersistentVolumeClaimVolumeSource references the user's PVC in the same
     namespace. This volume finds the bound PV and mounts that volume for the
     pod. A PersistentVolumeClaimVolumeSource is, essentially, a wrapper around
     another type of volume that is owned by someone else (the system).

FIELDS:
   claimName	<string> -required-
   #需要调用PVC存储卷的名称、PVC卷要与pod在通一个名称空间中
     ClaimName is the name of a PersistentVolumeClaim in the same namespace as
     the pod using this volume. More info:
     https://kubernetes.io/docs/concepts/storage/persistent-volumes#persistentvolumeclaims

   readOnly	<boolean>
   #是否将存储卷强制挂在为指读模式、默认为false
     Will force the ReadOnly setting in VolumeMounts. Default false.

2、配置清单

下面的清单定义了一个pod资源、他是前面章节中直接使用rbd存储的pod资源此处修改为调用了前面刚刚创建的名为pv-rbd-0001的pvc资源

[root@master chapter7]# cat pv-vol-rbd.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: vol-rbd-pod
spec:
  containers:
  - name: redis
    image: redis:4-alpine
    ports:
    - containerPort: 6379
      name: redisport
    volumeMounts:
    - mountPath: /data
      name: redis-rbd-vol
  volumes:
  - name: redis-rbd-vol
  persistentVolumeClaim:
      claimName: pv-rbd-0001

[root@master chapter7]# kubectl apply -f pv-vol-rbd.yaml --validate=false
pod/vol-rbd-pod created
[root@master chapter7]# kubectl get pod
NAME               READY   STATUS    RESTARTS   AGE
pod-example        1/1     Running   1          21d
vol-emptydir-pod   2/2     Running   0          16d
vol-hostpath-pod   1/1     Running   0          16d
vol-rbd-pod        1/1     Running   0          90s

3、持久化测试验证

资源重建后,可通过其命令客户端redis-cli创建测试数据,并手动触发其同步于存储系统中、下面加粗部分的字体为要执行的redis命令

[root@master chapter7]# kubectl exec -it vol-rbd-pod redis-cli
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl kubectl exec [POD] -- [COMMAND] instead.
127.0.0.1:6379> set mykey "hello luoahong.com"
OK
127.0.0.1:6379> get mykey
"hello luoahong.com"
127.0.0.1:6379> BGSAVE
Background saving started
127.0.0.1:6379> exit

为了测试其数据持久化效果、下面删除pod资源vol-rbd-pod ,并于再次重建后检测数据是否依然能访问

[root@master chapter7]# kubectl delete pods vol-rbd-pod 
pod "vol-rbd-pod" deleted
[root@master chapter7]# kubectl apply -f pv-vol-rbd.yaml --validate=false
pod/vol-rbd-pod created
[root@master chapter7]# kubectl get pod
NAME               READY   STATUS    RESTARTS   AGE
pod-example        1/1     Running   1          21d
vol-emptydir-pod   2/2     Running   0          16d
vol-hostpath-pod   1/1     Running   0          16d
vol-rbd-pod        1/1     Running   0          13s

待其重建完成后,通过再一次创建的pod资源的详细信息,我们可以观察到它挂在使用nfs存储的相关信息、接下来再次检查redis-cli中是否保存有此前存储的数据

[root@master chapter7]# kubectl exec -it vol-rbd-pod redis-cli
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl kubectl exec [POD] -- [COMMAND] instead.
127.0.0.1:6379> get mykey
"hello luoahong.com"
posted @ 2020-08-29 07:14  活的潇洒80  阅读(697)  评论(0编辑  收藏  举报