Monday, June 10, 2024


Kubernetes Persistent Volume (PV) is a storage resource in a Kubernetes cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes. Persistent Volumes are designed to provide a more straightforward and efficient way to manage storage resources in a cloud-native environment. Here’s an overview of the key characteristics and functionality of Persistent Volumes in Kubernetes:

How It Works

  • Persistent Volume Claims (PVCs): To use a Persistent Volume, a user must create a Persistent Volume Claim, which specifies the size and access modes (such as ReadWriteOnce, ReadOnlyMany, ReadWriteMany) among other settings. This claim acts as a request for storage, which can then be bound to a suitable Persistent Volume in the cluster.
  • Binding: Once a PVC is made, it is bound to an available and compatible Persistent Volume. This binding is exclusive, depending on the access modes specified, meaning that a PV can only be bound to one PVC at a time, and vice versa.
  • Using the PV: Pods can then use the PVC as a volume. The actual Persistent Volume behind the claim is transparent to the pod, providing a simple and consistent method to connect pods to the storage resources they require.

Persistent Volume Explained

Imagine you're renting an apartment (a pod in Kubernetes) where you can live and store your belongings (data). Normally, when you move out (when the pod is deleted), you'd have to take all your belongings with you, and if you move back in, you'd need to bring everything back or acquire them anew.

In the world of Kubernetes, a Persistent Volume (PV) is like a storage unit separate from your apartment. Even if you move out of your apartment, whatever you keep in that storage unit stays there, undisturbed, until you decide to come back or someone else rents it. This storage unit is not tied to any specific apartment; it's a space where you can securely store things long-term, no matter how often you move or change apartments.

When you first move in, you claim one of these storage units (this is akin to creating a Persistent Volume Claim (PVC) in Kubernetes). This claim is like telling the storage facility, "I need a unit of this size and these access conditions." The facility then finds a unit that fits your needs and reserves it for you (this is the binding process between PVC and PV).

So, in simple terms:

  • A Persistent Volume is like a rented storage unit for your data, separate from your living space but within the same facility, which you can use as long as you need, independent of where you live.
  • This setup ensures that your data—like furniture and personal belongings—is safely stored even if you switch apartments, providing continuity and ease of access whenever you need it.

Create a YAML file for the Persistent Volume Claim

vi pv.yaml


apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: "/mnt/data"

After creating the Persistent Volume, you can create a Persistent Volume Claim (PVC) to use this storage in your pods.

Apply the Configuration

kubectl apply -f pv.yaml


Create a Persistent Volume Claim (PVC) in the dev Namespace

Create a YAML file for the Persistent Volume Claim

vi pvc.yaml

To ensure your data persists even if the pod is deleted or replaced, you need to create a Persistent Volume (PV) and a Persistent Volume Claim (PVC). Below is the YAML configuration to create a Persistent Volume:


apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
  namespace: dev
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

Use the PVC in a Pod within the dev Namespace

Create a YAML file for the Pod vi pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  namespace: dev
spec:
  containers:
    - name: my-container
      image: nginx
      volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: my-storage
  volumes:
    - name: my-storage
      persistentVolumeClaim:
        claimName: my-pvc

kubectl apply -f pod.yaml


List pods

kubectl get pods -n dev


describe my-pod to see PV

kubectl describe pod my-pod -n dev

No comments:

Post a Comment

  PROMETHEUS AND GRAFANA A robust performance monitoring and alerting stack is crucial to service reliability. Cloud Native Computing Foun...