How to modify/replace a file which is already exist in a folder of pod

Hello Community

We are looking a solution to modify an existing file which is present in the particular directory of the pod.

For example -
In POD X, under /root/user/data there are multiple files present including “abc” so we want to just modify/replace a “abc” file which is present in /root/user/data.

Any idea How we can achieve this ??

Cluster information:

Kubernetes version: 1.24
Cloud being used: (put bare-metal if not on a public cloud) - Yes AWS EKS
Installation method:
Host OS: Amazon Linux
CNI and version:
CRI and version:

You can mount a file from a volume on top of it.

I tried that and it is removing the all existing files and replacing with a single file.
Are you suggesting like below ?? Or some other solution ?

      volumeMounts:
      - name: vol-auth-file
        mountPath: /root/user/data/abc
  volumes:
    - name: vol-auth-file
      configMap:
        name: test-auth-file

Note - We created a ConfigMap test-auth-file for a file abc

That mounts the whole configmap volume (which is a directory). You can use subPath to extract one file and mount it over another file.

Hi @thockin ,

I tried with subPath but facing the same issue.

Below is my code -

      volumeMounts:
      - name: vol-auth-file
        mountPath: /root/user/data/abc
        subPath: abc
  volumes:
    - name: vol-auth-file
      configMap:
        name: test-auth-file

Before mounting above configMap vol :-

root@app-01:/root/user/data# ls 
ext_events.lib.lua		       mod_conference_duration.lua	      abc

After mounting above configMap Vol code :-

root@app-01:/root/user/data# ls 
 abc

When I’m applying the changes, it erased all the files present in /root/user/data folder and placing with “abc” only.

It works for me - here’s my replay so you can compare against yours

$ cat /tmp/d.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: foo
data:
  mykey: myvalue
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: sleep
spec:
  replicas: 1
  selector:
    matchLabels:
      app: sleep
  template:
    metadata:
      labels:
        app: sleep
    spec:
      containers:
      - image: busybox
        name: sleep
        args:
          - sleep
          - inf
        volumeMounts:
        - name: myvol
          subPath: mykey
          mountPath: /etc/shadow
      volumes:
      - name: myvol
        configMap:
          name: foo

$ k apply -f /tmp/d.yaml
deployment.apps/sleep created
configmap/foo created

$ k get pod
NAME                         READY   STATUS    RESTARTS   AGE
sleep-6784bf9955-vbcc6       1/1     Running   0          19s

$ k exec -ti sleep-6784bf9955-vbcc6 -- cat /etc/shadow
myvalue

$ k exec -ti sleep-6784bf9955-vbcc6 -- ls -l /etc
total 36
-rw-r--r--    1 root     root           306 Jul 17 18:30 group
-rw-r--r--    1 root     root            23 Aug 16 17:41 hostname
-rw-r--r--    1 root     root           217 Aug 16 17:41 hosts
-rw-r--r--    1 root     root           114 May 28 19:54 localtime
drwxr-xr-x    6 root     root          4096 Jul 17 18:30 network
-rw-r--r--    1 root     root           494 Jul 17 18:30 nsswitch.conf
-rw-r--r--    1 root     root           340 Jul 17 18:30 passwd
-rw-r--r--    1 root     root           178 Aug 16 17:41 resolv.conf
-rw-r--r--    1 root     root             7 Aug 16 17:41 shadow

Thanks much,
Yes it is working fine.

There was some issue with typo that I fixed it. It is working now.