متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 249Kubernetes · Storage

How to give a Pod persistent storage

Files written inside a container are gone when the Pod restarts. For uploads, a small database or a cache that must survive, a Pod needs a PersistentVolumeClaim (PVC): a request for storage that a StorageClass fulfils automatically with a cloud disk or network volume (a PersistentVolume). This how-to creates a PVC, mounts it, checks the binding, grows it, and explains access modes and what happens on delete.

  • 5 الدروس
  • 1 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

249.1

The quick answer

pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: uploads
  namespace: shop
spec:
  accessModes: [ReadWriteOnce]
  storageClassName: standard
  resources:
    requests:
      storage: 10Gi
kubectl get storageclass
kubectl apply -f pvc.yaml
kubectl -n shop get pvc uploads

الأوامر في هذا الدرس

249.2

Step by step

  1. Which StorageClass? (line 1) — the one marked (default) is used when you do not name one. Note its RECLAIMPOLICY (Delete or Retain) and VOLUMEBINDINGMODE.
  2. Create the PVC (quick answer). With WaitForFirstConsumer it stays Pending until a Pod uses it — that is normal (the disk is created in the Pod's zone).
  3. Mount it in the Pod:
deployment.yaml (part)
    spec:
      containers:
        - name: api
          volumeMounts:
            - name: uploads
              mountPath: /var/lib/api/uploads
      volumes:
        - name: uploads
          persistentVolumeClaim:
            claimName: uploads
  1. Access modes — ReadWriteOnce (RWO): one node mounts it (block disks; a Deployment with this should have 1 replica or strategy: Recreate). ReadWriteMany (RWX): many nodes (NFS, Azure Files, EFS, CephFS). ReadWriteOncePod: exactly one Pod.
  2. File owner — if the app runs as a non-root user, set securityContext.fsGroup so it may write to the volume.
  3. Grow it — if the class has allowVolumeExpansion: true, edit the request (for example 20Gi); most CSI drivers grow online:
kubectl -n shop patch pvc uploads -p '{"spec":{"resources":{"requests":{"storage":"20Gi"}}}}'

Shrinking is not possible.

  1. Delete with care — with reclaimPolicy: Delete deleting the PVC deletes the disk and its data. For important data use a class with Retain, and back up with Velero or CSI snapshots.
  2. More than one Pod with its own disk — use a StatefulSet with volumeClaimTemplates.

الأوامر في هذا الدرس

249.3

Check that it worked

kubectl -n shop get pvc uploads shows STATUS Bound with a volume name and 10Gi; a file written into /var/lib/api/uploads is still there after kubectl -n shop delete pod <pod> (the new Pod mounts the same volume).

الأوامر في هذا الدرس

249.4

Common problems

ProblemCauseFix
PVC Pending foreverNo default StorageClass, or wrong name.Name an existing class; check kubectl describe pvc events.
Pod ContainerCreating: Multi-Attach errorRWO volume still attached to a Pod on another node.One replica / Recreate; or an RWX class.
Permission denied writingApp user cannot write the volume.securityContext.fsGroup; check the image user.
Pod stuck in another zoneDisk is zonal, Pod scheduled elsewhere.WaitForFirstConsumer binding; topology-aware classes.
Data gone after deleting the appReclaim policy Delete.Retain class for important data; backups.
Resize does nothingClass without allowVolumeExpansion.Use an expandable class; some drivers need a Pod restart.

الأوامر في هذا الدرس

249.5

Go further

1What does a PersistentVolumeClaim do?

It asks for storage of a size and access mode; the StorageClass then creates a volume and binds it to the claim.

2What happens to the data when you delete a PVC with reclaim policy Delete?

The volume and all its data are deleted too.

الأوامر في هذا الدرس

كل الأوامر في هذا الفصل