Get log files from Pod (containers) before it get killed?

8/30/2019

I am having 2 containers inside one pod, 1 is DB and 1 is application. When my application container started but, not ready to accept traffic by this time the container generates some log files and I want those log files for further application investigation. As the container does not passes readiness probe and it failed to start so, the pod is getting killed so, the log files also getting deleted so how I can get those log files before the pod is getting killed??

-- Dashrath Mundkar
kubernetes
kubernetes-helm
kubernetes-pod
openshift
openshift-origin

5 Answers

9/3/2019

A one-off solution is to install stern and then run the following command in a separate terminal before starting your application container:

stern <pod_name>

You can then pipe the output to local storage for further analysis

-- klementtt
Source: StackOverflow

9/4/2019

You can get logs of a container inside a pod by using -c CONTAINER flag of oc logs command.

If you know the name of the container in your pod, you can get the logs of that container with a command like below

for i in {1..100}; do oc get pods -o name | grep -v "deploy" | xargs -i oc logs -p {} -c CONTAINER_NAME; done

Of course it will be good if you run this in an empty project only with your failing pod.

-- Aytunc Beken
Source: StackOverflow

9/5/2019

create a persistent volume and mount it on the log directory of containers.You will get logs even after pod is killed. Few volume type that you can be used for this task are -

  • azure disk
  • hostpath
  • gce persistent disk The simplest one is hostpath but not preferred .
-- shubham_asati
Source: StackOverflow

8/30/2019

The quickest solution is probably to just mount a volume of type hostPath to your pod. Then, bind this volume to your log directory.
See the documentation here.

Just keep in mind that this solution is certainly not the cleanest one. It's just for debug purpose.

-- Marc ABOUCHACRA
Source: StackOverflow

8/30/2019

What about forward logs to STDOUT and STDERR? That would be the cleanest solution (however, it requires some changes in your code). https://kubernetes.io/docs/reference/kubectl/cheatsheet/#interacting-with-running-pods

-- Stepan Vrany
Source: StackOverflow