Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 381
Chillax Avatar asked Nov 16 '18 15:11

Chillax


People also ask

Do Helm chart has YAML files in its package?

yaml is used by many of the Helm tools, including the CLI. When generating a package, the helm package command will use the version that it finds in the Chart. yaml as a token in the package name. The system assumes that the version number in the chart package name matches the version number in the Chart.

Where are Helm files stored?

Helm charts are stored in chart repositories that are hosted in container registries, either on a local system or online.

Where should I store my Helm charts?

After you've updated the repository, they can use the helm repo update command to get the latest chart information. Under the hood, the helm repo add and helm repo update commands are fetching the index. yaml file and storing them in the $XDG_CACHE_HOME/helm/repository/cache/ directory.


1 Answers

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.

like image 188
Ryan Dawson Avatar answered Sep 23 '22 13:09

Ryan Dawson