Kubernetes: how can I mount container log files to host

8/6/2018

I've an applicaion running inside pod which is created by deployment specification in k8s. As I've NFS clients in all nodes, I can mount volume from host to pod. Here is the mount-vol portion of yml file

spec:
  containers:
    volumeMounts:
    - mountPath: /app/config
      name: config-volume
    - mountPath: /var/log
      name: logs

  volumes:
  - name: config-volume
    hostPath:
      path: /nfs/config
  - name: logs
    hostPath:
      path: /nfs/logs/containerlogs

config-volume: it mounts the config files from host to pod container.

logs: it is not working. I want to mount the log folder from pod container to host folder.

-- Ratul
docker-volume
kubernetes
logging

2 Answers

8/6/2018

I don't think you mount containers to host. It is always host to pod. Make sure that the path /nfs/logs/containerLogs exist in the host so that your pod can write to it.

-- Bal Chua
Source: StackOverflow

2/20/2019

As for me, your yaml almost do work. Personally, I think your yaml need to be completed, such as:

spec:
  containers:
    volumeMounts:
    - mountPath: /app/config
      name: config-volume
    - mountPath: /var/log
      name: logs

  volumes:
  - name: config-volume
    hostPath:
      path: /nfs/config
      type: Directory
  - name: logs
    hostPath:
      path: /nfs/logs/containerlogs
      type: Directory

Also your spec missed configs image & command

As you described above, there are 2 paths to store 2 kinds of logs.

What you may also do is to assign the 2 paths in your container spec, just like the way you start your application without container.

Actually, the similar configure in your spec is as follows:

              command:
              - /application
              - --log-dir=/var/log
              - --config-log-dir=/app/config
              - --logtostderr=false
              - --v=3

You may noticed that the arguments - --logtostderr and - --v=3. If your application has similar arguments, you should set them properly.

After you do the right config, the container will logs the log to the right place.

-- xjas
Source: StackOverflow