<p>I am trying to change a file in a pod in kubernetes but after the pod getting restarted, the file is reverting back to its original content. Is there any approach to do it? I want the updated file to be remain unchanged even after the pod getting restarted.</p>

<p>Thanks in advance</p>

abinash_123_
<p>Very basic example on how to persist file content as below.</p>

<p>First <strong>Create a persistent volume</strong> refer below yaml example with hostPath configuration</p>

<pre><code>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
</code></pre>

<p>Second <strong>create a persistent volume claim</strong> using below yaml example </p>

<pre><code>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
</code></pre>

<p><strong>Verify the pv and pvc STATUS is  set to BOUND</strong> </p>

<pre><code>$ 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
</code></pre>

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

<pre><code>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   &lt;none&gt;           &lt;none&gt;
</code></pre>

<p><strong>Test by connecting to container</strong> and write to the file on mount-path.</p>

<pre><code>$ 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 &gt;&gt; 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 &gt;&gt; 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#
</code></pre>

<p>Now <strong>restart the container</strong> and it should connect to same volume and file data should persist.</p>

<p><strong>Note : the restart count is now 1 on below example</strong></p>

<pre><code>$ 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   &lt;none&gt;           &lt;none&gt;


$ 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
</code></pre>
