{"id":4035,"date":"2024-05-29T06:55:30","date_gmt":"2024-05-29T06:55:30","guid":{"rendered":"https:\/\/code2deploy.com\/blog\/?p=4035"},"modified":"2024-05-29T06:55:31","modified_gmt":"2024-05-29T06:55:31","slug":"kubernetes-persistent-volumes-a-comprehensive-guide","status":"publish","type":"post","link":"https:\/\/code2deploy.com\/blog\/kubernetes-persistent-volumes-a-comprehensive-guide\/","title":{"rendered":"Kubernetes Persistent Volumes: A Comprehensive Guide"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Persistent Volumes (PVs) are a critical part of Kubernetes for managing durable storage. This blog explains the concepts of PVs, their types, and how to set up <strong>NFS<\/strong> and <strong>AWS EBS-backed PVs.<\/strong> We also highlight the necessary access privileges and detailed steps for setting up and connecting various components.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Steps Overview<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Understanding Persistent Volumes (PVs) and Types<\/strong><\/li>\n\n\n\n<li><strong>Setting Up NFS as a Persistent Volume<\/strong>\n<ul class=\"wp-block-list\">\n<li>Create and configure the NFS Server<\/li>\n\n\n\n<li>Install NFS Client on Kubernetes Nodes<\/li>\n\n\n\n<li>Deploy NFS Provisioner using Helm<\/li>\n\n\n\n<li>Create Persistent Volume Claim (PVC) for NFS<\/li>\n\n\n\n<li>Deploy an application using NFS PVC<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Dynamic AWS EBS Block Storage Provisioning<\/strong>\n<ul class=\"wp-block-list\">\n<li>Prepare AWS StorageClass<\/li>\n\n\n\n<li>Create AWS Secrets for IAM User<\/li>\n\n\n\n<li>Install AWS EBS CSI Driver<\/li>\n\n\n\n<li>Create PVC for EBS<\/li>\n\n\n\n<li>Deploy a Pod using EBS PVC<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Access Privileges and Modes<\/strong>\n<ul class=\"wp-block-list\">\n<li>Access Privileges for NFS and AWS<\/li>\n\n\n\n<li>Volume Binding Modes<\/li>\n\n\n\n<li>Access Modes for Persistent Volumes<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Understanding Persistent Volumes and Types<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Persistent Volumes (PVs)<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A PV is a storage resource provisioned in a cluster. It can be created by an administrator (static provisioning) or dynamically using Storage Classes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Types of Persistent Storage<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Pod-Label Volume<\/strong>: Shared within the same pod but not across multiple pods or nodes. Data is lost if the pod is deleted.<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image\"><img data-dominant-color=\"d0d1d4\" data-has-transparency=\"false\" style=\"--dominant-color: #d0d1d4;\" loading=\"lazy\" decoding=\"async\" width=\"1600\" height=\"788\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" src=\"https:\/\/code2deploy.com\/blog\/wp-content\/uploads\/2024\/05\/image.png\" alt=\"\" class=\"wp-image-4036 not-transparent\" srcset=\"https:\/\/code2deploy.com\/blog\/wp-content\/uploads\/2024\/05\/image.png 1600w, https:\/\/code2deploy.com\/blog\/wp-content\/uploads\/2024\/05\/image-300x148.png 300w, https:\/\/code2deploy.com\/blog\/wp-content\/uploads\/2024\/05\/image-1024x504.png 1024w, https:\/\/code2deploy.com\/blog\/wp-content\/uploads\/2024\/05\/image-768x378.png 768w, https:\/\/code2deploy.com\/blog\/wp-content\/uploads\/2024\/05\/image-1536x756.png 1536w\" \/><\/figure>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>HostPath-Label Volume<\/strong>: Shared among pods within the same node but not across multiple nodes.<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image\"><img data-dominant-color=\"cbccd0\" data-has-transparency=\"false\" style=\"--dominant-color: #cbccd0;\" loading=\"lazy\" decoding=\"async\" width=\"1600\" height=\"787\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" src=\"https:\/\/code2deploy.com\/blog\/wp-content\/uploads\/2024\/05\/image-1.png\" alt=\"\" class=\"wp-image-4037 not-transparent\" srcset=\"https:\/\/code2deploy.com\/blog\/wp-content\/uploads\/2024\/05\/image-1.png 1600w, https:\/\/code2deploy.com\/blog\/wp-content\/uploads\/2024\/05\/image-1-300x148.png 300w, https:\/\/code2deploy.com\/blog\/wp-content\/uploads\/2024\/05\/image-1-1024x504.png 1024w, https:\/\/code2deploy.com\/blog\/wp-content\/uploads\/2024\/05\/image-1-768x378.png 768w, https:\/\/code2deploy.com\/blog\/wp-content\/uploads\/2024\/05\/image-1-1536x756.png 1536w\" \/><\/figure>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li><strong>Node-Label Volume<\/strong>: Can use local or external storage (e.g., NFS, cloud storage). Data remains intact even if the pod, node, or cluster goes down.<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image\"><img data-dominant-color=\"b9bec1\" data-has-transparency=\"false\" style=\"--dominant-color: #b9bec1;\" loading=\"lazy\" decoding=\"async\" width=\"1600\" height=\"789\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" src=\"https:\/\/code2deploy.com\/blog\/wp-content\/uploads\/2024\/05\/image-2.png\" alt=\"\" class=\"wp-image-4038 not-transparent\" srcset=\"https:\/\/code2deploy.com\/blog\/wp-content\/uploads\/2024\/05\/image-2.png 1600w, https:\/\/code2deploy.com\/blog\/wp-content\/uploads\/2024\/05\/image-2-300x148.png 300w, https:\/\/code2deploy.com\/blog\/wp-content\/uploads\/2024\/05\/image-2-1024x505.png 1024w, https:\/\/code2deploy.com\/blog\/wp-content\/uploads\/2024\/05\/image-2-768x379.png 768w, https:\/\/code2deploy.com\/blog\/wp-content\/uploads\/2024\/05\/image-2-1536x757.png 1536w\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Setting Up NFS as a Persistent Volume<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Create and Configure the NFS Server<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Update the System Repository<\/strong>:<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>sudo apt update<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>Install NFS Server<\/strong>:<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>sudo apt install nfs-kernel-server<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li><strong>Create Data Directory<\/strong>:<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>sudo mkdir &#8211;mode=775 \/k8sdata<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li><strong>Configure NFS Exports<\/strong>:<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>sudo vim \/etc\/exports<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following lines:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/k8sdata *(<strong>rw<\/strong>,sync,no_subtree_check)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ol class=\"wp-block-list\" start=\"5\">\n<li><strong>Initialize the Shared Directory<\/strong>:<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>sudo exportfs -a<br>sudo exportfs -r<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ol class=\"wp-block-list\" start=\"6\">\n<li><strong>Restart NFS Server<\/strong>:<br><\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>sudo systemctl restart nfs-kernel-server<br>sudo systemctl status nfs-kernel-server<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Install NFS Client on Kubernetes Nodes<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Install NFS Client<\/strong>:<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>sudo apt install nfs-common -y<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>Verify the Shared Directory<\/strong>:<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>showmount -e [NFS-Server]<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Deploy NFS Provisioner Using Helm<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Visit the repo to get all manifest<strong> <\/strong><a href=\"https:\/\/github.com\/Enamulitc\/k8s-Lab\/tree\/main\/k8s-pv\">https:\/\/github.com\/Enamulitc\/k8s-Lab\/tree\/main\/k8s-pv<\/a><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Install Helm<\/strong>:<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>curl https:\/\/baltocdn.com\/helm\/signing.asc | sudo apt-key add &#8211;<br>sudo apt-get install apt-transport-https &#8211;yes<br>echo &#8220;deb https:\/\/baltocdn.com\/helm\/stable\/debian\/ all main&#8221; | sudo tee \/etc\/apt\/sources.list.d\/helm-stable-debian.list<br>sudo apt-get update<br>sudo apt-get install helm<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>Add NFS Provisioner Repo<\/strong>:<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>helm repo add nfs-subdir-external-provisioner https:\/\/kubernetes-sigs.github.io\/nfs-subdir-external-provisioner\/<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>helm repo update<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li><strong>Create Namespace for NFS<\/strong>:<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>kubectl create ns storagenfs<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li><strong>Deploy NFS Provisioner<\/strong>:<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>helm install nfs-subdir-external-provisioner \\<br>nfs-subdir-external-provisioner\/nfs-subdir-external-provisioner \\<br>&#8211;set nfs.server=[NFS-Server-IP] \\<br>&#8211;set nfs.path=\/k8sdata \\<br>&#8211;set storageClass.onDelete=true -n storagenfs<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ol class=\"wp-block-list\" start=\"5\">\n<li><strong>Verify Deployment<\/strong>:<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>kubectl get pods -n storagenfs<br>kubectl get storageclass<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Create Persistent Volume Claim (PVC) for NFS<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Create PVC Manifest<\/strong>:<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td># 1-sample-nfs-pvc.yaml<br>apiVersion: v1<br>kind: PersistentVolumeClaim<br>metadata:<br>&nbsp; name: sample-nfs-pvc<br>spec:<br>&nbsp; accessModes:<br>&nbsp; &nbsp; &#8211; ReadWriteOnce<br>&nbsp; storageClassName: nfs-client<br>&nbsp; resources:<br>&nbsp; &nbsp; requests:<br>&nbsp; &nbsp; &nbsp; storage: 2Gi<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>Deploy PVC<\/strong>:<br><br><\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>kubectl apply -f 1-sample-nfs-pvc.yaml<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Deploy an Application Using NFS PVC<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Create Service Manifest<\/strong>:<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td># 2-nfs-nginx-svc.yaml<br>apiVersion: v1<br>kind: Service<br>metadata:<br>&nbsp; name: nfs-nginx-svc<br>spec:<br>&nbsp; selector:<br>&nbsp; &nbsp; app: sc-nginx<br>&nbsp; ports:<br>&nbsp; &nbsp; &#8211; protocol: TCP<br>&nbsp; &nbsp; &nbsp; port: 80<br>&nbsp; &nbsp; &nbsp; targetPort: 80<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>Create Deployment Manifest<\/strong>:<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td># nfs-nginx-deployment.yaml<br>apiVersion: apps\/v1<br>kind: Deployment<br>metadata:<br>&nbsp; name: nfs-nginx<br>spec:<br>&nbsp; replicas: 2<br>&nbsp; selector:<br>&nbsp; &nbsp; matchLabels:<br>&nbsp; &nbsp; &nbsp; app: sc-nginx<br>&nbsp; template:<br>&nbsp; &nbsp; metadata:<br>&nbsp; &nbsp; &nbsp; labels:<br>&nbsp; &nbsp; &nbsp; &nbsp; app: sc-nginx<br>&nbsp; &nbsp; spec:<br>&nbsp; &nbsp; &nbsp; volumes:<br>&nbsp; &nbsp; &nbsp; &#8211; name: nfs-test<br>&nbsp; &nbsp; &nbsp; &nbsp; persistentVolumeClaim:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; claimName: sample-nfs-pvc<br>&nbsp; &nbsp; &nbsp; containers:<br>&nbsp; &nbsp; &nbsp; &#8211; image: nginx<br>&nbsp; &nbsp; &nbsp; &nbsp; name: nginx<br>&nbsp; &nbsp; &nbsp; &nbsp; volumeMounts:<br>&nbsp; &nbsp; &nbsp; &nbsp; &#8211; name: nfs-test<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mountPath: \/usr\/share\/nginx\/html<br>&nbsp; &nbsp; &nbsp; &nbsp; ports:<br>&nbsp; &nbsp; &nbsp; &nbsp; &#8211; containerPort: 80<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li><strong>Deploy the Application<\/strong>:<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>kubectl apply -f 2-nfs-nginx-svc.yaml<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>kubectl apply -f nfs-nginx-deployment.yaml<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Dynamic AWS EBS Block Storage Provisioning<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Visit the repo to get all manifest<strong> <\/strong><a href=\"https:\/\/github.com\/Enamulitc\/k8s-Lab\/tree\/main\/k8s-pv\/aws-ebs\">https:\/\/github.com\/Enamulitc\/k8s-Lab\/tree\/main\/k8s-pv\/aws-ebs<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Prepare AWS StorageClass<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Create StorageClass Manifest<\/strong>:<br><br><\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td># 1-storage-class.yaml<br>apiVersion: storage.k8s.io\/v1<br>kind: StorageClass<br>metadata:<br>&nbsp; name: ebs-sc<br>provisioner: ebs.csi.aws.com<br>parameters:<br>&nbsp; type: gp2<br>volumeBindingMode: WaitForFirstConsumer<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>Deploy StorageClass<\/strong>:<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>kubectl apply -f 1-storage-class.yaml<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Create AWS Secrets for IAM User<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Create Secret for AWS Credentials<\/strong>:<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>kubectl create secret generic aws-secret \\<br>&nbsp; &#8211;namespace kube-system \\<br>&nbsp; &#8211;from-literal &#8220;key_id=AWS_ACCESS_KEY_ID&#8221; \\<br>&nbsp; &#8211;from-literal &#8220;access_key=AWS_SECRET_ACCESS_KEY&#8221;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Install AWS EBS CSI Driver<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Add Helm Repo for EBS CSI Driver<\/strong>:<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>helm repo add aws-ebs-csi-driver https:\/\/kubernetes-sigs.github.io\/aws-ebs-csi-driver<br>helm repo update<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>Install EBS CSI Driver<\/strong>:<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>helm upgrade &#8211;install aws-ebs-csi-driver \\<br>&nbsp; &#8211;namespace kube-system \\<br>&nbsp; aws-ebs-csi-driver\/aws-ebs-csi-driver<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li><strong>Verify Installation<\/strong>:<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>kubectl get pods -n kube-system -l app.kubernetes.io\/name=aws-ebs-csi-driver<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Create PVC for EBS<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Create PVC Manifest<\/strong>:<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td># 2-sample-ebs-pvc.yaml<br>apiVersion: v1<br>kind: PersistentVolumeClaim<br>metadata:<br>&nbsp; name: ebs-claim<br>spec:<br>&nbsp; accessModes:<br>&nbsp; &nbsp; &#8211; ReadWriteOnce<br>&nbsp; storageClassName: ebs-sc<br>&nbsp; resources:<br>&nbsp; &nbsp; requests:<br>&nbsp; &nbsp; &nbsp; storage: 1Gi<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>Deploy PVC<\/strong>:<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>kubectl apply -f 2-sample-ebs-pvc.yaml<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Deploy a Pod Using EBS PVC<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Create Pod Manifest<\/strong>:<br><\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td># 3-sample-ebs-pv-pod.yaml<br>apiVersion: v1<br>kind: Pod<br>metadata:<br>&nbsp; name: app<br>spec:<br>&nbsp; containers:<br>&nbsp; &#8211; name: app<br>&nbsp; &nbsp; image: centos<br>&nbsp; &nbsp; command: [&#8220;\/bin\/sh&#8221;]<br>&nbsp; &nbsp; args: [&#8220;-c&#8221;, &#8220;while true; do echo $(date -u) &gt;&gt; \/data\/out.txt; sleep 5; done&#8221;]<br>&nbsp; &nbsp; volumeMounts:<br>&nbsp; &nbsp; &#8211; name: persistent-storage<br>&nbsp; &nbsp; &nbsp; mountPath: \/data<br>&nbsp; volumes:<br>&nbsp; &#8211; name: persistent-storage<br>&nbsp; &nbsp; persistentVolumeClaim:<br>&nbsp; &nbsp; &nbsp; claimName: ebs-claim<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>Deploy the Pod<\/strong>:<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>kubectl apply -f 3-sample-ebs-pv-pod.yaml<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Verification<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Verify StorageClass<\/strong>:<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>kubectl get storageclass<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>Verify PVC<\/strong>:<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>kubectl get pvc<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li><strong>Verify PV<\/strong>:<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>kubectl get pv<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Access Privileges and Modes<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Access Privileges<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>NFS<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>NFS Server<\/strong>: Requires root privileges to set up and configure.<\/li>\n\n\n\n<li><strong>NFS Client<\/strong>: Requires root privileges to install NFS client packages.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>AWS<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>IAM User<\/strong>: Needs specific IAM policies to create and manage EBS volumes.\n<ul class=\"wp-block-list\">\n<li>AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are required for creating Kubernetes secrets.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Volume Binding Modes<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>WaitForFirstConsumer<\/strong>: Volume is provisioned only when a pod using the PVC is scheduled. This helps in ensuring the volume is created in the same zone as the pod.<\/li>\n\n\n\n<li><strong>Immediate<\/strong>: Volume is provisioned as soon as the PVC is created, regardless of whether a pod is scheduled.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Access Modes<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>ReadWriteOnce (RWO)<\/strong>: The volume can be mounted as read-write by a single node.<\/li>\n\n\n\n<li><strong>ReadOnlyMany (ROX)<\/strong>: The volume can be mounted as read-only by many nodes.<\/li>\n\n\n\n<li><strong>ReadWriteMany (RWX)<\/strong>: The volume can be mounted as read-write by many nodes.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Choosing the appropriate access mode and volume binding mode depends on the use case and the application&#8217;s requirements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding and implementing persistent volumes in Kubernetes is crucial for managing storage efficiently. This guide provides detailed steps to set up NFS and AWS EBS-backed PVs, ensuring data persistence across pod restarts and node failures. With the knowledge of access privileges, volume binding modes, and access modes, you can tailor your Kubernetes storage solution to meet your application&#8217;s needs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Persistent Volumes (PVs) are a critical part of Kubernetes for managing durable storage. This blog explains the concepts of PVs, their types, and how to set up NFS and AWS EBS-backed PVs. We also highlight the necessary access privileges and detailed steps for setting up and connecting various components. Steps Overview Understanding Persistent Volumes and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[115,237,216,122,218,223],"tags":[505,497,506,504,501,502,507,503],"class_list":["post-4035","post","type-post","status-publish","format-standard","hentry","category-k8s-kubernetes","category-k8s-storage","category-kubernetes-basic","category-on-prem-cluster","category-pod","category-secrets","tag-ebs-provisioner","tag-k8s-statefulsets","tag-k8s-storage","tag-nfs-provisioner","tag-pv","tag-pvc","tag-stateful-application","tag-storageclass"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Kubernetes Persistent Volumes: A Comprehensive Guide - code2deploy.com<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.code2deploy.com\/kubernetes-persistent-volumes-a-comprehensive-guide\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Kubernetes Persistent Volumes: A Comprehensive Guide - code2deploy.com\" \/>\n<meta property=\"og:description\" content=\"Persistent Volumes (PVs) are a critical part of Kubernetes for managing durable storage. This blog explains the concepts of PVs, their types, and how to set up NFS and AWS EBS-backed PVs. We also highlight the necessary access privileges and detailed steps for setting up and connecting various components. Steps Overview Understanding Persistent Volumes and [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.code2deploy.com\/kubernetes-persistent-volumes-a-comprehensive-guide\/\" \/>\n<meta property=\"og:site_name\" content=\"code2deploy.com\" \/>\n<meta property=\"article:published_time\" content=\"2024-05-29T06:55:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-05-29T06:55:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/blog.code2deploy.com\/wp-content\/uploads\/2024\/05\/image.png\" \/>\n<meta name=\"author\" content=\"enam\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"enam\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blog.code2deploy.com\\\/kubernetes-persistent-volumes-a-comprehensive-guide\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.code2deploy.com\\\/kubernetes-persistent-volumes-a-comprehensive-guide\\\/\"},\"author\":{\"name\":\"enam\",\"@id\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/#\\\/schema\\\/person\\\/e46930c19b999a87f12566fa8357481b\"},\"headline\":\"Kubernetes Persistent Volumes: A Comprehensive Guide\",\"datePublished\":\"2024-05-29T06:55:30+00:00\",\"dateModified\":\"2024-05-29T06:55:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blog.code2deploy.com\\\/kubernetes-persistent-volumes-a-comprehensive-guide\\\/\"},\"wordCount\":1169,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/#\\\/schema\\\/person\\\/e46930c19b999a87f12566fa8357481b\"},\"image\":{\"@id\":\"https:\\\/\\\/blog.code2deploy.com\\\/kubernetes-persistent-volumes-a-comprehensive-guide\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/05\\\/image.png\",\"keywords\":[\"EBS-Provisioner\",\"k8s-StatefulSets\",\"K8s-Storage\",\"NFS-Provisioner\",\"PV\",\"PVC\",\"Stateful Application\",\"Storageclass\"],\"articleSection\":[\"K8s Kubernetes\",\"k8s Storage\",\"Kubernetes Basic\",\"On-Prem Cluster\",\"POD\",\"Secrets\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/blog.code2deploy.com\\\/kubernetes-persistent-volumes-a-comprehensive-guide\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blog.code2deploy.com\\\/kubernetes-persistent-volumes-a-comprehensive-guide\\\/\",\"url\":\"https:\\\/\\\/blog.code2deploy.com\\\/kubernetes-persistent-volumes-a-comprehensive-guide\\\/\",\"name\":\"Kubernetes Persistent Volumes: A Comprehensive Guide - code2deploy.com\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/blog.code2deploy.com\\\/kubernetes-persistent-volumes-a-comprehensive-guide\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/blog.code2deploy.com\\\/kubernetes-persistent-volumes-a-comprehensive-guide\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/05\\\/image.png\",\"datePublished\":\"2024-05-29T06:55:30+00:00\",\"dateModified\":\"2024-05-29T06:55:31+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/blog.code2deploy.com\\\/kubernetes-persistent-volumes-a-comprehensive-guide\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blog.code2deploy.com\\\/kubernetes-persistent-volumes-a-comprehensive-guide\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/blog.code2deploy.com\\\/kubernetes-persistent-volumes-a-comprehensive-guide\\\/#primaryimage\",\"url\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/05\\\/image.png\",\"contentUrl\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/05\\\/image.png\",\"width\":1600,\"height\":788},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/blog.code2deploy.com\\\/kubernetes-persistent-volumes-a-comprehensive-guide\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Kubernetes Persistent Volumes: A Comprehensive Guide\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/\",\"name\":\"code2deploy.com\\\/blog\",\"description\":\"TechOps\",\"publisher\":{\"@id\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/#\\\/schema\\\/person\\\/e46930c19b999a87f12566fa8357481b\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/#\\\/schema\\\/person\\\/e46930c19b999a87f12566fa8357481b\",\"name\":\"enam\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d864e2f082f4499f8f1b33f004ec166eea77b9e94738553b120b6dca2410f203?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d864e2f082f4499f8f1b33f004ec166eea77b9e94738553b120b6dca2410f203?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d864e2f082f4499f8f1b33f004ec166eea77b9e94738553b120b6dca2410f203?s=96&d=mm&r=g\",\"caption\":\"enam\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d864e2f082f4499f8f1b33f004ec166eea77b9e94738553b120b6dca2410f203?s=96&d=mm&r=g\"},\"sameAs\":[\"https:\\\/\\\/code2deploy.com\\\/blog\"],\"url\":\"https:\\\/\\\/code2deploy.com\\\/blog\\\/author\\\/enam\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Kubernetes Persistent Volumes: A Comprehensive Guide - code2deploy.com","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.code2deploy.com\/kubernetes-persistent-volumes-a-comprehensive-guide\/","og_locale":"en_US","og_type":"article","og_title":"Kubernetes Persistent Volumes: A Comprehensive Guide - code2deploy.com","og_description":"Persistent Volumes (PVs) are a critical part of Kubernetes for managing durable storage. This blog explains the concepts of PVs, their types, and how to set up NFS and AWS EBS-backed PVs. We also highlight the necessary access privileges and detailed steps for setting up and connecting various components. Steps Overview Understanding Persistent Volumes and [&hellip;]","og_url":"https:\/\/blog.code2deploy.com\/kubernetes-persistent-volumes-a-comprehensive-guide\/","og_site_name":"code2deploy.com","article_published_time":"2024-05-29T06:55:30+00:00","article_modified_time":"2024-05-29T06:55:31+00:00","og_image":[{"url":"https:\/\/blog.code2deploy.com\/wp-content\/uploads\/2024\/05\/image.png","type":"","width":"","height":""}],"author":"enam","twitter_card":"summary_large_image","twitter_misc":{"Written by":"enam","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.code2deploy.com\/kubernetes-persistent-volumes-a-comprehensive-guide\/#article","isPartOf":{"@id":"https:\/\/blog.code2deploy.com\/kubernetes-persistent-volumes-a-comprehensive-guide\/"},"author":{"name":"enam","@id":"https:\/\/code2deploy.com\/blog\/#\/schema\/person\/e46930c19b999a87f12566fa8357481b"},"headline":"Kubernetes Persistent Volumes: A Comprehensive Guide","datePublished":"2024-05-29T06:55:30+00:00","dateModified":"2024-05-29T06:55:31+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.code2deploy.com\/kubernetes-persistent-volumes-a-comprehensive-guide\/"},"wordCount":1169,"commentCount":0,"publisher":{"@id":"https:\/\/code2deploy.com\/blog\/#\/schema\/person\/e46930c19b999a87f12566fa8357481b"},"image":{"@id":"https:\/\/blog.code2deploy.com\/kubernetes-persistent-volumes-a-comprehensive-guide\/#primaryimage"},"thumbnailUrl":"https:\/\/code2deploy.com\/blog\/wp-content\/uploads\/2024\/05\/image.png","keywords":["EBS-Provisioner","k8s-StatefulSets","K8s-Storage","NFS-Provisioner","PV","PVC","Stateful Application","Storageclass"],"articleSection":["K8s Kubernetes","k8s Storage","Kubernetes Basic","On-Prem Cluster","POD","Secrets"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blog.code2deploy.com\/kubernetes-persistent-volumes-a-comprehensive-guide\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blog.code2deploy.com\/kubernetes-persistent-volumes-a-comprehensive-guide\/","url":"https:\/\/blog.code2deploy.com\/kubernetes-persistent-volumes-a-comprehensive-guide\/","name":"Kubernetes Persistent Volumes: A Comprehensive Guide - code2deploy.com","isPartOf":{"@id":"https:\/\/code2deploy.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/blog.code2deploy.com\/kubernetes-persistent-volumes-a-comprehensive-guide\/#primaryimage"},"image":{"@id":"https:\/\/blog.code2deploy.com\/kubernetes-persistent-volumes-a-comprehensive-guide\/#primaryimage"},"thumbnailUrl":"https:\/\/code2deploy.com\/blog\/wp-content\/uploads\/2024\/05\/image.png","datePublished":"2024-05-29T06:55:30+00:00","dateModified":"2024-05-29T06:55:31+00:00","breadcrumb":{"@id":"https:\/\/blog.code2deploy.com\/kubernetes-persistent-volumes-a-comprehensive-guide\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.code2deploy.com\/kubernetes-persistent-volumes-a-comprehensive-guide\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blog.code2deploy.com\/kubernetes-persistent-volumes-a-comprehensive-guide\/#primaryimage","url":"https:\/\/code2deploy.com\/blog\/wp-content\/uploads\/2024\/05\/image.png","contentUrl":"https:\/\/code2deploy.com\/blog\/wp-content\/uploads\/2024\/05\/image.png","width":1600,"height":788},{"@type":"BreadcrumbList","@id":"https:\/\/blog.code2deploy.com\/kubernetes-persistent-volumes-a-comprehensive-guide\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code2deploy.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Kubernetes Persistent Volumes: A Comprehensive Guide"}]},{"@type":"WebSite","@id":"https:\/\/code2deploy.com\/blog\/#website","url":"https:\/\/code2deploy.com\/blog\/","name":"code2deploy.com\/blog","description":"TechOps","publisher":{"@id":"https:\/\/code2deploy.com\/blog\/#\/schema\/person\/e46930c19b999a87f12566fa8357481b"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code2deploy.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/code2deploy.com\/blog\/#\/schema\/person\/e46930c19b999a87f12566fa8357481b","name":"enam","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d864e2f082f4499f8f1b33f004ec166eea77b9e94738553b120b6dca2410f203?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d864e2f082f4499f8f1b33f004ec166eea77b9e94738553b120b6dca2410f203?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d864e2f082f4499f8f1b33f004ec166eea77b9e94738553b120b6dca2410f203?s=96&d=mm&r=g","caption":"enam"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/d864e2f082f4499f8f1b33f004ec166eea77b9e94738553b120b6dca2410f203?s=96&d=mm&r=g"},"sameAs":["https:\/\/code2deploy.com\/blog"],"url":"https:\/\/code2deploy.com\/blog\/author\/enam\/"}]}},"_links":{"self":[{"href":"https:\/\/code2deploy.com\/blog\/wp-json\/wp\/v2\/posts\/4035","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code2deploy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code2deploy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code2deploy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/code2deploy.com\/blog\/wp-json\/wp\/v2\/comments?post=4035"}],"version-history":[{"count":1,"href":"https:\/\/code2deploy.com\/blog\/wp-json\/wp\/v2\/posts\/4035\/revisions"}],"predecessor-version":[{"id":4039,"href":"https:\/\/code2deploy.com\/blog\/wp-json\/wp\/v2\/posts\/4035\/revisions\/4039"}],"wp:attachment":[{"href":"https:\/\/code2deploy.com\/blog\/wp-json\/wp\/v2\/media?parent=4035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code2deploy.com\/blog\/wp-json\/wp\/v2\/categories?post=4035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code2deploy.com\/blog\/wp-json\/wp\/v2\/tags?post=4035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}