State Persistence - Sandeep-K-Khandelwal/CKAD GitHub Wiki

Volumes

When a POD is created to process data and then deleted, the data processed by it gets deleted as well. For this, we attach a volume to the POD. The data generated by the POD is now stored in the volume, and even after the POD is deleted, the data remains.

First, the Volume is created and then it is mounted into the container.

apiVersion: v1
kind: Pod 
metadata:
  name: random-number-generator 
spec:
  containers:
  - image: alpine
    name: alpine
    command: ["/bin/sh","-c"]
    args: ["shuf -i 0-100 -n 1 >> /opt/number.out;"] 
    volumeMounts:
    - mountPath: /opt
      name: data-volume
  volumes:
  - name: data-volume
      hostPath: 
        path: /data
        type: Directory
  volumes:
  - name: test-volume
    awsElasticBlockStore:
      volumeID: "<volume id>"
      fsType: ext4

Persistent Volumes

A Persistent Volume is a Cluster-wide pool of storage volumes configured by an administrator, to be used by users deploying applications on the cluster. The users can now select storage from this pool using Persistent Volume Claims.

apiVersion: v1
kind: PersistentVolume
metadata: 
  name: pv-vol1
spec:
  accessModes: 
  - ReadWriteOnce
  capacity:
    storage: 1Gi
  hostPath:
    path: /data

Persistent Volume Claims

An Administrator creates a set of Persistent Volumes and a user creates Persistent Volume Claims to use the storage. Once the Persistent Volume Claims are created, Kubernetes binds the Persistent Volumes to Claims based on the request and properties set on the volume.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: myclaim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 8Gi

Use Persistent Volume claim in the POD

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
    - name: myfrontend
      image: nginx
      volumeMounts:
      - mountPath: "/var/www/html"
        name: mypd
  volumes:
    - name: mypd
      persistentVolumeClaim:
        claimName: myclaim
⚠️ **GitHub.com Fallback** ⚠️