Automation, helm

Mounting volumes to kubernetes Pod container using helm chart


Helm chart for volume mount

In this article ,I will explain how to mount a folder/file in a kubernetes pod container using helm chart . There are some useful functions in Helm which makes our Job easy ,let’s go and create the volume mount .

Create a config map as mentioned below .

Mounting a Folder

apiVersion: v1
kind: ConfigMap
metadata:
name: my-config map
labels:
heritage: {{ .Release.Service | quote }}
release: {{ .Release.Name | quote }}
chart: mychart
app: myapp

data:
{{ (.Files.Glob "ConfigDir/*").AsConfig | indent 2 }}

I have created a folder called Configdir at the same level as the templates directory.Also I kept few configuration file in this folder .Now our aim is to mount this folder to pod container.

Now ,Make the below changes in the deployment yaml file .
1) Specify your config map name and name of your volume mount ,

restartPolicy: Always
volumes:
- name: test-mount
  configMap:
     name: my-config map

Here, we created volume with name of test-mount and entered the config map which is created recently .

Please make sure, indentation should proper otherwise chart won’t read the file .

2)Now we need to specify where to mount this folder to container .Before you do this, go and check the container mount location . Once you decide the location ,specify it in mountPath in the volumeMounts .I decided to mount at /myapp/ folder

containers:
- name: myapp
  image: "{{ .Values.myapp.image.repository }}:{{ .Values.myapp.image.tag }}"
 volumeMounts:
   - name: test-mount
     mountPath: /myapp/

Now the ConfigDir from local folder mounted to /myapp/ in container.Now start using the files in container or pass the file to pod container as arguments .

This is the easiest way to overwrite the configuration file dynamically using config map .

Suppose the file already exist then add one more parameter subPath with filename
that will override the existing file .

volumeMounts:
- name: test-mount
  mountPath: /myapp/
  subPath: db_config.txt

Mounting a File

The same way if you want to mount a file, there is another useful function in Helm that you can specify it in config map.

data:
db_config: {{ .Files.Get "myapp/db_config.txt" | indent 4 }}

Update the deployment file with below entries.

volumes:
        - name: test-mount
          configMap:
            name: my-config map
            items:
              - key: db_config
                path: custom_db_config.txt

Here ,we consider “db_config” as key and file content will be stored as “custom_db_config.txt”. Specify this in deployment file “volumes” as item’s key and path .

No change in volumeMounts section.

After deployment, “suites” folder will be mounted to container and file will be created with name of “custom_db_config.txt”.

To get the file dynamically from values.yaml then refer the below code .

data:
db_config: {{ .Files.Get .Values.test.CONFIG_PATH | indent 4 }}

Mounting a File which is outside of chart

The above examples work when the file is available inside the chart.Let say, if your file presents an outside chart then we need to use another approach.

To help with this, helm provides an in-build function called –from-file where you can specify the file location.

kubectl create configmap <config-map-name> --from-file=<key-name>=<file-location>

Ex :kubectl create configmap mydb-config-map --from-file=custom_file=/Users/raj/db_config.txt

When you create a config map with the above command, that will create config map with key as custom_file and value as a file.Now we need to specify the same in deployment file in the “volumes 

      restartPolicy: Always
      volumes:
        - name: test-suites
          configMap:
            name: mydb-config-map
            items:
              - key: custom_file
                path: db_config.txt

Also update the volumeMounts in deployment file with your container location

volumeMounts:
- name: test-mount
  mountPath: /myapp/
  


1 thought on “Mounting volumes to kubernetes Pod container using helm chart”

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.