kubectl exec commands are not recorded in pod's bash history

Viewed 827

Is there a way for the kubectl exec commands to be recorded by history command in the pod? I want to collect all the commands executed on the container by monitoring history command.

Example: kubectl exec podname -n namespace -c container -- bash -c "ls" ==> Can this be recorded by history command.

1 Answers

A couple of things to clarify in order to get the full context of this behavior:

First, kubectl exec is a neat API-based (Warning: Medium member's story) wrapper for docker exec.

This is essential as it means that it'll use the Kubernetes API to relay your commands to Docker. This implies that whatever behavior, shell-related in this case, is directly linked to how Docker implemented the command execution within containers, and has not much to do with kubectl.

The second thing to have in mind is the command itself: history, which is an shell feature and not a separate binary.

The implementation depends on the shell used, but generally it caches the command history in memory before writing it to the history file after the session is closed. This action can be "forced" using history features but can vary depending on the shell implementation and might not be an uniform, reliable approach while working with the docker exec command.

Considering these things and that your request seems to be aiming to monitor actions performed in your containers, maybe a better approach would be to use Linux audit to record commands executed by users.

Not only this doesn't rely on the aforementioned points, it writes logs to a file and this allows you to use your Kubernetes logging strategy to pick them, exporting them to whatever you're using as log sink, facilitating posterior inspection.

Related