1. Introduction
الـ Containers والـ Pods بطبيعتهم Ephemeral؛ يعني ممكن يتعملهم Delete ويتعملهم Recreate في أي وقت.
المشكلة إن البيانات اللي موجودة داخل الـ Container Filesystem ممكن تضيع لما الـ Container يتوقف أو يتشال.
عشان نحتفظ بالبيانات، بنستخدم Volumes.
? What is a Volume
Volume هو Storage Resource بيتم إتاحته للـ Container داخل الـ Pod عن طريق Mount Path، بحيث يقدر يقرأ ويكتب البيانات عليه.
2. The Problem: Ephemeral Storage
تخيل Pod بيشغّل Application بتكتب بيانات في الملف:
/opt/number.out
لو البيانات اتكتبت داخل الـ Container Filesystem العادي، ممكن تضيع لما الـ Container يتشال.
لكن لو ربطنا Volume بالـ Pod وعملنا Mount له على /opt، البيانات هتتكتب داخل الـ Volume بدلًا من الـ Container’s writable layer.
flowchart TD
A(["Pod Created"]) --> B(["Application Generates Data"])
B --> C(["Writes to /opt/number.out"])
C --> D{"Where is /opt Mounted?"}
D -->|"Without Volume"| E(["Container Filesystem"])
D -->|"With Volume"| F(["Mounted Volume"])
E --> G(["Data May Be Lost"])
F --> H(["Data Survives Pod Deletion if Backend Persists"])
classDef pod fill:#e9dfce,stroke:#8b7355,color:#33291f,stroke-width:2px
classDef bad fill:#e8c5bd,stroke:#9c6258,color:#42251f,stroke-width:2px
classDef good fill:#c5d0c4,stroke:#667766,color:#263326,stroke-width:2px
class A,B,C,D pod
class E,G bad
class F,H good
linkStyle default stroke:#a58a64,stroke-width:2px
3. Creating a Volume with hostPath
الـ hostPath بيستخدم Directory موجود على الـ Node نفسه.
مثال: هنستخدم /data على الـ Host، ونعمل Mount له داخل الـ Container على /opt.
I. Pod Definition
apiVersion: v1
kind: Pod
metadata:
name: random-number-generator
spec:
containers:
- name: generator
image: busybox
command:
- sh
- -c
- "shuf -i 1-100 -n 1 > /opt/number.out; sleep 3600"
volumeMounts:
- name: data-volume
mountPath: /opt
volumes:
- name: data-volume
hostPath:
path: /data
type: DirectoryOrCreate
II. Understanding the Configuration
| Field | Purpose |
|---|---|
volumes |
Defines Volumes available to the Pod |
name |
Volume name inside the Pod specification |
hostPath |
Uses a path from the Node’s filesystem |
path |
Directory on the Host Node |
volumeMounts |
Defines where the Volume appears inside the Container |
mountPath |
Container path where the Volume is mounted |
III. Data Flow
flowchart LR
A(["Application"]) -->|"Writes /opt/number.out"| B(["Container"])
B -->|"Mount"| C(["data-volume"])
C -->|"hostPath"| D(["Node: /data/number.out"])
classDef app fill:#e9dfce,stroke:#8b7355,color:#33291f,stroke-width:2px
classDef volume fill:#d8c6a5,stroke:#8b7355,color:#33291f,stroke-width:2px
classDef host fill:#c5d0c4,stroke:#667766,color:#263326,stroke-width:2px
class A,B app
class C volume
class D host
linkStyle default stroke:#a58a64,stroke-width:2px
بالتالي، الملف اللي التطبيق بيكتبه في /opt/number.out داخل الـ Container، بيظهر على الـ Node في:
/data/number.out
لو الـ Pod اتحذف، الملف يفضل موجود على الـ Node طالما الـ Directory نفسه موجود.
4. The Problem with hostPath
hostPath مناسب للتجارب والـ Local Development في حالات معينة، لكنه مش اختيار مناسب عادةً لإدارة Persistent Storage في Multi-Node Production Clusters.
ليه؟
لأن كل Node عنده Filesystem مستقل.
تخيل Cluster فيه 3 Nodes:
Node 1: /data
Node 2: /data
Node 3: /data
دي ثلاث Directories مختلفة، ومش معناها إنهم بيشاركوا نفس البيانات.
لو Pod اشتغل على Node 1 وكتب ملف في /data، وبعد كده اتنقل أو اتعمله Recreate على Node 2، مش هيلاقي نفس الملف تلقائيًا.
flowchart TD
A(["Kubernetes Cluster"]) --> B(["Node 1"])
A --> C(["Node 2"])
A --> D(["Node 3"])
B --> E(["/data - File A"])
C --> F(["/data - Empty"])
D --> G(["/data - Empty"])
classDef cluster fill:#e9dfce,stroke:#8b7355,color:#33291f,stroke-width:2px
classDef node fill:#d8c6a5,stroke:#8b7355,color:#33291f,stroke-width:2px
classDef data fill:#c5d0c4,stroke:#667766,color:#263326,stroke-width:2px
class A cluster
class B,C,D node
class E,F,G data
linkStyle default stroke:#a58a64,stroke-width:2px
5. Other Storage Options
Kubernetes بيدعم أنواع مختلفة من Storage Backends.
أمثلة :
| Storage Type | Description |
|---|---|
| NFS | Network File System |
| CephFS | Distributed File System |
| Fibre Channel | Storage networking technology |
| AWS EBS | Block Storage on AWS |
| Azure Disk | Managed Block Storage on Azure |
| Azure File | Managed File Storage on Azure |
| Google Persistent Disk | Block Storage on Google Cloud |
اختيار الـ Backend بيعتمد على البيئة والـ Requirements والـ CSI Driver المتاح.
6. Volume vs Container Filesystem
| Container Filesystem | Volume |
|---|---|
| مرتبط بعمر الـ Container | Lifecycle يعتمد على نوع الـ Volume |
| البيانات ممكن تضيع مع حذف الـ Container | ممكن يحتفظ بالبيانات بعد حذف الـ Container أو Pod |
| موجود داخل Container Writable Layer | بيتم Mount داخل Container |
| مناسب للملفات المؤقتة | مناسب للبيانات التي تحتاج تخزينًا خارج الـ Container Layer |
8. Practical Lab
Create the Pod
kubectl apply -f random-number-generator.yamlCheck the Pod
kubectl get podsRead the Generated File
kubectl exec random-number-generator -- cat /opt/number.outCheck the Node Directory
في
hostPath، لازم تفحص الـ Node اللي الـ Pod شغال عليه، مش تفترض إن/dataموجودة على جهازك المحلي.لو بتستخدم
kind، الـ Node عبارة عن Container، وممكن تفحصه باستخدام:docker exec -it devops-lab-control-plane shثم:
cat /data/number.outDelete the Pod
kubectl delete pod random-number-generatorبعد حذف الـ Pod، افحص الملف على الـ Node مرة تانية.
9. Key Takeaways
- Pods and Containers are ephemeral.
- Volumes provide storage to Containers.
volumesdefines the Volume at Pod level.volumeMountsmounts it inside a Container.hostPathuses a path on a specific Node.- Different Nodes do not automatically share the same
hostPathdata. - Production storage often uses networked or cloud storage through suitable drivers.