Kubernetes - volume persistente

Postat la Fri 13 October 2023 in tutoriale

Dupa cum stim containerele ce ruleaza in kubernetes nu sunt permanente, se pot opri si reporni pe oricare nod din cluster deci nu sunt potrivite pentru a stoca date pe termen lung ca de exemplu bazele date, imagini sau cand mai multe containere /pod-uri trebuie sa faca share la aceleasi date.

In Kubernetes sunt suportate cateva tipuri de volume:

  • directoare locale pe disc la noduri
  • partii locale
  • NFS
  • AWS EBS
  • Azure Disk
  • GCEPersistentDisk
  • RDB

Un exemplu de fisier pentru configurarea unui volum NFS de 2Gi in mod RW ce poate asociat la mai multe containere:

#db-data-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: db-data-pv
spec:
  capacity:
    storage: 2Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: nfs
  mountOptions:
    - hard
    - nfsvers=4.0
  nfs:
    path: /mnt/sdb/nfsdata/db-data
    server: 10.118.33.1

In acest exemplu folosim partia exportata /mnt/sdb/nfsdata/db-data de pe serverul 10.118.33.1

Initializam acest volum permanent cu comanda kubectl:

kubectl apply -f db-data-pv.yaml

Verificam crearea acestui volum:

#kubectl get pv -o wide
NAME         CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE   VOLUMEMODE
db-data-pv   2Gi        RWX            Recycle          Available           nfs                     1m    Filesystem

#kubectl describe persistentvolumes db-data-pv
Name:            db-data-pv
Labels:          <none>
Annotations:     pv.kubernetes.io/bound-by-controller: yes
Finalizers:      [kubernetes.io/pv-protection]
StorageClass:    nfs
Status:          Bound
Claim:           .. code-block:: console
Reclaim Policy:  Recycle
Access Modes:    RWX
VolumeMode:      Filesystem
Capacity:        2Gi
Node Affinity:   <none>
Message:
Source:
Type:      NFS (an NFS mount that lasts the lifetime of a pod)
Server:    10.118.33.1
Path:      /mnt/sdb/nfsdata/db-data
ReadOnly:  false
Events:        <none>

Pentru a putea folosi un volum persistent in pod trebuie solicitat printr-un PersistentVolumeClaim (PVC).

In PVC se solicita un volum de 2Gi de tip NFS in mod rw:

#db-data-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: db-data-pvc
spec:
  storageClassName: nfs
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 2Gi

Initializam acest PVC comanda kubectl:

kubectl apply -f db-data-pvc.yaml

si se va atribui un volum conform cerintelor - 2Gi/NFS/RW

Verificam crearea acestui PVC:

#kubectl get pvc
NAME          STATUS   VOLUME       CAPACITY   ACCESS MODES   STORAGECLASS   AGE
db-data-pvc   Bound    db-data-pv   2Gi        RWX            nfs            16s

#kubectl describe persistentvolumeclaims db-data-pvc
Name:          db-data-pvc
Namespace:     cristi-dev
StorageClass:  nfs
Status:        Bound
Volume:        db-data-pv
Labels:        <none>
Annotations:   pv.kubernetes.io/bind-completed: yes
pv.kubernetes.io/bound-by-controller: yes
Finalizers:    [kubernetes.io/pvc-protection]
Capacity:      2Gi
Access Modes:  RWX
VolumeMode:    Filesystem
Used By:       <none>
Events:        <none>

Iar daca verificam din nou volumul creat mai sus, vom regasi ca este asociat cu PVC db-data-pvc

#kubectl get pv
NAME         CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                    STORAGECLASS   REASON   AGE
db-data-pv   2Gi        RWX            Recycle          Bound    cristi-dev/db-data-pvc   nfs                     2m58s

#kubectl describe persistentvolumes db-data-pv
Name:            db-data-pv
Labels:          <none>
Annotations:     pv.kubernetes.io/bound-by-controller: yes
Finalizers:      [kubernetes.io/pv-protection]
StorageClass:    nfs
Status:          Bound
Claim:           cristi-dev/db-data-pvc
Reclaim Policy:  Recycle
Access Modes:    RWX
VolumeMode:      Filesystem
Capacity:        2Gi
Node Affinity:   <none>
Message:
Source:
Type:      NFS (an NFS mount that lasts the lifetime of a pod)
Server:    10.118.33.1
Path:      /mnt/sdb/nfsdata/db-data
ReadOnly:  false
Events:        <none>

Nota: mai multi parametrii de configurare si optiuni gasiti in documentatie