How to mount a ConfigMap as a file without using volume

8/15/2019

The aim behind this question is to know how having a file inside a Pod, if we use ConfigMap, I don't want to apply changes if the configMap will change

Thanks

-- Smaillns
configmap
kubernetes
pod

2 Answers

8/15/2019

I think you want a file inside a pod but you don't want to get that updated if the config map is updated. So you can not use configmap because it will update files if pod restart.

So the only option I can think to push file after the pod is created something like "kubectl cp" command to copy a file to the pod.

kubectl cp <some-namespace>/<some-pod>:/tmp/foo /tmp/bar

The problem will be when pod restarted the file be gone so you have to automate it by somehow one way,

Below is using init container.

you can put the file in some XYZ location and download it by init container.

  initContainers:
  - name: download-conf
    image: busybox:1.28
    command: ['sh', '-c', 'curl http://exampel.com/conf.file -o statfull.conf']

NOTE:- This is an just approach code have't tested

-- yogesh kunjir
Source: StackOverflow

8/15/2019

I am not really understanding, why don't you want to use a volume? A proper way, to mount a confgimap to a pod looks like this: Configmap- specify name of the file in a data section:

apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: 2016-02-18T18:52:05Z
  name: txt-file-configmap
  namespace: default
  resourceVersion: "516"
  selfLink: /api/v1/namespaces/default/configmaps/game-config
  uid: b4952dc3-d670-11e5-8cd0-68f728db1985
data:
  file.txt: |
    here
    are
    filecontents

And in a pod, specify a volume with a configmap name and volumeMount, pointing a path, where to mount the volume:

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "cat /etc/txtfiles/file.txt" ]
      volumeMounts:
      - name: txt-file
        mountPath: /etc/txtfiles
  volumes:
    - name: txt-file
      configMap:
        name: txt-file-configmap

The example pod which I provided you, will have the configmap mounted as a file and will print it's contents.

-- Marcin Ginszt
Source: StackOverflow