Persistent volume with Azure Kubernetes Services

On November 16, 2023


What is a Persistent Volume?

• A Persistent Volume (PV) is a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned by a storage class. PVs are cluster resources that can be used by pod containers. Kubernetes abstracts the underlying storage implementation using PVs so that pods can access storage without needing to know the details of the storage infrastructure.

What is a Persistent Volume Claim?

• A Persistent Volume Claim (PVC) is a request for storage by a user or application. PVCs are requests for a particular size and access mode (read-write, read-only, etc.). When a PVC is created, Kubernetes looks for a PV that matches the PVC’s specifications. If a matching PV is found, the PVC is bound to the PV. If a matching PV is not found, Kubernetes dynamically provisions a new PV and binds it to the PVC.

Creating a Persistent Volume with Azure Kubernetes Services

• In Azure Kubernetes Services, there are two ways to create a persistent volume. The first is to manually create a PV resource, and the second is to use a dynamic storage provisioner.

Manual Creation of a Persistent Volume

To create a persistent volume manually, you must first define the storage resource. For example, if you want to use Azure Disk Storage, you can create a PV resource using the following YAML:

 apiVersion: v1
kind: PersistentVolume
metadata:
 name: azure-disk-pv
spec:
 capacity:
   storage: 5Gi
 accessModes:
 - ReadWriteOnce
 azureDisk:
   kind: Managed
   diskName: pv-azure-disk
   diskURI: /subscriptions/your-subscription-id/resourceGroups/your-resource-group/providers/Microsoft.Compute/disks
   /pv-azure-disk
   cachingMode: None




This YAML creates a PV resource called azure-disk-pv with a capacity of 5Gi, an access mode of ReadWriteOnce, and using Azure Disk Storage. The azureDisk section specifies the details of the disk to use, including the disk name, disk URI, and caching mode. The storageClassName field indicates that this PV should be provisioned by a storage class called azure-disk.

Architecture of blog

• Azure Disk or Azure File storage resources are created in Azure.

• A Persistent Volume (PV) specification is created in Kubernetes, which defines the properties of the storage resource, such as its capacity, access mode, and storage class.

• The PV specification is submitted to the Kubernetes API server, which creates the PV object in the cluster.

• Kubernetes schedules pods that require storage, and the pod specification references the PV object that matches the storage requirements.

• When the pod is scheduled onto a node in the cluster, the Kubernetes volume plugin mounts the PV to the pod’s container.

• The pod’s container can now read and write data to the mounted volume, and the data is stored on the underlying Azure Disk or Azure File storage resource.

Persistent volume using Azure Kubernetes Services POC

• Create an Azure Kubernetes Service cluster if you haven’t already.

• Create a storage account in Azure that will be used to store the persistent volume data. You can use Azure Portal or Azure CLI to create the storage account.

• Create a Kubernetes storage class that defines the storage requirements for your application. The storage class defines the storage provisioner and the storage account to be used. Here’s an example YAML file:

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: azurefile
provisioner: kubernetes.io/azure-file
parameters:
  skuName: Standard_LRS
  storageAccount: mystorageaccountname




• Create a persistent volume claim (PVC) that requests the required amount of storage from the storage class you defined in step 3. Here’s an example YAML file:

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mypvc
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: azurefile
  resources:
    requests:
      storage: 5Gi




• Deploy your application that uses the persistent volume and reference the PVC in your deployment YAML file. Here’s an example YAML file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
        - name: myapp
          image: myimage
          volumeMounts:
            - name: mypvc
              mountPath: /data
      volumes:
        - name: mypvc
          persistentVolumeClaim:
            claimName: mypvc




That’s it! Once your application is deployed, it will have access to the persistent volume that you created using Azure Kubernetes Services.

OpenEBS Use cases

Database storage: Databases require persistent storage to maintain data integrity and consistency. AKS can be used to deploy databases such as PostgreSQL, MySQL, or SQL Server, and persistent volumes can be used to store their data. With persistent volumes, data can be preserved even if the database pod is deleted or recreated.

Application data storage: Many applications require persistent storage for their data, such as files or user preferences. With persistent volumes, this data can be stored beyond the lifecycle of the container. This is especially useful for stateful applications such as content management systems or CRMs.

Data backup and recovery: In addition to storing data, persistent volumes can also be used to create backups and enable disaster recovery. By creating snapshots of persistent volumes, it is possible to quickly restore data in case of data loss or system failure.

Analytics and reporting: AKS can be used to deploy analytics and reporting tools such as Apache Spark or Elasticsearch. These tools require large amounts of data storage for their operations, which can be accomplished using persistent volumes.

Machine learning: AKS can be used to deploy machine learning models that require large amounts of data for training and inference. With persistent volumes, data can be stored and accessed by multiple pods, enabling efficient use of resources.

Conclusion

Persistent Volumes and Claims are critical for stateful applications running on Kubernetes. With Azure Kubernetes Services, you can easily create and manage PVs using either manual or dynamic provisioning. By abstracting the underlying storage infrastructure, Kubernetes makes it easier to manage and scale storage resources for your application.


*

*

*

*