Kubernetes/Helm: sharing a single not properties file between Init and Main containers

12/16/2019

In a kubernetes (minikube) / helm env I have an ldif file I want to share on a volume between an Init container and a normal one. I don't want to share the entire folder in which this file is stored.

Unfortunately from my understanding this is not possible unless the file is a properties one (syntax "key=value").

I tested using configMap/subPath and it seems like that if key/value syntax is not respected the Init container is not even starting, otherwise everything works and the file appears on the main container as well.

So I wonder if it is even possibile to accomplish this kind of sharing.

BR

edit: main container startup command is a single service start, it cannot execute copies or move the file shared by init container, unless it is the only way.

-- user83647511
configmap
data-sharing
kubernetes
kubernetes-helm
volume

2 Answers

12/16/2019

To share files between init container and other containers, you can use volume mount with emptyDir

  volumes:
  - name: cache-volume
    emptyDir: {}

The the file that init container wants to share can be copied by the container process on the mountPath of emptyDir and then main container picks it from mouthPath of emptyDir volume.

That way even after pod restarts, the file in question, say in path /path1/file gets copied on to /path2/file ( mouthPath of emptyDir on initcontainer) and then stays there as emptyDir gets mounted on to main container is avaliable until pod restarts

-- Shambu
Source: StackOverflow

12/16/2019

Yes, it is possible, and you are on the right track.

Here's an example of how to do this.

---
kind: ConfigMap 
apiVersion: v1 
metadata:
  name: example-configmap 
data:
  my-file.ldif: |
     dn: cn=The Postmaster,dc=example,dc=com
     objectClass: organizationalRole
     cn: The Postmastermongodb
---
kind: Pod
apiVersion: v1
metadata:
  name: example-pod
spec:
  volumes: 
  - name: config-volume
    configMap:
      name: example-configmap
  initContainers:
  - name: init
    image: busybox
    volumeMounts:
    - name: config-volume
      mountPath: /path/in/the/init-container/my-file.ldif
      subPath: my-file.ldif
  containers:
  - name: main
    image: busybox
    volumeMounts:
    - name: config-volume
      mountPath: /path/in/the/container/my-file.ldif
      subPath: my-file.ldif

It would help if you posted your configmap. You might be getting tripped up because for this to work you need the whole contents of your file as the value of one key in your configmap.

-- switchboard.op
Source: StackOverflow