Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the logs of a POD in openshift to local file

I have my spring boot application running on Openshift as container built using docker Image. I have enabled the logging in my application and prints all the logs. Now i want to examine the log files of the POD to check for any errors, since one my request is failing. I do know about the command line option oc logs -f <podname> That just prints the log into cmd prompt, but i want whole log to be copied from server to local file. So that i can find some particular lines or errors. Is is possible?

like image 347
Lucia Avatar asked Oct 03 '19 10:10

Lucia


People also ask

Where are OpenShift logs stored?

OpenShift Container Platform uses Fluentd to collect container and node logs. By default, the log collector uses the following sources: journald for all system logs. /var/log/containers/*.

How do I download a file from OpenShift terminal to local?

To copy files from the container to the local machine, you can use the oc rsync command. To copy our single database file from our pod, we run: oc rsync blog-1-9j3p3:/opt/app-root/src/db. sqlite3 .

Where are pods logs stored?

These logs are usually located in the /var/log/containers directory on your host. If a container restarts, kubelet keeps logs on the node. To prevent logs from filling up all the available space on the node, Kubernetes has a log rotation policy set in place.

How do I view logs in OpenShift?

You can view the logs for various resources, such as builds, deployments, and pods by using the OpenShift CLI (oc) and the web console. Resource logs are a default feature that provides limited log viewing capability. To enhance your log retrieving and viewing experience, it is recommended that you install OpenShift Logging.

How do I get the log file of a pod project?

Just Check if it connects to POD and do some ls -lh (it should give some response) Extract the oc login command. Login into oc using login command on cli client. Navigate to namespace where pod is hosted -> oc project project-name Log file is generated in the current directory.

How to view pods logs in OC?

The oc logs pod/<pod name> command can be used to view a pods log. The -f or --follow option can be used to stream the log in real time. The -p or --previous flag can be used to see previous logs.

How to create a log file for an OC project?

Login into oc using login command on cli client. Navigate to namespace where pod is hosted -> oc project project-name Log file is generated in the current directory. oc cp <namespace>/<pod>:<myFolder> .


3 Answers

You can copy files in and out of pods using the rsync command.

Or use the logs command like you are and just redirect to a file so you can edit it locally:

oc logs  <podname> &> /path/to/file
like image 108
Mike Avatar answered Oct 17 '22 04:10

Mike


From your oc CLI tool execute:

oc logs pod_name -n project_name > filename.log
like image 23
icaromagnago Avatar answered Oct 17 '22 04:10

icaromagnago


oc rsync

Try oc rsync as suggested above:

oc rsync <pod>:/path/to/file localfilename

However in my case I got:

WARNING: cannot use rsync: rsync not available in container <pod>

oc cp

So I tried try oc cp, and it worked:

oc cp <namespace>/<pod>:/path/to/file local_filename

Without specifying the namespace the copy command will not work (and display no error message), so I had to which project the pod belongs.

Identify a pod's project/namespace

  • <pod> is the pod name
  • <namespace> is actually the project the <pod> belongs to.
  • use oc project to list current project, or
  • oc projects to list all projects
  • or or search for the pod name in all projects oc get pods --all-namespaces | egrep <pod>

Important Note

 # !!!Important Note!!!
 # Requires that the 'tar' binary is present in your container
 # image.  If 'tar' is not present, 'kubectl cp' will fail.
 # about my environment
 # oc version
 # oc 3.6, openshift 3.7, kubernetes 1.7

Docs:

OpenShift 4.3 Oficial Documentation - copying files

like image 30
ionescu77 Avatar answered Oct 17 '22 04:10

ionescu77