Contents

    Container Orchestration · KubernetesIntermediate4 min read

    Last updated ·

    Persistent Volume Claims (PVC)

    Understand how Kubernetes users request storage, how PVs bind to PVCs, and what happens when claims are deleted.

    • kubernetes
    • storage
    • pvc
    • pv
    • binding
    Edit on GitHub

    1. What is a Persistent Volume Claim?

    في الدرس السابق، عرفنا إن الـ Administrator بيجهز Persistent Volumes.

    لكن الـ Application User مش المفروض يحتاج يعرف كل تفاصيل الـ Storage Backend أو يختار الـ Disk بنفسه.

    بدل كده، بيعمل Persistent Volume Claim (PVC) يطلب فيه الـ Storage اللي محتاجه.

    Persistent Volume Claim

    PVC هو Kubernetes Object بيمثل طلب من User للحصول على Storage بمواصفات معينة، زي الحجم والـ Access Modes والـ Storage Class.

    الفرق الأساسي:

    Persistent Volume (PV) Persistent Volume Claim (PVC)
    Represents available storage Requests storage
    Usually created by Administrator Usually created by Application User
    Cluster-scoped Namespaced
    Defines storage properties Defines required properties

    2. How PV and PVC Work Together

    الـ Administrator بيجهز PVs، والـ User بيعمل PVC.

    Kubernetes بيحاول يعمل Binding بين الـ PVC وواحد من الـ PVs المتاحة بناءً على الـ Requirements.

    flowchart TD
        A(["Administrator Creates PV"]) --> B(["Available PV"])
        C(["User Creates PVC"]) --> D(["PVC Requests Storage"])
        B --> E{"Matching PV Found?"}
        D --> E
        E -->|"Yes"| F(["PVC Bound to PV"])
        E -->|"No"| G(["PVC Pending"])
        G --> H(["New Matching PV Becomes Available"])
        H --> F
    
        classDef admin fill:#e9dfce,stroke:#8b7355,color:#33291f,stroke-width:2px
        classDef storage fill:#d8c6a5,stroke:#8b7355,color:#33291f,stroke-width:2px
        classDef status fill:#c5d0c4,stroke:#667766,color:#263326,stroke-width:2px
    
        class A,C admin
        class B,D,E,H storage
        class F,G status
    
        linkStyle default stroke:#a58a64,stroke-width:2px

    3. PVC Binding Rules

    Kubernetes بيبحث عن PV مناسب للـ PVC بناءً على مجموعة من الخصائص.

    أهمها:

    1. Capacity

    الـ PV لازم تكون سعته مساوية أو أكبر من السعة المطلوبة في الـ PVC.

    مثال:

    PVC requests: 500Mi
    PV capacity: 1Gi

    ممكن يحصل Binding لأن الـ PV عندها Capacity كافية.

    2. Access Modes

    الـ Access Modes المطلوبة في الـ PVC لازم تكون مدعومة من الـ PV.

    مثال:

    # PV
    accessModes:
      - ReadWriteOnce
    # PVC
    accessModes:
      - ReadWriteOnce

    3. Storage Class

    الـ Storage Class بتساعد في تحديد نوع الـ Storage المطلوب.

    الـ PVC ممكن يطلب Storage Class معينة، والـ PV لازم تتوافق مع الطلب.

    4. Volume Mode

    Kubernetes بيدعم نوعين أساسيين:

    Volume Mode Meaning
    Filesystem Volume is presented as a filesystem
    Block Volume is presented as a raw block device

    5. Selector

    ممكن تستخدم selector في PVC لاختيار PVs بناءً على Labels.

    مثال:

    selector:
      matchLabels:
        environment: production

    4. One-to-One Binding

    العلاقة بين PV وPVC هي علاقة One-to-One.

    يعني:

    • PVC واحد يرتبط بـ PV واحدة.
    • PV واحدة لا يتم تقسيمها تلقائيًا بين عدة PVCs.
    • لو PVC طلبت 500Mi واتربطت بـ PV سعتها 1Gi، المساحة المتبقية مش بتتحول تلقائيًا إلى PV أخرى أو Claim جديدة.
    flowchart TD
        A(["PV: 1Gi"]) -->|"Bound"| B(["PVC: 500Mi"])
        C(["PVC: Another Claim"]) --> D(["Cannot Share the Same PV"])
    
        classDef pv fill:#d8c6a5,stroke:#8b7355,color:#33291f,stroke-width:2px
        classDef pvc fill:#c5d0c4,stroke:#667766,color:#263326,stroke-width:2px
    
        class A pv
        class B,C,D pvc
    
        linkStyle default stroke:#a58a64,stroke-width:2px

    5. Creating a PVC

    هنعمل PVC اسمها myclaim تطلب 500Mi وتستخدم ReadWriteOnce.

    I. persistent-volume-claim.yaml

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: myclaim
    spec:
      accessModes:
        - ReadWriteOnce
    
      resources:
        requests:
          storage: 500Mi

    II. Create the PVC

    kubectl apply -f persistent-volume-claim.yaml

    III. Inspect the PVC

    kubectl get pvc

    أو:

    kubectl describe pvc myclaim

    6. Understanding the Pending State

    لو مفيش PV متاحة تطابق شروط الـ PVC، هتفضل في حالة:

    STATUS: Pending

    مثال:

    NAME      STATUS    VOLUME
    myclaim   Pending

    لما تتوفر PV مناسبة، Kubernetes ممكن يعمل Binding تلقائيًا.

    مهم: الـ PVC ممكن تفضل Pending لأسباب مختلفة، مش بس عدم وجود PV. مثلًا:

    • مفيش PV مطابقة.
    • الـ Storage Class المطلوبة مش موجودة.
    • Dynamic Provisioning مش قادر ينشئ Storage.
    • الـ Access Modes أو الـ Volume Mode غير متوافقة.

    7. Inspecting the Binding

    مثال:

    PV:
      Capacity: 1Gi
      Access Mode: ReadWriteOnce
    
    PVC:
      Requested: 500Mi
      Access Mode: ReadWriteOnce

    لو مفيش شروط أخرى تمنع الـ Binding، ممكن Kubernetes يربط الـ PVC بالـ PV.

    افحص النتيجة:

    kubectl get pv
    kubectl get pvc

    هتلاقي الـ PVC بقت Bound، والـ PV كمان بقت Bound.

    وتقدر تعرف الـ PV المرتبطة بالـ PVC من:

    kubectl get pvc myclaim

    8. Using a PVC Inside a Pod

    بعد ما الـ PVC ترتبط بـ PV، تقدر تستخدمها داخل Pod عن طريق persistentVolumeClaim.

    I. pod.yaml

    apiVersion: v1
    kind: Pod
    metadata:
      name: app
    spec:
      containers:
        - name: app
          image: nginx:1.29-alpine
          volumeMounts:
            - name: app-storage
              mountPath: /usr/share/nginx/html
    
      volumes:
        - name: app-storage
          persistentVolumeClaim:
            claimName: myclaim

    لاحظ الفرق:

    • persistentVolumeClaim بيحدد الـ PVC اللي الـ Pod هتستخدمها.
    • claimName هو اسم الـ PVC.
    • volumeMounts بيحدد مكان ظهور الـ Storage داخل الـ Container.
    flowchart TD
        A(["Pod"]) --> B(["Volume"])
        B --> C(["PVC: myclaim"])
        C --> D(["PV: pv-vol1"])
        D --> E(["Storage Backend"])
    
        classDef pod fill:#e9dfce,stroke:#8b7355,color:#33291f,stroke-width:2px
        classDef claim fill:#d8c6a5,stroke:#8b7355,color:#33291f,stroke-width:2px
        classDef backend fill:#c5d0c4,stroke:#667766,color:#263326,stroke-width:2px
    
        class A,B pod
        class C,D claim
        class E backend
    
        linkStyle default stroke:#a58a64,stroke-width:2px

    9. What Happens When a PVC Is Deleted?

    السؤال المهم: لما نحذف PVC، هل الـ PV والبيانات اللي جواها بيتم حذفهم تلقائيًا؟

    الإجابة بتعتمد على:

    Persistent Volume Reclaim Policy

    الـ Reclaim Policy بتحدد Kubernetes يتعامل إزاي مع الـ PV بعد ما الـ PVC المرتبطة بيها تتحذف.

    1. Retain

    persistentVolumeReclaimPolicy: Retain
    • الـ PV تفضل موجودة بعد حذف الـ PVC.
    • البيانات لا يتم حذفها تلقائيًا بواسطة Kubernetes.
    • الـ PV تدخل غالبًا في حالة Released.
    • الـ Administrator مسؤول عن التعامل مع الـ Storage وتجهيزها للاستخدام مرة أخرى.

    Retain — Data retained

    The PV and its data are preserved after the PVC is deleted. Manual administrator action is required to reuse the volume.

    2. Delete

    persistentVolumeReclaimPolicy: Delete
    • عند تحرير الـ PV بعد حذف الـ PVC، Kubernetes يطلب حذف الـ Storage Resource من خلال الـ Provisioner أو Driver المسؤول.
    • الـ PV Object والـ Backing Storage ممكن يتم حذفهم.

    Delete — Storage deletion requested

    When the PVC is deleted, Kubernetes requests the backing storage resource to be deleted through the Provisioner or Driver.

    3. Recycle

    كانت Recycle بتعتمد على تنظيف البيانات من الـ Volume قبل إتاحتها مرة أخرى.

    لكنها Deprecated ولم تعد مدعومة كـ Reclaim Policy صالحة للاستخدام في Kubernetes الحديثة.

    استخدم Retain أو Delete حسب الـ Requirement.

    10. Reclaim Policy Comparison

    Policy Behavior
    Retain Keeps the storage for manual recovery or cleanup
    Delete Requests deletion of the PV and backing storage when supported
    Recycle Deprecated legacy behavior

    11. Practical Lab

    هنستخدم الـ PV اللي أنشأناها في الدرس السابق، واللي سعتها 1Gi.

    1. Check Existing PV

      kubectl get pv

      المفروض تلاقي PV في حالة Available.

    2. Create the PVC

      kubectl apply -f persistent-volume-claim.yaml
    3. Check PVC Status

      kubectl get pvc
    4. Inspect PV and PVC

      kubectl describe pv pv-vol1
      kubectl describe pvc myclaim
    5. Verify the Binding

      kubectl get pv,pvc

      اتأكد إن:

      • PVC status is Bound.
      • PV status is Bound.
      • الـ PV مرتبطة بالـ PVC المطلوبة.
    6. Delete the PVC

      kubectl delete pvc myclaim
    7. Inspect the PV Again

      kubectl get pv

      لو الـ Reclaim Policy هي Retain، غالبًا هتلاقي الـ PV في حالة Released بدل Available.

    12. Troubleshooting PVCs

    Symptom Possible Cause What to Check
    PVC Pending No matching PV kubectl get pv
    PVC Pending StorageClass mismatch kubectl describe pvc
    PVC Pending Requested capacity too large PV capacity
    PVC Pending Access Mode mismatch PV and PVC access modes
    PV Released Previous PVC was deleted Reclaim Policy
    Pod Pending PVC is not Bound kubectl describe pod

    Useful commands:

    kubectl get pv
    kubectl get pvc
    kubectl describe pvc myclaim
    kubectl describe pv pv-vol1
    kubectl describe pod app

    13. Key Takeaways

    • PVC represents a request for storage.
    • PV represents the actual Kubernetes storage resource.
    • Kubernetes binds PVCs to matching PVs.
    • Binding is one-to-one.
    • A PVC can remain Pending until suitable storage is available.
    • A Pod consumes storage through a PVC.
    • Reclaim Policy controls what happens after a claim is released.
    • Retain preserves the storage for manual handling.
    • Delete requests deletion of the storage resource.
    • Recycle is deprecated.

    Was this helpful?

    No

    Thanks for the feedback.

    Offline · Reading cached copy