Kubernetes Volumes - Dynamic path

12/21/2017

I want my applications to write log files at a host location, so I'm mounting a hostPath volume. But all applications try to write logs using the same file name.

I'd like to separate the files into folders named after the Pod names, but I see nowhere in the documentation how to implement it:

  volumes:
  - name: logs-volume
    hostPath:
      path: /var/logs/apps/${POD_NAME}
      type: DirectoryOrCreate

In the (not working) example above, apps should write files to the POD_NAME folder.

Is it possible?

-- gcstr
kubernetes

2 Answers

12/21/2017

You might be able to use the downward API for this (read more at https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/#use-pod-fields-as-values-for-environment-variables). You would set up a variable such as:

env:
  - name: MY_POD_NAME
    valueFrom:
      fieldRef:
        fieldPath: metadata.name

Then in the volume path use the variable with parentheses like:

volumes:
  - name: logs-volume
    hostPath:
      path: /var/logs/apps/$(MY_POD_NAME)
      type: DirectoryOrCreate

However, I'm not sure if you can use the environment variables in places other than command and arguments. If that is the case, you could modify your application code or entrypoint script to prepend the environment variable to the location where it writes logs.

An alternative to environment variables would be using a mapped volume that contains files with the information (https://kubernetes.io/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/). Your application would then need to read the data at that volume or the previous defined environment variable and prepend the name to the path it writes logs to.

-- Andy Shinn
Source: StackOverflow

11/30/2018

An alpha feature that might help is available in kubernetes 1.11. I haven't tested it, but it apparently allows something like:

    volumeMounts:
    - mountPath: /var/log
      name: logs
      subPathFrom:
        fieldRef:
          fieldPath: metadata.name
  volumes:
  - name: logs
    hostPath:
      path: /var/logs/apps/
-- James Healy
Source: StackOverflow