Create and Manage PVC

Objective

This guide provides instructions on how to create and manage Persistent Volume Claim (PVC) for applications running on Volterra vK8s deployments. A PVC provides access to storage resources whose lifecycle is independent of the lifecycle of pods and the applications.

Kubernetes supports the provisioning and consumption of storage resources called as Persistent Volumes (PV) by means of storage classes. The PVC is used by pods to consume the PVs. For more information on PVCs and PVs, see Persistent Volumes.

The following image shows the operational view of PV creation and consumption using a PVC.

PvcOps
Figure: PVC Operational View

Using the instructions provided in this guide, you can create and manage a PVC for an application deployed using the Volterra platform.

Note: For an application to consume storage resources, it needs to be provisioned by the cluster administrator.


Prerequisites

The following prerequisites apply:

  • VES account

  • An application deployed using Volterra vK8s.

    • Note: See Deploy guides to deploy your applications on Volterra network cloud or edge cloud.

Configuration

You can create a PVC and use it in your application deployment using one of the following methods:

  • Create a PVC and add it to your deployment in the Volterra console.
  • Download the kubeconfig of your vK8s deployment from Volterra console and use kubectl to apply PVC and deployment.

Note: In both methods, it is required that first you create vK8s in Volterra console.

Configuration Sequence

Creating PVC and using it in the deployment requires you to perform the following sequence of actions.

Phase Description
Create PVC Create a PVC object specifying the storage requirement and access mode.
Create Deployment Create a vK8s deployment applying the PVC.

Note: You can also update an existing deployment to use a created PVC.


Create PVC and Deployment in Volterra Console

Step 1: Log into the Volterra console and change to a desired namespace or create one where your application needs to be deployed.

Step 2: Select Applications from the configuration menu and Virtual K8s from the options pane. Click on the vK8s object to open its dashboard.

Note: In case vK8s object is not already created, the Add virtual K8s option will be available to create vK8s. See Deploy guides for more information.

Step 3: Select the PVCs tab and click Add PersistentVolumeClaim. The PVC creation form gets loaded.

Step 4: Enter the PVC configuration in the JSON or YAML format. This example shows a sample PVC configuration.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mongodb-pvc          *# The name of your claim—you’ll need this later when using the claim as the pod’s volume.*
  namespace: vk8s-pvc
spec:
  accessModes:
    - ReadWriteOnce          *# You want the storage to support a single
client (performing both reads and writes)*
  resources:
    requests:
      storage: 100Mi        *# Requesting 100M of storage*
  volumeMode: Filesystem

Step 5: Click Save to create the PVC object.

PvcUI
Figure: Create PVC in Volterra Console

Step 6: Select the Deployments tab and click Add Deployment to load the deloyment creation form.

Note: You can also edit an existing deployment by selecting the ... -> Edit option against that deployment.

Step 7: Enter the deployment configuration referening the PVC created in Step 6. Click Save to create deployment.

DeployUI
Figure: Create PVC in Volterra Console

Note: The persistentVolumeClaim section of the deployment configuration references the PVC in the claimName field. Ensure you specify the appropriate PVC object name. For this example, the name of PVC object created in Step 6 is used.


Create PVC and Deployment Using Kubectl

Step 1: Log into the Volterra console and change to the namespace where your vK8s object is created.

Step 2: Select Applications from the configuration menu and Virtual K8s from the options pane. The vK8s object is displayed.

Note: In case vK8s object is not already created, the Add virtual K8s option will be available to create vK8s. See Deploy guides for more information.

Step 3: Click ... -> Download to download the kubeconfig file for your vK8s object. Save the file in your machine.

vK8sKubecfg
Figure: Kubeconfig for the vK8s Object

Step 4: Create a PVC configuration file on your local machine in JSON or YAML format. This example shows a sample PVC configuration.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mongodb-pvc          *# The name of your claim—you’ll need this later when using the claim as the pod’s volume.*
  namespace: vk8s-pvc
spec:
  accessModes:
    - ReadWriteOnce          *# You want the storage to support a single
client (performing both reads and writes)*
  resources:
    requests:
      storage: 100Mi        *# Requesting 100M of storage*
  volumeMode: Filesystem
  

Step 5: Create a deployment configuration file on your local machine in JSON or YAML format. This example shows a sample configuration.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongodb
  namespace: vk8s-pvc
  annotations:
    ves.io/workload-flavor: medium
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mongodb
  template:
    metadata:
      labels:
        app: mongodb
    spec:
      containers:
      - image: mongo
        name: mongodb
        volumeMounts:
        - name: mongodb-data
          mountPath: /data/db
        ports:
        - containerPort: 27017
          protocol: TCP
      volumes:
      - name: mongodb-data
        persistentVolumeClaim:
          claimName: mongodb-pvc *# Referencing the PersistentVolumeClaim by name in the pod volume*

Step 6: Create and export the KUBECONFIG environment variable using the kubeconfig file downloaded in Step 3. This example uses the kubeconfig file downloaded to the Downloads folder.

export KUBECONFIG=~/Downloads/ves_vk8s-pvc_vk8s1.yaml

Step 7: Create PVC and deployment objects using the configuration files created in Step 4 and Step 5 respectively.

kubectl apply -f pvc.yml
kubectl apply -f deployment.yml

