Expose volumes in Helm just like in docker

4/6/2021

I'm creating an application that is using helm(v3.3.0) + k3s. A program in a container uses different configuration files. As of now there are just few config files (that I added manually before building the image) but I'd like to add the possibility to add them dynamically when the container is running and not to lose them once the container/pod is dead. In docker I'd do that by exposing a folder like this:

docker run [image] -v /host/path:/container/path

Is there an equivalent for helm? If not how would you suggest to solve this issue without stopping using helm/k3s?

-- Jing
docker
k3s
kubernetes
kubernetes-helm

2 Answers

4/6/2021

In Kubernetes (Helm is just a tool for it) you need to do two things to mount host path inside container:

spec:
  volumes:
  # 1. Declare a 'hostPath' volume under pod's 'volumes' key:
  - name: name-me
    hostPath:
      path: /path/on/host
  containers:
  - name: foo
    image: bar
    # 2. Mount the declared volume inside container using volume name
    volumeMounts:
    - name: name-me
      mountPath: /path/in/container

Lots of other volumes types and examples in Kubernetes documentation.

-- anemyte
Source: StackOverflow

4/6/2021

Kubernetes has a dedicated construct for holding configuration files, ConfigMaps. Helm in turn has support for Accessing Files Inside Templates which can help you copy them into ConfigMap objects. A minimal setup here would look like:

<!-- language: lang-yaml -->
# templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config
data:
  config.ini: |
{{ .Files.Get "config.ini" | indent 4 }}
<!-- language: lang-yaml -->
# templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment:
metadata: { ... }
spec:
  template:
    spec:
      volumes:
        - name: config-data
          configMap:
            name: my-config  # matches ConfigMap metadata: { name: }
      containers:
        - volumeMounts:
            - name: config-data  # matches volume name: in this file
              mountPath: /container/path

You can use Helm's templating constructs in various ways here: to dynamically construct the contents of the ConfigMap, to set an environment variable saying which file to use, and so on.

Do not use hostPath volumes here. Since Kubernetes is designed as a clustered environment, you do not have much control over which node a given pod will run on; you would have to copy these config files to every node in the cluster and try to update them all when a file changed. That's a huge maintenance problem, especially if you don't have direct filesystem access to the nodes.

-- David Maze
Source: StackOverflow