How to store temporary data to Kubernetes Cluster?

3/20/2019

I want to store temporary data such as a json object and sharable between current pods and any newly created pods in a given kubernetes cluster.

I would like to use this for checkpointing purposes that I would like to share with current pods and any new pods that gets introduced in the cluster. I cannot loose the data for any reason even if all the pods gets deleted by mistake.

If I can isolate this to a specific namespace, that is also helpful.

How can I make a persistent data to store and share across pods?

-- Kannaiyan
azure-kubernetes
kubernetes

2 Answers

3/20/2019

You can use PersistentVolume and PersistentVolumeClaim for this purpose. This is the solution offered by k8s for persistent data. Write all data to PVC which you don't want to loose even if all pod dies. Also data written to PV can be shared by pod as volumeMounts. There are multiple ways you can create PV and PVC like hostPath, NFS etc. You can please check k8s docs for more details and suitable PV type for your use case.

-- Rajesh Deshpande
Source: StackOverflow

3/20/2019

You can use the concept call PV and PVC in kubernetes.

kind: PersistentVolume
apiVersion: v1
metadata:
  name: sql-pv-volume
  labels:
    type: local
spec:
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"
  persistentVolumeReclaimPolicy: Retain
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: sql-pv-claim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi

it will create the pvc in your kubernetes cluster and you can attach it to pods.

volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: sql-pv-claim

if your pod will goes deleted then also data will be there in PVC

-- Harsh Manvar
Source: StackOverflow