PVC: How to create and bind to a pod

What are PV and PVC?
Create a PVC
• To connect to ‘standard’ disk-type storage
• To connect to ‘ultra’, ‘cold’, or ‘ssd_hiiops’ disk-type storage
Bind a PVC to a pod

What are PV and PVC?

A PV (Persistent Volume) is a Kubernetes resource used to store data. PVs are used by pods but have an independent lifecycle which is specified in the Reclaim Policy. The Reclaim Policy controls whether a PV will continue to exist or will be deleted whenever a pod attached to it gets destroyed.

A PV represents a piece of available storage. When a user wants to bind that piece of storage to a pod, they need to create a persistent volume claim (PVC). It is a request for storage. When a PVC is created, Kubernetes takes the request and forwards it to the Kubernetes component responsible for storage provisioning, i.e., a storage class. A storage class creates PVs automatically in response to a PVC.

Create a PVC

There are four storage classes based on the disk type: standard, ultra, cold, and ssd_hiiops. The standard disk-type storage class exists by default. If you need the standard disk type, you only need to create the default PVC. If you need any other disk type, you first have to create a storage class with the required disk type and then create a PVC.

Create a PVC to connect to a standard disk-type storage class

1. Create a YAML file in any code or text editor to configure a PVC:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: block-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi

Enter your custom values instead:

block-pvc—the PVC name
1Gi—the required storage size

2. Run the kubectl command from the file directory:

kubectl apply -f <name of the YAML file>.yaml

You will get the output:

persistentvolumeclaim/<the name of the created PVC> created

It means you have created a PVC with a standard disk-type storage class. To connect the PVC to your pods, refer to the ‘Bind a PVC to a pod’ section.

Create a PVC to connect to ultra/cold/ssd_hiiops disk-type storage

You can check the available disk types in different regions in the Gcore control panel or via an API request.

1. Create a YAML file in any code or text editor to configure a storage class with the required disk type:

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: csi-sc-cinderplugin-hiiops
annotations:
storageclass.kubernetes.io/is-default-class: "false"
provisioner: cinder.csi.gcorelabs.com
parameters:
type: ssd_hiiops
allowVolumeExpansion: true

Enter your custom values instead:

csi-sc-cinderplugin-hiiops—the storage class name
ssd_hiiops—the required disk type

2. Run the kubectl command from the file directory:

kubectl apply -f <name of the created YAML file>.yaml

You will get the output:

storageclass/<the name of the created storage class> created

3. Сreate a YAML file in any code or text editor to configure a PVC:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: block-pvc
spec:
storageClassName: csi-sc-cinderplugin-hiiops
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi

Enter your custom values instead:

block-pvc—your PVC name
csi-sc-cinderplugin-hiiops—the name of the created storage class
1Gi— the required storage size

4. Run the kubectl command from the file directory:

kubectl apply -f <name of the created YAML file>.yaml

You will get the output:

persistentvolumeclaim/<the name of the created PVC> created

It means you have created a PVC with a storage class of the required disk type. To connect the PVC to your pods, refer to the section below.

Bind your PVC to a pod

1. Create a YAML file in any code or text editor to bind the created storage class to your pod.

apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: myfrontend
image: nginx
volumeMounts:
- mountPath: "/var/www/html"
name: mypd
volumes:
- name: mypd
persistentVolumeClaim:
claimName: block-pvc

Enter your custom values instead:

mypod—the pod name
myfrontend—the container name
“/var/www/html”—the destination inside the pod where to mount the storage class
mypd—the storage class name
block-pvc—the created PVC name

2. Run the kubectl command from the file directory:

kubectl apply -f <name of the created YAML file>.yaml

3. You will get the output:

pod/<the pod name> created

It means you have binded the PVC to your pod, and now its containers can access the storage.

Was this article helpful?
Recently viewed articles