- Published on
Looking at Application Logs in Kubernetes
- Authors
- Name
- Yair Mark
- @yairmark
At some point when running apps in Kubernetes you will need to look at the logs of a running application. To do this we use the following command:
kubectl logs yourPodName -f
This will tail the logs. It works in a similar way to how the tail
shell command works. You can also specify the -c
flag if you have a pod with multiple containers where you specify the name of the container after that flag.
A trick I use when tailing logs is pushing enter a few times after the current output and leaving a gap. What this does is lets you visually separate what has just happened with what is happening now as there is a clear gap.
The logs command like most shell commands can be piped to other commands. I often do this leaving off the follow flag -f
so that I can more easily look through the logs:
kubectl logs yourPodName | less
In case you were curious piping to less +F
will keep tailing the running logs that use the -f
flag but as soon as you stop tailing (ctrl+c
) then try following again (shift-f
) - the tailing does not continue.
This command also has the --since
flag which lets you specify how far back the logs should go and can be paired with the follow flag to keep following. This is very useful for a long running app where leaving this out will result in the entire contents of the log being output up until the point you are at now:
kubectl logs yourPodName --since 1h -f