Access Kubernetes pod's log files from inside the pod?

6/7/2018

I'm currently migrating a legacy server to Kubernetes, and I found that kubectl or dashboard only shows the latest log file, not the older versions. In order to access the old files, I have to ssh to the node machine and search for it.

In addition to being a hassle, my team wants to restrict access to the node machines themselves, because they will be running pods from many different teams and unrestricted access could be a security issue.

So my question is: can I configure Kubernetes (or a Docker image) so that these old (rotated) log files are stored in some directory accessible from inside the pod itself?

  • Of course, in a pinch, I could probably just execute something like run_server.sh | tee /var/log/my-own.log when the pod starts... but then, to do it correctly, I'll have to add the whole logfile rotation functionality, basically duplicating what Kubernetes is already doing.
-- jick
docker
kubernetes
log-rotation
logging

1 Answer

6/7/2018

So there are a couple of ways to and scenarios for this. If you are just interested in the log of the same pod from before last restart, you can use the --previous flag to look at logs:

kubectl logs -f <pod-name-xyz> --previous

But since in your case, you are interested in looking at log files beyond one rotation, here is how you can do it. Add a sidecar container to your application container:

 volumeMounts:
    - name: varlog
      mountPath: /tmp/logs
  - name: log-helper
    image: busybox
    args: [/bin/sh, -c, 'tail -n+1 -f /var/log/*.log']
    volumeMounts:
    - name: varlog
      mountPath: /tmp/logs
volumes:
  - name: varlog
    hpostPath: /var/log

This will allow the directory which has all logs from /var/log directory from host to /tmp/log inside the container and the command will ensure that content of all files is flushed. Now you can run:

kubectl logs <pod-name-abc> -c count-log-1

This solution does away with SSH access, but still needs access to kubectl and adding a sidecar container. I still think this is a bad solution and you consider of one of the options from the cluster level logging architecture documentation of Kubernetes such as 1 or 2

-- Vishal Biyani
Source: StackOverflow