gcstr
gcstr

Reputation: 1537

Kubernetes Volumes - Dynamic path

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?

Upvotes: 7

Views: 6338

Answers (2)

Yasser
Yasser

Reputation: 695

As of kubernetes 1.17 this is supported using subPathExpr. See https://kubernetes.io/docs/concepts/storage/volumes/#using-subpath-expanded-environment for details.

Upvotes: 3

James Healy
James Healy

Reputation: 15168

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/

Upvotes: 2

Related Questions