Kubernetes: Merge files from a volume into a target directory, without using subPath - kubernetes

Using subPath, I can mount individual files from a volume into a container directory while still retaining the other files already present in the directory.
However, subPath requires creating an entry in the manifest for each file to be mounted.
I want to mount all files in a volume without naming each file explicitly in the manifest.
As far as possible I do not want to use init containers.
Is this possible in Kubernetes?

Related

set environment variables within a mountPath in Kubernetes

we have deployed an application in Kubernetes as deployment and stored the logs in a folder named /podlogs. whenever the pod has taken the restart, it will create a new folder named with the latest pod name inside the app-log folder and store the actual log files. For example this new folder could be /podlogs/POD_Name
Previously we have mounted /podlogs to ELK and azure blob containers.
By using the subPath, we would like to also mount the /podlogs/POD_Name to a second mount.
How can we pass the env variable in the mount path along with the subPath?
See the DownwardAPI https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/
It is possible to expose the pod name as a env variable or to mount
It is conceivable you could then mount or expose this to the ELK container

Helm: creating a volume with original image files

I have an image, specifically Nginx, that I want to run with readOnlyRootFilesystem: true.
For this reason I started creating empty volumes (using volumes, emptyDir and then volumeMounts) for the places that a container would write to, like /etc/nginx. This way I can use readOnlyRootFilesystem with specific mounted writable folders.
However it seems there are some files in /etc/nginx, like /etc/nginx/template/nginx.tmpl, that are needed by the container.
How can I create the volumes in such a way that it contains the files present in the image? So that files like /etc/nginx/template/nginx.tmpl are present in the new volume but readOnlyRootFilesystem can still be used.

How to mount Kubernetes directory as some other directory?

I have a Kubernetes app that I want to copy to another environment.
The original app uses container directory /data/X and synchronizes it with NFS server.
I have a following problems:
I want to run new app in another environment and must not overwrite NFS storage of the original app
I need to create /data/X directory in the container of new app, as the developers rely on this directory inside container - they install software there and create files inside
My question is: Is there any option for me to create directory /data/Y inside container on the environment and let it act as a folder /data/X so the software is installed inside container correctly, files are created and NFS storage of the original app is not overwritten?
Do you just want the /data/X folder to be inside the running container and not on the NFS for this new environment?
If so you can make sure that /data/X folder is created as part of the image, for example by putting this line in the Dockerfile
RUN mkdir -p /data/X
In the original environment you mount NFS storage on /data/X. This will behave like mounts do in Linux, i.e that they shadow everything in /data/X on the local file system and you appear to read/write to the NFS share.
In the new environment you are creating you just skip mounting anything there. The developers can still use /data/X as they usually would, but the content will not be persisted and it will not be possible to share it between containers

Is it possible to mount a file in read/write mode in kubernetes deployment

I have application run inside the kuberentes pod that update the user configuration file and on every deployment it flush the data, as the file reside in a folder which cann't be mounted so I created the empty configmap to mount that file as configmap with subpath mounting and also set the defaultmode of file 777 but still my application is unable to update the content of the file.
Is there way I can mount a file with read/write permission enable for all user so my application can update the file at runtime.
No, a configmap mount is read-only since you need to go through the API to update things. If you just want scratch storage that is temporary you can use an emptyDir volume but it sounds like you want this to stick around so check out the docs on persistent volumes (https://kubernetes.io/docs/concepts/storage/persistent-volumes/). There's a lot of options and complexity, you'll need to work out what is the best match for your use case.

Kubernetes - Share single file between containers (within the same pod)

I have an API that describes itself through an openapi3 file. This app is contained in a pod that also has a sidecar app that is supposed to read this file at startup time.
My probleme is how my sidecar app can read the openapi file from the other container ?
I know I could do it using a volume (emptyDir) and modify the command so my api copies the file at startup time. I'd rather not go this route. I have been looking for a feature, where I define a volume which is mapped to an existing folder in my app, but without being empty. Is there such a thing ?
One of the simplest approaches is to use emptyDir: https://kubernetes.io/docs/concepts/storage/volumes/#emptydir
In your container that generates the file that needs to be shared, mount emptyDir volume with write access and copy the file there. In your sidecar that needs to read the file, mount the same volume as read only and read the file.
With this pattern, all containers in the pod can have access to the same file system with read / write as needed.