Kubernetes - Persistent Volume

Kubernetes - Persistent Volume


Article content

Persistent Volume:

Persistent Volumes (PVs) in Kubernetes, provide persistent storage for applications running within a cluster. PVs allow data to persist independently, even when Pods are terminated or rescheduled.

Persistent Volume Claim:

A PVC specifies the desired size and access mode for the storage. When a PVC is created, Kubernetes looks for an available PV that meets its requirements and binds them together.

Now first let us create PV and PVC.

Step 1: Create the Persistent Volume file pv.yml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv
  labels:
    app: pv
spec:
  capacity:
    storage: 5Gi
  accessMode:
    - ReadWriteOnce   
  storageClassName: local-storage
  hostPath:
    path: /data                     


Article content
kubectl apply -f pv.yml        
Article content

Step 2: Create the Persistent Volume Claim file pvc.yml

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

Step 3: Run the below commands to check the status of PV and PVC.

kubectl get pv        
kubectl get pvc        
Article content
Article content

Step 4: Create pod pod.yml

apiVersion: v1
kind: Pod
metadata:
  name: pv-pod
spec:
  containers:
    - name: pv-pod
      image: nginx:latest
      volumeMounts:
        - mountPath: /pod-data
          name: data
  volumes:
    - name: data
      persistentVolumeClaim:
        claimName: pvc        


Article content
kubectl apply -f pod.yml        
Article content

Step 5: Create a test.txt file in the pod-data folder

kubectl exec -it pod/pv-pod -- bash        
Article content

Step 6: Delete the pod and recreate the Pod and see that the test.txt file still persists

kubectl delete pod/pv-pod        
Article content
kubectl apply -f pod.yml        
kubectl exec -it pod/pv-pod -- bash        
Article content


To view or add a comment, sign in

Others also viewed

Explore topics