How to persist changes in Kubernetes? There are two files in the docker image that should update with every run

1/16/2022

I do have persistent volume and claim but I am unable to tell Kubernetes how to save these specific files. This is my deployment file, the files I want to persist are in the container root directory.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-first-cluster1
spec:
  replicas: 2
  selector:
    matchLabels:
      app: scrape
  template:
    metadata:
      labels:
        app: scrape
    spec:
      containers:
      - name: scraper
       
        image: asia-south1-docker.pkg.dev/erudite-stratum-338019/repo/scrape:latest
        #
        ports:
        - containerPort: 8080
        env:
        - name: PORT
          value: "8080"

        volumeMounts:
        - mountPath: "/dev/shm"
          name: dshm
      volumes:
      - name: dshm
        emptyDir:
          medium: Memory 

This is the persistent volume file

apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv-1
  labels:
    pv: my-pv-1
spec:
  storageClassName: standard
  capacity:
    storage: 5Gi
  
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: /

And, this is the PersistentVolumeClaim

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc-claim-1
spec:
  storageClassName: standard
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 3Gi
  selector:
    matchLabels:
      pv: my-pv-1
-- Sardar Arslan
gcloud
google-kubernetes-engine
kubernetes

1 Answer

1/16/2022

you can copy files from /root to Persistent volume using LifeCycle hooks ..

PostStart LifeCycle event get executed after container creation.

lifecycleHooks:postStart:exec:command.
-- Ravindra Bagale
Source: StackOverflow