Tailing few lines from huge logs of kubectl logs -f

KubernetesTailKubectl

Kubernetes Problem Overview


kubectl logs -f pod shows all logs from the beginning and it becomes a problem when the log is huge and we have to wait for a few minutes to get the last log. Its become more worst when connecting remotely. Is there a way that we can tail the logs for the last 100 lines of logs and follow them?

Kubernetes Solutions


Solution 1 - Kubernetes

In a cluster best practices are to gather all logs in a single point through an aggregator and analyze them with a dedicated tool. For that reason in K8S, log command is quite basic.

Anyway kubectl logs -h shows some options useful for you:

# 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

Some tools with your requirements (and more) are available on github, some of which are:

Solution 2 - Kubernetes

Try kubectl logs -f pod --tail=10

Solution 3 - Kubernetes

To fetch tail lines from logs of a pod with multi containers.

kubectl logs <pod name> --all-containers=true --tail=10

To fetch tail lines from logs of pods within an application:

kubectl logs --selector app=<your application> --tail=10

(ex:if your application has 3 pods then output of above command can be 30 logs 10 of each pod logs)

Solution 4 - Kubernetes

You can also follow logs from the end if you are testing something:

kubectl logs my-pod-name --follow

This will work just like running tail -f in bash or other shells.

Solution 5 - Kubernetes

You can ues this way to get the first 10 lines

kubectl logs my-pod-name  -n my-ns | head -n 10

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTinkaal GogoiView Question on Stackoverflow
Solution 1 - KubernetesNicola BenView Answer on Stackoverflow
Solution 2 - KubernetesKun LiView Answer on Stackoverflow
Solution 3 - KubernetesGoli NikithaView Answer on Stackoverflow
Solution 4 - KubernetesrpenaView Answer on Stackoverflow
Solution 5 - Kubernetesbruce wyenView Answer on Stackoverflow