Lucia
Lucia

Reputation: 855

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?

Upvotes: 8

Views: 59680

Answers (10)

icaromagnago
icaromagnago

Reputation: 41

The oc logs command knows where to find the log files of your pod so you do not need to specify a log file location/path within the command.

Use the following command to copy the log for a specified project and pod to the local directory where you are running this command:

oc logs [pod_name] -n [project_name] > [outputFilename].log

Upvotes: 4

RaZ
RaZ

Reputation: 1306

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
  • <pod> is the pod name
  • <namespace> is actually the project the <pod> belongs to.

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

Identify a pod's project/namespace

  • 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

Upvotes: 5

Anupam
Anupam

Reputation: 1

Try following command.

oc logs -f podname > log-file-name

Upvotes: 0

ulix1808
ulix1808

Reputation: 1

for me the way it works was to copy the folder with the logs I was intended to get from my pod to the default path were the pod sent you when you login in the pod and then apply:

oc cp <namespace>/<pod>:<myFolder> .

Upvotes: 0

Lucia
Lucia

Reputation: 855

oc logs pod_name -n project_name > filename.log

this works for me

Upvotes: 0

GeekWoman
GeekWoman

Reputation: 1

  1. Extract the oc login command.
  2. Login into oc using login command on cli client.
  3. Navigate to namespace where pod is hosted -> oc project project-name
  4. Type in -> oc logs podname > pods_logs.txt

Log file is generated in the current directory.

Upvotes: 0

deepak prajapati
deepak prajapati

Reputation: 291

To download the file from fabric pod to your local machine

  • To Connect to the fabric instance with oc login command

    oc login url--token=<token>>

  • Check to connect pod using terminal

    oc rsh <podname>

    Just Check if it connects to POD and do some ls -lh(it should give some response)

  • To Copy the file from remote POD to your local:

    oc rsync <podname>:<path>/logs.txt localfilename

Upvotes: 0

Megadotnet
Megadotnet

Reputation: 309

login from oc cli tools, then switch to project, execute:

oc logs <podname> >> <podname>.log

Upvotes: 0

Daein Park
Daein Park

Reputation: 4703

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?

What about check /var/log/containers on the node which pods are running ? There are all container logs which are symbolic links as <pod name>_<namespace>_<container name>-<hash> format. Basically, oc logs also refers the same container logs at there.

e.g.>

node ~# ls -1 /var/log/containers
alertmanager-main-0_openshift-monitoring_alertmanager-123...789.log
alertmanager-main-0_openshift-monitoring_alertmanager-456...123.log
alertmanager-main-0_openshift-monitoring_alertmanager-proxy-789...456.log
...

Upvotes: 2

Mike
Mike

Reputation: 5182

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

Upvotes: 6

Related Questions