Working with Persistent Volumes
Description
There are two basic concepts, PV (PersistentVolume) and PVC (PersistentVolumeClaim)
Within the PaaS K8S of our platform, PV is a Cinder Volume, i.e. an ordinary disk in the terminology of our Cloud. Can be created manually by the cluster administrator, or dynamically via PVC.
PersistentVolumeClaim (PVC) is a request to create a PV, when it is created, a PV will be automatically created in the cluster (the corresponding disk will appear in the Disks section of the control panel).
The PersistentVolume mechanism allows you to connect an existing disk to the K8S cluster as a persistent data store.
Example
Let's look at an example. You have an ext4 file system disk with a test_file.txt file. Let's create a PV based on this disk, describing a manifest for it:
--- apiVersion: v1 kind: PersistentVolume metadata: name: pv-test spec: accessModes: - ReadWriteOnce capacity: storage: 8Gi cinder: volumeID: 239e30a9-5a14-41a1-9245-17ace88076a4 fsType: ext4 persistentVolumeReclaimPolicy: Retain storageClassName: dp1 volumeMode: Filesystem
accessModes: ReadWriteOnce - means that this disk can be connected to only one pod (multiple connections to different pods are available only for NFS)
capacity: storage: 8Gi - PV size field, is required and must be equal to the size of the used disk
volumeID - the field where the ID of the used disk is indicated
fsType: ext4 - type of filesystem located on disk
persistentVolumeReclaimPolicy: Retain - PV lifecycle parameter, if the value is Retain, the disk will remain in the project after removing PVC and PV (if the value is Delete, the disk will be deleted)
storageClassName: dp1 - must match the type of disk used
Let's create a PV based on the manifest and check it:
$ kubectl apply -f pv.yaml persistentvolume / pv-test created $ kubectl get pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE pv-test 8Gi RWO Retain Available 20m
Now we need to describe the PVC for this PV
--- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: test-pvc spec: accessModes: - ReadWriteOnce resources: requests: storage: 8Gi storageClassName: dp1 volumeName: pv-test
Size (storage) and type (storageClassName) must match the specified parameters in PV
volumeName: pv-test - here you need to specify the name of the PV created in the previous step
Create PVC
$ kubectl apply -f pvc.yaml persistentvolumeclaim / test-pvc created $ kubectl get pvc NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE test-pvc Bound pv-test 8Gi RWO dp1 22m
Now let's create a manifest for the pod using PVC
--- apiVersion: v1 kind: Pod metadata: name: test-pod spec: volumes: - name: pvc persistentVolumeClaim: claimName: test-pvc containers: - image: nginx name: nginx volumeMounts: - mountPath: / mnt name: pvc
The spec: volumes section describes the disk that will be mounted in our container, here we set its name and through the persistentVolumeСlaim parameter we indicate our created PVC (by name)
The containers: volumeMounts section specifies the disk described in the previous paragraph, the mountPath: / mnt parameter specifies the path where the disk will be mounted.
Create under
$ kubectl apply -f pod_test.yaml pod / centos-pod created $ kubectl get pods NAME READY STATUS RESTARTS AGE test-pod 1/1 Running 0 24m
Now let's connect to the running pod and read the file from the mounted disk
$ kubectl exec -it test-pod - / bin / bash root @ test-pod: / # cat /mnt/test-file.txt Hello World! root @ test-pod: / #
The PersistentVolumeClaim mechanism allows automatic creation of disks if the volumeName parameter is not specified .
Available disc types for PVC can be viewed with the command
$ kubectl get storageclasses
We recommend that you familiarize yourself with more detailed information about Persistent Volumes on the official Kubernetes website .