انتقل إلى المحتوى الرئيسي

المختبر 6.2 - PersistentVolumes و PersistentVolumeClaims

أهداف المختبر​

في نهاية هذا المختبر، ستكون قادرًا على:

  • إنشاء PersistentVolume (PV) يدويًا.
  • إنشاء PersistentVolumeClaim (PVC).
  • ربط PVC بـ Pod.
  • فهم دورة حياة PV و PVC.
  • التحقق من استمرارية البيانات.

المدة المقدرة​

45-60 دقيقة

المتطلبات المسبقة​

  • تثبيت وتكوين kubectl.
  • تشغيل عنقود Kubernetes محلية (minikube أو kind).
  • معرفة بـ PersistentVolumes و PVCs (الفصل 6.2).

الجزء 1: إنشاء PersistentVolume​

سنقوم بإنشاء PersistentVolume يدويًا. بالنسبة لمجموعة محلية (minikube/kind)، سنستخدم حجم من نوع hostPath الذي يركب دليل من العقدة.

الخطوة 1.1: إنشاء PersistentVolume​

قم بإنشاء ملف pv-example.yaml:

# pv-example.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-example
labels:
type: local
spec:
storageClassName: manual # Nom de la StorageClass (ou "" pour pas de classe)
capacity:
storage: 1Gi # Taille du volume
accessModes:
- ReadWriteOnce # RWO: un seul nœud peut monter en lecture-écriture
persistentVolumeReclaimPolicy: Retain # Que faire quand le PVC est supprimé: Retain, Delete, Recycle
hostPath:
path: /tmp/k8s-pv-example # Chemin sur le nœud (pour minikube/kind)
type: DirectoryOrCreate # Créer le répertoire s'il n'existe pas

التفسير:

  • storageClassName: manual: يشير إلى أن هذا PV يتم توفيره يدويًا (ليس بواسطة StorageClass).
  • accessModes: ReadWriteOnce: يمكن تركيب الحجم للقراءة والكتابة بواسطة عقدة واحدة فقط في كل مرة.
  • persistentVolumeReclaimPolicy: Retain: عندما يتم حذف PVC، يتم الاحتفاظ بـ PV (لا يتم حذف البيانات).
  • hostPath: بالنسبة لمجموعة محلية، نستخدم دليل من العقدة. في الإنتاج، ستستخدم أحجام سحابية (EBS، Azure Disk، GCE Persistent Disk) أو NFS.

قم بتطبيق PersistentVolume:

kubectl apply -f pv-example.yaml

تحقق من إنشاء PV:

kubectl get pv pv-example

يجب أن ترى PV بحالة Available (متاح، لم يتم ربطه بعد بـ PVC).

تحقق من التفاصيل:

kubectl describe pv pv-example

الجزء 2: إنشاء PersistentVolumeClaim​

يطلب PVC حجمًا بخصائص محددة (الحجم، وضع الوصول، StorageClass).

الخطوة 2.1: إنشاء PersistentVolumeClaim​

قم بإنشاء ملف pvc-example.yaml:

# pvc-example.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-example
spec:
storageClassName: manual # Doit correspondre au storageClassName du PV
accessModes:
- ReadWriteOnce # Doit correspondre ou être un sous-ensemble des accessModes du PV
resources:
requests:
storage: 500Mi # Demande 500 MiB (le PV a 1 GiB, donc ça fonctionne)

التفسير:

  • storageClassName: manual: يبحث PVC عن PV بهذه StorageClass.
  • accessModes: ReadWriteOnce: يطلب PVC حجمًا بهذا وضع الوصول.
  • resources.requests.storage: 500Mi: يطلب PVC على الأقل 500 ميجابايت.

قم بتطبيق PersistentVolumeClaim:

kubectl apply -f pvc-example.yaml

تحقق من إنشاء PVC وربطه بـ PV:

kubectl get pvc pvc-example

يجب أن ترى PVC بحالة Bound (مرتبط بـ PV).

تحقق من أن PV الآن مرتبط:

kubectl get pv pv-example

يجب أن تكون الحالة Bound ويجب أن تشير العمود CLAIM إلى default/pvc-example.


الجزء 3: استخدام PVC في Pod​

الآن، سنقوم بإنشاء Pod يستخدم PVC لتخزين البيانات المستمرة.

الخطوة 3.1: إنشاء Pod مع PVC​

قم بإنشاء ملف pod-with-pvc.yaml:

# pod-with-pvc.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-with-pvc
spec:
containers:
- name: app-container
image: busybox
command: ["/bin/sh", "-c"]
args: ["echo 'Données persistantes - $(date)' >> /data/persistent-data.txt && cat /data/persistent-data.txt && sleep 3600"]
volumeMounts:
- name: persistent-storage
mountPath: /data # Chemin où le volume sera monté dans le conteneur
volumes:
- name: persistent-storage
persistentVolumeClaim:
claimName: pvc-example # Nom du PVC à utiliser

قم بتطبيق Pod:

kubectl apply -f pod-with-pvc.yaml

انتظر حتى يكون Pod في حالة Running:

kubectl get pods pod-with-pvc

تحقق من السجلات لرؤية البيانات المكتوبة:

kubectl logs pod-with-pvc

يجب أن ترى الرسالة مع التاريخ.

الخطوة 3.2: التحقق من الاستمرارية​

احذف Pod:

kubectl delete pod pod-with-pvc

قم بإنشاء Pod جديد يقرأ البيانات:

# pod-read-pvc.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-read-pvc
spec:
containers:
- name: app-container
image: busybox
command: ["/bin/sh", "-c"]
args: ["cat /data/persistent-data.txt && sleep 3600"]
volumeMounts:
- name: persistent-storage
mountPath: /data
volumes:
- name: persistent-storage
persistentVolumeClaim:
claimName: pvc-example

قم بتطبيق Pod الجديد:

kubectl apply -f pod-read-pvc.yaml

تحقق من السجلات:

kubectl logs pod-read-pvc

يجب أن ترى البيانات التي كتبها Pod الأول، مما يثبت الاستمرارية!


الجزء 4: التنظيف​

احذف الموارد التي تم إنشاؤها:

kubectl delete pod pod-read-pvc
kubectl delete -f pvc-example.yaml
kubectl delete -f pv-example.yaml

ملاحظة: مع persistentVolumeReclaimPolicy: Retain، حتى بعد حذف PVC، يبقى PV بحالة Released. يتم الاحتفاظ بالبيانات. لحذف PV بالكامل، يجب حذفه يدويًا:

kubectl delete pv pv-example

ملخص المختبر​

في هذا المختبر، استكشفت PersistentVolumes و PersistentVolumeClaims. تعلمت كيفية إنشاء PV يدويًا، وإنشاء PVC يرتبط تلقائيًا بـ PV، واستخدام PVC في Pod لتخزين البيانات المستمرة. كما تحققت من أن البيانات تبقى بعد حذف وإعادة إنشاء Pods.


الخطوات التالية​

سيقدم لك المختبر التالي StorageClasses والتوفير الديناميكي، الذي يبسط بشكل كبير إدارة التخزين.

المختبر 6.3: StorageClasses والتوفير الديناميكي


تم إنشاء المختبر في: ديسمبر 2024