How best to have files on volumes in Kubernetes using helm charts?

11/16/2018

The plan is to move my dockerized application to Kubernetes.

The docker container uses couple of files - which I used to mount on the docker volumes by specifying in the docker-compose file:

volumes:
      - ./license.dat:/etc/sys0/license.dat
      - ./config.json:/etc/sys0/config.json

The config file would be different for different environments, and the license file would be the same across.

How do I define this in a helm template file (yaml) so that it is available for the running application?

What is generally the best practise for this? Is it also possible to define the configuration values in values.yaml and the config.json file could get it?

-- Chillax
docker-volume
kubernetes
kubernetes-helm

1 Answer

11/16/2018

Since you are dealing with json a good example to follow might be the official stable/centrifugo chart. It defines a ConfigMap that contains a config.json file:

data:
  config.json: |-
{{ toJson .Values.config| indent 4 }}

So it takes a config section from the values.yaml and transforms it to json using the toJson function. The config can be whatever you want define in that yaml - the chart has:

config:
  web: true
  namespaces:
  - name: public
    anonymous: true
    publish: true
...

In the deployment.yaml it creates a volume from the configmap:

      volumes:
      - name: {{ template "centrifugo.fullname" . }}-config
        configMap:
          name: {{ template "centrifugo.fullname" . }}-config

Note that {{ template "centrifugo.fullname" . }}-config matches the name of the ConfigMap.

And mounts it into the deployment's pod/s:

        volumeMounts:
        - name: "{{ template "centrifugo.fullname" . }}-config"
          mountPath: "/centrifugo"
          readOnly: true

This approach would let you populate the json config file from the values.yaml so that you can set different values for different environments by supplying custom values file per env to override the default one in the chart.

To handle the license.dat you can add an extra entry to the ConfigMap to define an additional file but with static content embedded. Since that is a license you may want to switch the ConfigMap to a Secret instead, which is a simple change of replacing the word ConfigMap for Secret in the definitions. You could try it with ConfigMap first though.

-- Ryan Dawson
Source: StackOverflow