I can see the logs for a particular pod by running 'kubectl logs podName'. I have also seen that logs contains an option --log-dir flag, but it doesn't seem to be working. Is there some kind of configuration I can change, logs will be saved to a particular file on my host machine?
3 Answers
kubectl logs pod_name > app.log
for example, if you have a kube pod named app-6b8bdd458b-kskjh
and you intend to save the logs from this pod to a file name app.log
then the command should be
kubectl logs app-6b8bdd458b-kskjh > app.log

- 768
- 2
- 9
- 26

- 401
- 4
- 5
-
3This might not work for long running processes with verbose logs, especially on GKE where logs are flushed regularly. So I would recommend launching `kubectl log -f
> pod.log` right after pod startup instead. – Fabrice Jammes Jun 02 '21 at 04:47 -
you can also use selectors for easy access. eg. kubectl logs -l app=
– igelr Nov 08 '22 at 09:20
Note: This may not be an direct answer of how to make the logs go to the host machine, but it does provide a way to get logs onto the machine. This may not be the best answer, but it's what I know so I'm sharing. Please answer if you have a more direct solution.
You can do this using fluentd. Here's a tutorial about how to set it up. You can configure it to write to a file in a mounted hostdir or have it write to S3. It also allows you to aggregate all your logs from all your containers which may or may not be useful. Combined with ElasticSearch and Kibana you can put together a pretty strong logging stack. It will depend on your use-case of course though.

- 2,102
- 1
- 15
- 30
-
I am investigating this now. The Kubernetes logs are really pushing for Google Cloud logging or ElasticSearch/Kibana, but my ultimate goal is to develop my own logging stack after I figure out how to get the logs on my local machine first. Thank you for answering. I'll be sure to report back once I've played with fluentd – mbhatia4336 Dec 10 '16 at 23:44
Grafana Loki allows to collect, store and access pods logs in a developer-friendly way.

- 2,275
- 1
- 26
- 39