Search code examples
kubernetesdocker-volumeconfigmap

How to create a volume that mounts a file which it's path configured in a ConfigMap


I'll describe what is my target then show what I had done to achieve it... my goal is to:

  • create a configmap that holds a path for properties file
  • create a deployment, that has a volume mounting the file from the path configured in configmap

What I had done:

ConfigMap:

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-configmap
data:
  my_properties_file_name: "my.properties"

Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-client-deployment
spec:
  selector:
    matchLabels:
      app: my-client
  replicas: 1 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        app: my-client
    spec:
      containers:
      - name: my-client-container
        image: {{ .Values.image.client}}
        imagePullPolicy: {{ .Values.pullPolicy.client }}
        ports:
        - containerPort: 80
        env:
        - name: MY_PROPERTIES_FILE_NAME
          valueFrom:
            configMapKeyRef:
              name: my-configmap
              key: my_properties_file_name
        volumeMounts:
        - name: config
          mountPath: "/etc/config"
          readOnly: true
      imagePullSecrets:
      - name: secret-private-registry  
      volumes:
      # You set volumes at the Pod level, then mount them into containers inside that Pod
      - name: config
        configMap:
          # Provide the name of the ConfigMap you want to mount.
          name: my-configmap
          # An array of keys from the ConfigMap to create as files
          items:
          - key: "my_properties_file_name"
            path: "my.properties"

The result is having a file namedmy.properties under /etc/config, BUT the content of that file is "my.properties" (as it was indicated as the file name in the configmap), and not the content of properties file as I have it actually in my localdisk.

How can I mount that file, using it's path configured in a configmap?


Solution

  • Put the content of the my.properties file directly inside the ConfigMap:

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: my-configmap
    data:
      my_properties_file_name: |
        This is the content of the file.
        It supports multiple lines but do take care of the indentation.
    

    Or you can also use a kubectl create configmap command:

    kubectl create configmap my-configmap --from-file=my_properties_file_name=./my.properties
    

    In either method, you are actually passing the snapshot of the content of the file on the localdisk to kubernetes to store. Any changes you make to the file on the localdisk won't be reflected unless you re-create the configmap.

    The design of kubernetes allows running kubectl command against kubernetes cluster located on the other side of the globe so you can't simply mount a file on your localdisk to be accessible in realtime by the cluster. If you want such mechanism, you can't use a ConfigMap, but instead you would need to setup a shared volume that is mounted by both your local machine and the cluster for example using a NFS server.