Watch all our Tutorials and Training Videos for Free on our Youtube Channel, Get Online Web Tools for Free on swebtools.com

Search Suggest

How to Create Local Persistent Volume in Kubernetes

How to Create Local Persistent Volume in Kubernetes, , DevOps, Kubernetes, Containerization
How to Create Local Persistent Volume in Kubernetes

Kubernetes is an open-source container orchestration platform that automates deployment, scaling, and management of containerized applications. Persistent volumes (PV) in Kubernetes are used to provide data storage for applications running in containers. Local Persistent Volumes (LPV) provide a way to use local storage devices for persistent storage of data. In this article, we will discuss how to create local persistent volumes in Kubernetes.

Prerequisites:

  • A Kubernetes cluster
  • kubectl command-line tool installed and configured
  • Basic knowledge of Kubernetes objects

Step 1: Create a StorageClass

The first step in creating a local persistent volume is to create a StorageClass. A StorageClass is used to define the properties of a PV that will be created using that StorageClass. Use the following command to create a StorageClass named local-storage:

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer

Step 2: Create a PersistentVolumeClaim

The next step is to create a PersistentVolumeClaim (PVC). A PVC is used to request a specific amount of storage from a PV that matches the properties defined in the StorageClass. Use the following command to create a PVC named local-pvc:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: local-pvc
spec:
storageClassName: local-storage
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi

Step 3: Create a PersistentVolume

The final step is to create a PersistentVolume (PV) that matches the properties defined in the StorageClass. Use the following command to create a PV named local-pv:

apiVersion: v1
kind: PersistentVolume
metadata:
name: local-pv
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: local-storage
local:
path: /mnt/local-pv

In this example, the PV has a capacity of 10Gi, and the storage will be mounted at /mnt/local-pv on the worker node where the pod is scheduled. The access mode is set to ReadWriteOnce, which means that the volume can be mounted as read-write by a single node at a time. The persistentVolumeReclaimPolicy is set to Retain, which means that when the PV is released, the data on the volume will be retained for manual deletion.

More examples:

  • If you want to use a different path for the local storage device, change the path in the local section of the PV manifest.
  • If you want to use a different size for the PV, change the value of the storage property in both the PVC and PV manifests.
  • If you want to use a different access mode, change the accessModes property in both the PVC and PV manifests.

In this article, we discussed how to create a local persistent volume in Kubernetes. By creating a StorageClass, a PVC, and a PV, you can easily provide persistent storage for your Kubernetes applications using local storage devices. This is a useful technique for applications that require high-performance storage or for applications that need to be run on a single node.

Related Searches and Questions asked:

  • How to Configure Event Rate Limit in Kubernetes
  • How to Create Kubernetes Network Policies
  • Get Kubernetes Ingress Log for Debugging
  • How to Configure Deny Service External IPs in Kubernetes
  • That's it for this post. Keep practicing and have fun. Leave your comments if any.