Verify PVC Operation

Creating PVC creates the PV and PVC objects. Creating deployment creates a pod which uses pvc for its storage. You can verify the created objects and access the storage resource to execute operations such as read or write as per the configured access mode.

Step 1: Verify the PV information.

kubectl get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                  STORAGECLASS   REASON   AGE
pvc-584c4a18-cf6f-4f4d-8f4a-53828ff2d999   1Gi        RWO            Delete           Bound    vk8s-pvc/mongodb-pvc   standard                3m46s

kubectl describe pv
Name:            pvc-584c4a18-cf6f-4f4d-8f4a-53828ff2d999
Labels:          <none>
Annotations:     pv.kubernetes.io/bound-by-controller: yes
                 pv.kubernetes.io/provisioned-by: kubernetes.io/azure-disk
                 volumehelper.VolumeDynamicallyCreatedByKey: azure-disk-dynamic-provisioner
Finalizers:      [kubernetes.io/pv-protection]
StorageClass:    standard
Status:          Bound
Claim:           vk8s-pvc/mongodb-pvc
Reclaim Policy:  Delete
Access Modes:    RWO
VolumeMode:      Filesystem
Capacity:        1Gi
Node Affinity:   <none>
Message:
Source:
    Type:         AzureDisk (an Azure Data Disk mount on the host and bind mount to the pod)
    DiskName:     kubernetes-dynamic-pvc-584c4a18-cf6f-4f4d-8f4a-53828ff2d999
    DiskURI:      /subscriptions/f68d94a5-1db7-4954-9a79-02b5711cb0a1/resourceGroups/demo-ce/providers/Microsoft.Compute/disks/kubernetes-dynamic-pvc-584c4a18-cf6f-4f4d-8f4a-53828ff2d999
    Kind:         Managed
    FSType:
    CachingMode:  ReadOnly
    ReadOnly:     false
Events:           <none>

Step 2: Verify the PVC information.

kubectl get pvc
NAME          STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mongodb-pvc   Bound                                                      4m16s
kubectl describe pvc
Name:          mongodb-pvc
Namespace:     vk8s-pvc
StorageClass:
Status:        Bound
Volume:
Labels:        <none>
Annotations:   <none>
Finalizers:    [kubernetes.io/pvc-protection]
Capacity:
Access Modes:
VolumeMode:    Filesystem
Mounted By:    mongodb-bcbf9745-6nkzz
Events:        <none>

Step 3: Verify the deployment and pod status.

kubectl get deployment
NAME      READY   UP-TO-DATE   AVAILABLE   AGE
mongodb   1/1     1            1           25m
kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
mongodb-bcbf9745-6nkzz   2/2     Running   0          25m

Step 4: Access your application using the pod information obtained in Step 3. This example opens the MongoDB shell.

kubectl exec -it mongodb-bcbf9745-6nkzz mongo
Defaulting container name to mongodb.
Use 'kubectl describe pod/mongodb-bcbf9745-6nkzz -n vk8s-pvc' to see all of the containers in this pod.
MongoDB shell version v4.2.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("c84807bb-c22b-41c6-9103-6f51f89cd3b2") }
MongoDB server version: 4.2.3
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
    http://docs.mongodb.org/
Questions? Try the support group
    http://groups.google.com/group/mongodb-user
...

> 

Step 5: You can perform database operations to update data to the persistent storage. This samples writes JSON documents to the MongoDB.

> use mystore
switched to db mystore
> db.foo.find()
> db.foo.insert({name:'foo'})
WriteResult({ "nInserted" : 1 })
> db.foo.insert({name:'test'})
WriteResult({ "nInserted" : 1 })
>

Verify the records from the MongoDB shell.

> db.foo.find()
{ "_id" : ObjectId("5e7761abf01f2bc511bd6b8a"), "name" : "foo" }
{ "_id" : ObjectId("5e7761b5f01f2bc511bd6b8b"), "name" : "test" }
>
> exit
bye

Step 6: Delete your deployment and recreate the deployment.

kubectl delete -f deployment.yml
deployment.apps "mongodb" deleted


kubectl get pods
No resources found in vk8s-pvc namespace.
kubectl apply -f deployment.yml
deployment.apps/mongodb created

kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
mongodb-bcbf9745-g6vqv   2/2     Running   0          61s

Step 7: Access the MongoDB shell using the new pod name and verify the records created in Step 5. The records persist during the deployment or pod restarts.


Delete PVC

You can delete PVCs in using the kubectl delete command or from the Volterra console.

  • To delete using kubectl, specify the PVC either by file or by name.
kubectl delete -f <file.yaml>
kubectl delete pvc <pvc-name>
  • To delete from Volterra console, change to the namespace where your application is deployed. Select Applications from the configuration menu and Virtual K8s from the options pane. Click on the vK8s objects and select PVCs tab in the dashboard. Click ... -> Delete for your PVC and confirm deletion.

Delete_pvc_pvc
Figure: Delete PVC in Volterra Console

Note: If you delete a PVC that is in active use by a pod, the PVC is not removed immediately. PVC removal is postponed until the PVC is no longer actively used by any pods. Also, if an admin deletes a PV that is bound to a PVC, the PV is not removed immediately. PV removal is postponed until the PV is no longer bound to a PVC.


Concepts


API References