Preserve the changes after the Kubernates pod restarts

1/23/2020

I have some sets of operation goes on a kubernetes pods which stores some local data on pod itself and when the pod is restarted ,it gets lost.

Any suggestions how can i make the changes make available in new kubernetes pod

-- Neeraj
kubernetes

2 Answers

1/23/2020

Files in a Container are ephemeral in nature so they will be lost on container restart unless stored on a persistent storage volume.

Refer this task on official kubernetes docs Configure a Pod to Use a PersistentVolume for Storage

Very basic example on how to persist file content as below.

First Create a persistent volume refer below yaml example with hostPath configuration

apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv-1
  lables:
    pv: my-pv-1
spec:
  capacity:
    storage: 1Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: /var/log/mypath

$ kubectl create -f pv.yaml
persistentvolume/my-pv-1 created

Second create a persistent volume claim using below yaml example

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc-claim-1
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
  selector:
    matchLabels:
      pv: my-pv-1


$ kubectl create -f pvc.yaml
persistentvolumeclaim/my-pvc-claim-1 created

Verify the pv and pvc STATUS is set to BOUND

$ kubectl get persistentvolume
NAME      CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                  STORAGECLASS   REASON   AGE
my-pv-1   1Gi        RWX            Retain           Bound    default/my-pvc-claim-1                         62s

$ kubectl get persistentvolumeclaims
NAME             STATUS   VOLUME    CAPACITY   ACCESS MODES   STORAGECLASS   AGE
my-pvc-claim-1   Bound    my-pv-1   1Gi        RWX                           58

Third consume the pvc in required POD(container) refer below example yaml where the volume is mounted on container.

apiVersion: v1
kind: Pod
metadata:
  name: nginx-with-vol
spec:
  containers:
  - image: nginx
    name: nginx-with-vol
    volumeMounts:
    - mountPath: /var/log/mypath
      name: test-vol
  volumes:
  - name: test-vol
    persistentVolumeClaim:
        claimName: my-pvc-claim-1


$ kubectl create -f test-volume.yaml
pod/nginx-with-vol created


$ kubectl get pods -o wide
NAME             READY   STATUS    RESTARTS   AGE   IP            NODE         NOMINATED NODE   READINESS GATES
nginx-with-vol   1/1     Running   0          35s   10.244.3.53   k8s-node-3   <none>           <none>

Test by connecting to container and write to the file on mount-path.

$ kubectl exec -it nginx-with-vol -- /bin/bash
root@nginx-with-vol:/#
root@nginx-with-vol:/# df -kh
Filesystem      Size  Used Avail Use% Mounted on
overlay          12G  6.1G  5.5G  53% /
tmpfs            64M     0   64M   0% /dev
tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/vda1        12G  6.1G  5.5G  53% /etc/hosts
shm              64M     0   64M   0% /dev/shm
tmpfs           3.9G   12K  3.9G   1% /run/secrets/kubernetes.io/serviceaccount
tmpfs           3.9G     0  3.9G   0% /proc/acpi
tmpfs           3.9G     0  3.9G   0% /proc/scsi
tmpfs           3.9G     0  3.9G   0% /sys/firmware
root@nginx-with-vol:/#
root@nginx-with-vol:/# cd /var/log/mypath/
root@nginx-with-vol:/var/log/mypath# pwd
/var/log/mypath
root@nginx-with-vol:/var/log/mypath# ll
bash: ll: command not found
root@nginx-with-vol:/var/log/mypath# date >> date_file.txt
root@nginx-with-vol:/var/log/mypath#
root@nginx-with-vol:/var/log/mypath# cat date_file.txt
Mon Jan 27 09:58:43 UTC 2020
root@nginx-with-vol:/var/log/mypath#
root@nginx-with-vol:/var/log/mypath# date >> date_file.txt
root@nginx-with-vol:/var/log/mypath#
root@nginx-with-vol:/var/log/mypath# cat date_file.txt
Mon Jan 27 09:58:43 UTC 2020
Mon Jan 27 09:58:51 UTC 2020
root@nginx-with-vol:/var/log/mypath#

Now restart the container and it should connect to same volume and file data should persist.

Note : the restart count is now 1 on below example

$ kubectl get pods -o wide
NAME             READY   STATUS    RESTARTS   AGE     IP            NODE         NOMINATED NODE   READINESS GATES
nginx-with-vol   1/1     Running   1          8m11s   10.244.3.53   k8s-node-3   <none>           <none>


$ kubectl exec -it nginx-with-vol -- /bin/bash
root@nginx-with-vol:/#

root@nginx-with-vol:/# cd /var/log/mypath/

root@nginx-with-vol:/var/log/mypath# ls -lart
total 12
drwxr-xr-x 2 root root 4096 Jan 27 09:58 .
-rw-r--r-- 1 root root   58 Jan 27 09:58 date_file.txt
drwxr-xr-x 1 root root 4096 Jan 27 10:01 ..

root@nginx-with-vol:/var/log/mypath# date
Mon Jan 27 10:03:22 UTC 2020


root@nginx-with-vol:/var/log/mypath# cat date_file.txt
Mon Jan 27 09:58:43 UTC 2020
Mon Jan 27 09:58:51 UTC 2020
-- DT.
Source: StackOverflow

1/23/2020

Without concrete example, it's hard to help. But what you're talking about seems to be a mounted volume...

https://kubernetes.io/docs/concepts/storage/volumes/

-- X-Blaster
Source: StackOverflow