Kubernetes Docker logs

5/25/2018

I am using the following command to check logs in Kubernetes.

kubectl logs pod_name -n namespace

It is printing all the logs from the beginning.

  1. Is there anyway to tail the logs or check logs between the given window?
  2. Is it possible to rotate docker logs based on size or date?

Thanks

-- user1578872
kubernetes

5 Answers

5/25/2018

Is there anyway to tail the logs or check logs between the given window?

To tail the logs, use the -foption

kubectl logs pod_name -n namespace -f

Is it possible to role docker logs based on size or date?

You can query logs x-lines ago or since a time range. Take a look at the —tail and —since options

kubectl logs [-f] [-p] POD [-c CONTAINER]

Examples Return snapshot logs from pod nginx with only one container

kubectl logs nginx

Return snapshot of previous terminated ruby container logs from pod web-1

kubectl logs -p -c ruby web-1

Begin streaming the logs of the ruby container in pod web-1

kubectl logs -f -c ruby web-1

Display only the most recent 20 lines of output in pod nginx

kubectl logs --tail=20 nginx

Show all logs from pod nginx written in the last hour

kubectl logs --since=1h nginx

https://kubernetes-v1-4.github.io/docs/user-guide/kubectl/kubectl_logs/

-- erstaples
Source: StackOverflow

4/25/2020

kubectl logs pod_name --since=2m --timestamps

-- Siva S
Source: StackOverflow

11/30/2018

The "tail" functionality of "kubectl logs" can be used with this convenient GUI frontend: https://retrospective.centeractive.com/blog_retrospective_5_0_0.html

The frontend leverages several functions of "kubectl", for example:

  • allows to filter the "tail" output in several ways ("check logs between the given window" from question #1)
  • allows the visual configuration of a group of Kuberenetes pods via labels. The log data the pods in a group can then be "multi-tailed" in a single view.

Disclosure: I helped in making this frontend.

-- horistack
Source: StackOverflow

5/2/2019

Yes, we can extract the log by using the since like below -

kubectl logs --since=48h podname > 24Logs.txt

Then you can easily check the logs for specific time within last 48 hours.

-- Kripa Yadav
Source: StackOverflow

5/25/2018

1: yes, you can tail or filter by date.

As easy as running kubectl logs --help

Options:
  -c, --container='': Print the logs of this container
  -f, --follow=false: Specify if the logs should be streamed.
      --include-extended-apis=true: If true, include definitions of new APIs via calls to the API server. [default true]
      --interactive=false: If true, prompt the user for input when required.
      --limit-bytes=0: Maximum bytes of logs to return. Defaults to no limit.
      --pod-running-timeout=20s: The length of time (like 5s, 2m, or 3h, higher than zero) to wait until at least one
pod is running
  -p, --previous=false: If true, print the logs for the previous instance of the container in a pod if it exists.
  -l, --selector='': Selector (label query) to filter on.
      --since=0s: Only return logs newer than a relative duration like 5s, 2m, or 3h. Defaults to all logs. Only one of
since-time / since may be used.
      --since-time='': Only return logs after a specific date (RFC3339). Defaults to all logs. Only one of since-time /
since may be used.
      --tail=-1: Lines of recent log file to display. Defaults to -1 with no selector, showing all log lines otherwise
10, if a selector is provided.
      --timestamps=false: Include timestamps on each line in the log output

2: Docker stores the container logs in host in the path /var/lib/docker/containers/{ContainerId} so you could copy/truncate the logs directly.

That won't have any impact in the container or the pod.

-- Koe
Source: StackOverflow