4

I download a pod's log, it has size bigger then 1G, how to shrike/truncate the pod's log? so I can download it faster?

PS: I need erase the old part of logs so I can free the disk space

Yu Jiaao
  • 4,444
  • 5
  • 44
  • 57
  • do you want all the logs or the most recent? – damitj07 Oct 14 '19 at 08:37
  • If you have to do this often, I suggest you use a log shipper to push the logs outside of the cluster so you don't lose them, and then implement some sort of log rotation on the node using [`logrotate`](https://linux.die.net/man/8/logrotate) or a similar tool – d4nyll Nov 18 '19 at 20:27

2 Answers2

9

kubectl logs --tail=1000 to show only the most recent 1000 lines. Adjust as needed.

--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.

coderanger
  • 52,400
  • 4
  • 52
  • 75
  • I need erase the old part of logs so I can free the disk space – Yu Jiaao Oct 14 '19 at 08:54
  • 1
    Kubernetes does not handle this, it's up to your underlying systems. Generally most Kubernetes installers include logrotate with some basic configs but you would need to look at your specific installer for more info. – coderanger Oct 14 '19 at 08:57
7

Also to add to Coderangers answer if you want a time based truncate. This is how you can get the last 1 hour's logs.

kubectl logs --since=1h nginx

Shows all logs from pod nginx written in the last hour.


Update Post Comment.

The pod and container logs are stored in respective applications paths. Refer to this answer. https://stackoverflow.com/a/50872881/5617140

Another solution is the truncation of the docker logs in each for your kubernetes nodes.

sudo sh -c "du -ch /var/lib/docker/containers/*/*-json.log | grep total"
This will give total size of docker logs.

sudo sh -c "truncate -s 0 /var/lib/docker/containers/*/*-json.log"
Above command will truncate all the log files. 

Note* - This is a workaround solution. Make sure you backup the important logs before doing this.

damitj07
  • 2,689
  • 1
  • 21
  • 40