Kubernetes: Injecting multiple Kubernetes volumes to the same directory

https://blog.sebastian-daschner.com/entries/multiple-kubernetes-volumes-directory

Kubernetes config maps and secrets allow use to inject configuration files into containers. If we want multiple config entries that originate from different config maps or secrets to be injected into the same location, we are required to specify a sub path:

kind: Deployment
apiVersion: apps/v1
metadata:
  name: hello-world
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: hello-world
    spec:
      containers:
      - name: hello-world
        image: docker.example.com/app:1
        volumeMounts:
        - name: hello-world-config-volume
          mountPath: /config/application.properties
          subPath: application.properties
          readOnly: true
        - name: hello-world-credentials-volume
          mountPath: /config/credentials.properties
          subPath: credentials.properties
          readOnly: true
      volumes:
      - name: hello-world-config-volume
        configMap:
          name: hello-world-config
      - name: hello-world-credentials-volume
        secret:
          secretName: hello-world-credentials

This example will create two volumes from the contents of the config map hello-world-config and the secret hello-world-credentials.

Imagine, these resources have the following contents:

kind: ConfigMap
apiVersion: v1
metadata:
  name: hello-world-config
data:
  application.properties: |
    greeting=Hello
    name=World

---

kind: Secret
apiVersion: v1
metadata:
  name: hello-world-credentials
data:
  credentials.properties: <base64>
type: Opaque

The example will mount the file contents of the key application.properties of the config map to a file with the same name under /config/ and will mount the secret value credentials.properties as the second file under that directory. The application will be able to access both files read-only.

The subPath declaration also allows to mount a single volume into the pod multiple times with different sub paths.