Contents

    Container Orchestration · KubernetesIntermediate7 min read

    Last updated ·

    Persistent Volumes & Claims — Practical Lab

    Configure persistent storage for a Kubernetes workload using hostPath, PersistentVolumes, and PersistentVolumeClaims. Practice binding, mounting, and troubleshooting storage lifecycle issues.

    • kubernetes
    • storage
    • persistent-volumes
    • persistent-volume-claims
    • hostpath
    • troubleshooting
    Edit on GitHub

    1. Lab Objectives

    في هذا الـ Lab هننفّذ سيناريو عملي متكامل لإدارة Persistent Storage داخل Kubernetes.

    هنبدأ بتطبيق بيكتب Logs داخل الـ Container، وبعدها هننقل التخزين إلى Volume على الـ Node، ثم نستخدم PV وPVC لإدارة التخزين بصورة منفصلة عن تعريف الـ Pod.

    بنهاية التطبيق هتكون قادر على:

    • فهم مشكلة فقدان البيانات عند حذف الـ Pod.
    • ربط Volume بمسار داخل الـ Container.
    • إنشاء PersistentVolume باستخدام hostPath.
    • إنشاء PersistentVolumeClaim وربطه بالـ PV.
    • تشخيص أسباب بقاء PVC في حالة Pending.
    • استخدام PVC داخل Pod.
    • فهم تأثير Retain عند حذف الـ PVC.
    • تشخيص PVC protection وReleased state.

    I. Lab Workflow

    1. Understand the initial problem — data stored in Container filesystem is not persistent.
    2. Create a Host Volume using hostPath to persist logs on the Node.
    3. Create a PersistentVolume to represent a cluster-wide storage resource.
    4. Create a PersistentVolumeClaim to request storage from the PV.
    5. Use the PVC inside a Pod via persistentVolumeClaim volume type.
    6. Understand the Retain Reclaim Policy and what happens after PVC deletion.
    7. Observe PVC Protection and the Terminating state when a Pod holds the claim.

    2. Understand the Initial Problem

    لدينا Pod اسمه webapp، والتطبيق يكتب Logs في المسار التالي:

    /log/app.log

    في البداية، لا يوجد Volume مخصص لهذا المسار؛ لذلك البيانات موجودة داخل الـ Container filesystem.

    I. Inspect the Pod

    kubectl get pods
    
    kubectl describe pod webapp

    اعرض محتويات ملف الـ Logs:

    kubectl exec webapp -- cat /log/app.log

    يمكنك استخدام -c إذا كان الـ Pod يحتوي على أكثر من Container:

    kubectl exec webapp -c <container-name> -- cat /log/app.log

    II. Why Is This a Problem?

    الـ Container filesystem ليس مكانًا مناسبًا للاحتفاظ بالبيانات التي يجب أن تستمر بعد إعادة إنشاء الـ Pod.

    لو الـ Pod اتحذف واتعمله Recreate، فالبيانات الموجودة داخل الـ Container filesystem لن تكون مضمونة الاستمرار.

    الحل هو فصل مكان تخزين البيانات عن الـ Container filesystem.

    3. Step One — Create a Host Volume

    أول خطوة هي ربط مسار موجود على الـ Node بمسار الـ Logs داخل الـ Container.

    سنستخدم:

    Location Path
    Node /var/log/webapp
    Container /log

    I. Configure the Volume

    نحتاج إلى تعريف جزأين داخل Pod specification:

    1. volumes: يحدد مصدر التخزين.
    2. volumeMounts: يحدد أين سيظهر التخزين داخل الـ Container.
    spec:
      containers:
        - name: webapp
          # Keep the existing image and other settings.
          volumeMounts:
            - name: log-volume
              mountPath: /log
    
      volumes:
        - name: log-volume
          hostPath:
            path: /var/log/webapp
            type: DirectoryOrCreate

    How does it work?

    Node filesystem: /var/log/webapp
            ↕  Volume: log-volume
    Container filesystem: /log

    أي كتابة داخل /log في الـ Container ستصل إلى المسار المرتبط به على الـ Node.

    II. Verify the Result

    kubectl exec webapp -- cat /log/app.log

    وعلى الـ Node:

    sudo cat /var/log/webapp/app.log

    المفترض أن ترى نفس محتوى الـ Logs.

    4. Step Two — Create a PersistentVolume

    الخطوة التالية هي إنشاء PersistentVolume يمثل Storage Resource داخل Kubernetes.

    سننشئ PV بالمواصفات التالية:

    Property Value
    Name pv-log
    Capacity 100Mi
    Access Mode ReadWriteMany
    Reclaim Policy Retain
    Storage Backend hostPath
    Host Path /pv/log

    I. pv.yaml

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: pv-log
    spec:
      capacity:
        storage: 100Mi
    
      accessModes:
        - ReadWriteMany
    
      persistentVolumeReclaimPolicy: Retain
    
      hostPath:
        path: /pv/log
        type: DirectoryOrCreate

    أنشئ الـ PV:

    kubectl apply -f pv.yaml

    تحقق من حالته:

    kubectl get pv

    اعرض التفاصيل:

    kubectl describe pv pv-log

    II. Understand the PV State

    عند إنشاء PV بدون PVC مرتبط به، تكون حالته:

    Available

    أي أن الـ Storage Resource موجود وجاهز ليتم حجزه بواسطة PVC مناسب.

    5. Step Three — Create a PersistentVolumeClaim

    الآن سننشئ PVC ليطلب Storage من Kubernetes.

    سنستخدم المواصفات التالية:

    Property Value
    Name claim-log-1
    Access Mode ReadWriteOnce
    Requested Storage 50Mi

    I. pvc.yaml

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: claim-log-1
    spec:
      accessModes:
        - ReadWriteOnce
    
      resources:
        requests:
          storage: 50Mi

    أنشئ الـ PVC:

    kubectl apply -f pvc.yaml

    تحقق من الحالة:

    kubectl get pvc

    ستجد أن الـ PVC في حالة:

    Pending

    بينما الـ PV ما زال:

    Available

    6. Troubleshooting — Why Is the PVC Pending?

    لدينا:

    Property PV PVC
    Capacity 100Mi 50Mi
    Access Mode ReadWriteMany ReadWriteOnce
    Status Available Pending

    قد تظن أن المشكلة في Capacity، لكن هذا ليس السبب.

    الـ PV لديه 100Mi، وبالتالي يستطيع تلبية طلب 50Mi.

    المشكلة هنا هي اختلاف الـ Access Modes.

    I. Compare the Access Modes

    kubectl describe pv pv-log
    
    kubectl describe pvc claim-log-1

    الـ PV يعلن:

    accessModes:
      - ReadWriteMany

    بينما الـ PVC يطلب:

    accessModes:
      - ReadWriteOnce

    يجب أن تتوافق شروط الـ PVC مع الـ PV حتى يتم الـ Binding.

    II. Fix the PVC

    حدّث الـ PVC ليطلب نفس Access Mode الموجود في الـ PV:

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: claim-log-1
    spec:
      accessModes:
        - ReadWriteMany
    
      resources:
        requests:
          storage: 50Mi

    في هذا المثال، الـ PVC موجود بالفعل. تعديل accessModes في PVC قائم ليس تحديثًا عاديًا مدعومًا؛ يجب إعادة إنشاء الـ PVC بعد التأكد أنه غير مستخدم بواسطة أي Pod.

    احذف الـ PVC القديم:

    kubectl delete pvc claim-log-1

    ثم أنشئه مرة أخرى:

    kubectl apply -f pvc.yaml

    تحقق من النتيجة:

    kubectl get pv,pvc

    المفترض أن يصبح:

    • PV: Bound
    • PVC: Bound

    III. What Happened to the Capacity?

    لاحظ أن الـ PV سعته 100Mi بينما الـ PVC طلب 50Mi.

    عند حدوث Binding، يتم ربط الـ PVC بالـ PV بالكامل.

    قد يظهر في kubectl get pvc أن الـ Capacity المرتبطة بالـ Claim أصبحت 100Mi؛ لأن هذه هي سعة الـ PV المرتبط به، وليس معنى ذلك أن Kubernetes أنشأ PV جديدًا بسعة 50Mi.

    7. Step Four — Use the PVC Inside a Pod

    أصبح لدينا الآن:

    flowchart TD
        A(["PersistentVolume: pv-log"])
        B(["PersistentVolumeClaim: claim-log-1"])
    
        A -->|"Bound"| B
    
        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 pvc
    
        linkStyle default stroke:#a58a64,stroke-width:2px

    الخطوة التالية هي استخدام الـ PVC داخل الـ Pod.

    I. Configure the Pod

    نحتفظ بنفس mountPath:

    volumeMounts:
      - name: log-volume
        mountPath: /log

    لكن بدلًا من تعريف hostPath مباشرة، سنستخدم persistentVolumeClaim.

    volumes:
      - name: log-volume
        persistentVolumeClaim:
          claimName: claim-log-1

    II. Complete Storage Configuration

    apiVersion: v1
    kind: Pod
    metadata:
      name: webapp
    spec:
      containers:
        - name: webapp
          image: <existing-image>
          volumeMounts:
            - name: log-volume
              mountPath: /log
    
      volumes:
        - name: log-volume
          persistentVolumeClaim:
            claimName: claim-log-1

    III. Understand the Complete Storage Flow

    flowchart TD
        A(["Application Writes Logs"]) --> B(["Container: /log"])
        B --> C(["volumeMount: log-volume"])
        C --> D(["Pod Volume"])
        D --> E(["PVC: claim-log-1"])
        E --> F(["PV: pv-log"])
        F --> G(["hostPath: /pv/log"])
    
        classDef app fill:#efe4d1,stroke:#96734b,color:#382d20,stroke-width:2px
        classDef storage fill:#e5d8bd,stroke:#806442,color:#382d20,stroke-width:2px
        classDef backend fill:#d7c5a7,stroke:#806442,color:#382d20,stroke-width:2px
    
        class A,B app
        class C,D,E,F storage
        class G backend
    
        linkStyle default stroke:#a58a64,stroke-width:2px

    لاحظ الفرق بين المسؤوليات:

    • PV: يعرّف Storage Resource.
    • PVC: يطلب ويحجز Storage Resource.
    • Pod: يستخدم الـ PVC من خلال Volume.
    • VolumeMount: يحدد مكان ظهور التخزين داخل الـ Container.

    IV. Verify the Configuration

    kubectl get pv,pvc,pods
    
    kubectl describe pod webapp

    افحص الـ Logs داخل الـ Pod:

    kubectl exec webapp -- cat /log/app.log

    وافحص المسار الذي يستخدمه الـ PV على الـ Node:

    sudo cat /pv/log/app.log

    المفترض أن تكون البيانات موجودة في المكان المرتبط بالـ PV.

    8. Step Five — Understand the Reclaim Policy

    تحقق من Reclaim Policy:

    kubectl get pv pv-log

    أو:

    kubectl describe pv pv-log

    ستجد:

    persistentVolumeReclaimPolicy: Retain

    الـ Reclaim Policy تحدد ما الذي يحدث للـ PV والـ Storage Backend بعد حذف الـ PVC المرتبط به.

    I. Retain

    عند حذف الـ PVC:

    • لا يتم حذف الـ PV تلقائيًا.
    • لا يتم حذف البيانات تلقائيًا من الـ Storage Backend.
    • يدخل الـ PV في حالة Released.
    • لا يصبح متاحًا تلقائيًا لـ PVC جديد.
    PV Lifecycle with Retain
      Bound → Released
      PVC deleted → PV retains its data and binding history

    II. Reclaim Policy Comparison

    Policy Behavior
    Retain يحتفظ بالـ PV والبيانات، ويحتاج Administrator إلى معالجة الـ Volume وإعادة استخدامه يدويًا.
    Delete يحذف الـ PV والـ Storage Backend عند دعم الـ Driver لهذه العملية.
    Recycle Deprecated؛ لا تعتمد عليه في تصميمات Kubernetes الحديثة.

    9. Step Six — PVC Protection

    سنفترض أن الـ Pod webapp ما زال يستخدم claim-log-1.

    حاول حذف الـ PVC:

    kubectl delete pvc claim-log-1

    قد تلاحظ أن الـ PVC يظل في:

    Terminating

    I. Why Does This Happen?

    Kubernetes يستخدم Finalizer اسمه:

    kubernetes.io/pvc-protection

    الغرض منه منع حذف PVC ما زال مستخدمًا بواسطة Pod.

    افحص الـ PVC:

    kubectl describe pvc claim-log-1

    واعرض الـ Finalizers:

    kubectl get pvc claim-log-1 -o yaml

    قد تجد:

    metadata:
      finalizers:
        - kubernetes.io/pvc-protection

    II. Resolve the Issue

    أولًا، احذف الـ Pod الذي يستخدم الـ PVC:

    kubectl delete pod webapp

    بعد اختفاء الـ Pod، يستطيع Kubernetes إكمال حذف الـ PVC.

    تحقق:

    kubectl get pvc

    ثم افحص الـ PV:

    kubectl get pv

    المفترض أن تظهر حالة الـ PV:

    Released

    والسبب هو أن الـ Reclaim Policy مضبوطة على Retain.

    10. Troubleshooting Commands

    الأوامر التالية مفيدة عند تشخيص مشاكل PV وPVC:

    Command Purpose
    kubectl get pv عرض جميع PersistentVolumes
    kubectl get pvc -A عرض PersistentVolumeClaims عبر جميع Namespaces
    kubectl describe pv <name> فحص تفاصيل PV
    kubectl describe pvc <name> فحص تفاصيل PVC
    kubectl get pv,pvc عرض حالات PV وPVC معًا
    kubectl get pod <name> -o yaml فحص Volume وVolumeMount configuration
    kubectl describe pod <name> فحص Events ومشاكل Mounting
    kubectl get events --sort-by=.metadata.creationTimestamp مراجعة الأحداث الأخيرة

    I. Common Problems

    PVC Pending

    أسباب محتملة:

    • لا يوجد PV مناسب.
    • اختلاف Access Modes.
    • الـ PV لا يملك Capacity كافية.
    • اختلاف storageClassName.
    • وجود Selector لا يطابق أي PV.

    Pod Cannot Mount the Volume

    أسباب محتملة:

    • PVC غير موجود في Namespace الخاص بالـ Pod.
    • PVC لم يصل إلى حالة Bound.
    • Storage Driver أو Backend لديه مشكلة.
    • مشاكل في Node أو Volume Mount.

    PVC Stuck in Terminating

    افحص:

    • هل يوجد Pod ما زال يستخدم الـ PVC؟
    • هل يوجد Finalizer يمنع الحذف؟
    • هل عملية الحذف ما زالت قيد التنفيذ؟

    PV Stuck in Released

    مع Retain، هذا سلوك متوقع.

    الـ PV يحتفظ بالـ Storage Resource ولا يصبح Available تلقائيًا بعد حذف الـ PVC.

    11. Key Takeaways

    • البيانات داخل Container filesystem ليست Persistent Storage.
    • hostPath يربط Volume بمسار على Node محددة.
    • volumes يعرّف مصدر التخزين، وvolumeMounts يحدد مكان استخدامه داخل Container.
    • PV يمثل Storage Resource على مستوى الـ Cluster.
    • PVC يمثل طلبًا وحجزًا لمورد التخزين.
    • الـ PVC والـ PV يرتبطان بعلاقة One-to-One.
    • يجب أن تتوافق شروط الـ PVC مع مواصفات PV المناسب.
    • PVC لا يمكن استخدامه بواسطة Pod من Namespace آخر.
    • Retain يحتفظ بالـ PV والبيانات بعد حذف PVC، لكنه لا يعيد PV إلى Available تلقائيًا.
    • kubernetes.io/pvc-protection يمنع حذف PVC ما زال مستخدمًا بواسطة Pod.

    Was this helpful?

    No

    Thanks for the feedback.

    Offline · Reading cached copy