\\\n## **What Are Kubernetes Persistent Volumes?**\n\nOrdinarily, containers in a Kubernetes cluster are immutable. This means that when you shut down a container, any information stored within it is lost. This is suitable for stateless applications, but in order to run stateful applications like databases in Kubernetes, you will need persistent storage.\n\n\\\nTo enable persistent storage that survives the shutdown of a container or pod, Kubernetes provides a persistent volumes subsystem. It provides API objects to let you allocate and manage storage resources persistently. \\n \n\nYou can set up persistent storage using the following API resources: \\n \n\n* **PersistentVolume (PVs)**—each PV represents a storage resource available within the cluster. Unlike ephemeral volumes, the lifecycle of a PV is independent of any pod using it. This ensures that the storage remains persistent. Administrators can provision PVs manually. Kubernetes can also dynamically provision PVs using storage classes.\n* **PersistentVolumeClaims (PVCs)**—each PVC represents a request for storage. A PVC can consume only a PV resource that meets its size and access modes requirements. If a relevant PV is available, it is bound to the PVC. Otherwise, Kubernetes dynamically provisions a PV or the request fails.\n\n\\\nFor more background see this __[blog about Kubernetes Persistent Volumes](https://cloud.netapp.com/blog/kubernetes-persistent-storage-why-where-and-how)__.\n\n## Kubernetes Storage Concepts\n\n### Kubernetes Storage Class\n\nKubernetes lets administrators define __[StorageClasses](https://kubernetes.io/docs/concepts/storage/storage-classes/)__ that are abstracted from the storage implementation. It enables users to request the specific StorageClass relevant to each PVC without specifying all storage requirements.\n\n\\\nAdministrators can create various types of StorageClasses, each defined differently for the provisioner, parameters, and reclaimPolicy. It lets you set up StorageClasses that provide different performance, such as HDD or SSD, costs, and applications.\n\nThe provisioner lets you tie a StorageClass to specific backends, such as AWS EBS or Azure Files, and a storage endpoint. Additional parameters are available according to the chosen provisioner to help you describe the volumes for each StorageClass using parameters like `type`, `zone`, `server`, or `path`.\n\n### Kubernetes StatefulSet\n\nA __[Kubernetes StatefulSet](https://spot.io/resources/kubernetes-architecture/kubernetes-statefulsets-scaling-managing-persistent-apps/)__ is a workload API object for managing stateful applications. It lets you define a set of pods with unique network identifiers to be deployed and scaled in a specific order. StatefulSets help manage persistent storage across a Kubernetes cluster.\n\n\\\nPods associated with a StatefulSet are assigned a unique identity comprised of stable storage, an ordinal, and a stable network identity. This identifier sticks to the pods and the StatefulSet maintains it across scheduling and rescheduling. Learn more in this __[blog about Kubernetes networking](https://www.tigera.io/learn/guides/kubernetes-networking/)__.\n\n### Kubernetes CSI Plugins\n\nContainer Storage Interface (CSI) plugins help make third-party storage available for your Kubernetes cluster. It helps introduce persistent storage to containerized applications. CSI plugins receive PVC requests that specify the third-party vendor as a provisioner and then create and mount the requested volume.\n\n## Lifecycle of PVs and PVCs\n\nA PVC is a request for a PV that exists as a storage resource within a Kubernetes cluster. A PVC is tied to a matching PV to ensure storage remains persistent. As a result, the lifecycle of PVs and PVCs is usually tightly connected.\n\n### Provisioning\n\nKubernetes provides the following types of provisioning for persistent storage:\n\n\\\n* **Static**—this type of provisioning is available for cluster administrators. It enables admins to manually create PV resources and make them available to all cluster users. Once the PV exists within the Kubernetes API, users can create a PVC request for a pod to consume a PV resource.\n* **Dynamic**—this type of provisioning is available for a Kubernetes cluster. It enables the cluster to automatically provision a PV to match a PVC according to predefined StorageClasses. Dynamic provisioning typically occurs when non of the PVs statically provisioned match a PVC.\n\n### Binding\n\nA control loop monitors new PVCs. Once a user creates a new PVC, the control loop searches for a marching PV that meets the PVC requirements. It looks to satisfy the requested amount of storage and may do so using a PV that exceeds the request. Here is how this process works:\n\n\\\n* If the control loop finds a matching PV, it binds it to the PVC. Binding ensures that the PV and PVC remain exclusive.\n* If the control loop cannot find a PV that meets the PVC requirements for access mode and storage size, it dynamically provisions a new PV according to the StorageClass and binds it to the PVC.\n* If no PV is available for a PVC—static or dynamic—the PVC remains unbound indefinitely.\n\n### Using\n\nKubernetes pods use PVCs as volumes. Users can specify an access mode for the PVC, and the cluster mounts the bound PV to the pod. When using volumes that support multiple access modes, the user must specify the desired mode. Once a user gets a bound PVC, the bound PV also belongs to that user.\n\n### Reclaiming\n\nOnce a user no longer needs to use a volume, it can be deleted or retained. Users can delete a PVC object directly from the API that enables them to reclaim this resource. Otherwise, the PV’s reclaim policy defines what the cluster should do with a volume that was released of its claim.\n\n## Quick Tutorial: Working with Persistent Volumes\n\nThis tutorial is adapted from the full persistent storage tutorial in the __[Kubernetes documentation](https://minikube.sigs.k8s.io/docs/)__.\n\n\\\nBefore starting this tutorial, set up a Kubernetes cluster on your local machine with only one node. Ensure that kubectl is installed on your machine and communicating successfully with the cluster. You can easily create a one-node cluster with __[Minikube](https://minikube.sigs.k8s.io/docs/)__.\n\n### Step 1: Create a File On the Node\n\nBecause we are working with persistent storage, we need to create a static file and demonstrate that this file persists even after we shut down a node.\n\n\\\nOpen a shell to your node, create a directory named `/mnt/data`, and inside it create an `index.html` file with the text “Hello World Persistent Volume”.\n\n### Step 2: Create a Persistent Volume\n\nTo create a persistent volume, open a text editor and copy-paste the YAML code below.\n\n\\\n`apiVersion: v1`\n\n`kind: PersistentVolume`\n\n`metadata:`\n\n` name: my-pv-volume`\n\n` labels:`\n\n` type: local`\n\n`spec:`\n\n` storageClassName: manual`\n\n` capacity:`\n\n` storage: 6Gi`\n\n` accessModes:`\n\n` —ReadWriteOnce`\n\n` hostPath:`\n\n` path: "/mnt/data"`\n\n\\\nSave the YAML file as `my-pv-volume.yaml`in your local directory. Deploy the persistent volume by running this command: \\n \n\n`kubectl create -f my-pv-volume.yaml`\n\n### Step 3: Create a Persistent Volume Claim\n\nCreate a PVC specification by opening your text editor and copying the YAML code below:\n\n\\\n`apiVersion: v1`\n\n`kind: PersistentVolumeClaim`\n\n`metadata:`\n\n` name: my-pv-claim`\n\n`spec:`\n\n` storageClassName: manual`\n\n` accessModes:`\n\n` —ReadWriteOnce`\n\n` resources:`\n\n` requests:`\n\n` storage: 2Gi`\n\n\\\nSave the file in your local directory as `my-pv-claim.yaml`. Deploy it using this command:\\*\\* \\n \n\n`kubectl apply -f my-pv-claim.yaml`\n\n### Step 4: Create a Pod and Mount the PVC as a Volume\n\nNow it gets interesting—let’s create a pod that mounts this PVC so it can make use of our persistent volume. Copy the following code to your text editor:\n\n\\\n`apiVersion: v1`\n\n`kind: Pod`\n\n`metadata:`\n\n` name: my-pv-pod`\n\n`spec:`\n\n` volumes:`\n\n` —name: my-pv-storage`\n\n` persistentVolumeClaim:`\n\n` claimName: my-pv-claim`\n\n` containers:`\n\n` —name: my-pv-container`\n\n` image: nginx`\n\n` ports:`\n\n` —containerPort: 80`\n\n` name: "http-server"`\n\n` volumeMounts:`\n\n` —mountPath: "/usr/share/nginx/html"`\n\n` name: my-pv-storage`\n\n\\\nNotice that the pod specifies the PVC, it does not directly reference the PV. Kubernetes is responsible for matching this pod with a PV that meets its requirements, based on the information in the PVC. In our case it is simple because there is only one PV.\n\n\\\nSave the pod spec in your local directory as `my-pv-pod.yaml`. Deploy the pod by running this command:\n\n\\\n`kubectl apply -f my-pv-pod.yaml`\n\n\\\nEnsure the pod is running:\n\n\\\n`kubectl get pod my-pv-pod`\n\n### Step 5: Verify that the Pod Has Access to Persistent Storage\n\nLet’s see if our pod can access the file we saved in persistent storage at the beginning of the tutorial. The important thing here is that **any** pod you create using this specification will have access to the same storage—even after you shut down or delete the pod, the file will persist.\n\n\\\nRun this command to bash into the container:\n\n\\\n`kubectl exec -it my-pv-pod -- /bin/bash`\n\n \\n Once you are running in the root shell of your container, run these commands:\n\n\\\n`apt update`\n\n`apt install curl`\n\n`curl http://localhost/`\n\n\\\nYou should see the content of your index.html files:\n\n\\\n`Hello World Persistent Volume`\n\n \\n Congrats! You just created a pod that has access to a Persistent Volume via a Persistent Volume Claim.\n\n## Conclusion\n\nIn this article, I explained the basics of Kubernetes persistent storage, explained the concepts of Persistent Volumes and Persistent Volume Claims, and showed how to set up a quick demo of a pod that mounts a PVC:\n\n\\\n1\\. Create a one-node cluster using Minikube.\n\n2\\. Create an HTML file on the node.\n\n3\\. Create a Persistent Volume YAML file and deploy it using `kubectl`.\n\n4\\. Create a Persistent Volume Claim YAML file and deploy it using `kubectl`.\n\n5\\. Create a Pod YAML which mounts the PVC as a volume, and deploy it using `kubectl`.\n\n6\\. Bash into the container and use curl to view the HTML file from step 2—this proves that your container has access to a persistent volume.\n\n\\\nI hope this will be useful as you take your first steps in Kubernetes storage management.