Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kubectl logs - continuously

People also ask

How do you find the continuous log in Kubernetes?

The default logging tool is the command ( kubectl logs ) for retrieving logs from a specific pod or container. Running this command with the --follow flag streams logs from the specified resource, allowing you to live tail its logs from your terminal.

How do I view kubectl logs?

To get Kubectl pod logs, you can access them by adding the -p flag. Kubectl will then get all of the logs stored for the pod. This includes lines that were emitted by containers that were terminated.

How do you get logs of all pods in Kubernetes?

If you want to show logs of all pods, you can use -l and specify a lable, but at the same time -f won't be used. "but this will choose one pod of the deployment, not all pods" -> true and I've spent a lot of time debugging my app before realizing that not all logs were being displayed.

How can I see the logs of a pod?

Checking the logs of a running pod All that you need to do to do that is to run the following command: kubectl logs nginx-7d8b49557c-c2lx9.


kubectl logs -f <pod-id>

You can use the -f flag:

-f, --follow=false: Specify if the logs should be streamed.

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#logs


kubectl logs --help will guide you:

Example:

# Begin streaming the logs of the ruby container in pod web-1
kubectl logs -f -c ruby web-1

Flags:

-f, --follow[=false]: Specify if the logs should be streamed.

You can also add --since=10m or so start from that relative time ago.


I needed to access the logs of a long running pod, and -f began streaming logs from days ago, which would have taken hours to get to where I needed to view ( just the last couple minutes or so ).

There is a --since=10m flag, but that didn't seem to work for me.

What did wonders was --tail=100, where 100 is the number of recent lines to display.


Try this,

tail logs from pods

kubectl --tail <"no of lines"> logs <"pod_name">

Example :

kubectl --tail 100 logs app_pod


If you want to get the stream of logs from a multi pod app you can use kubetail, example:

kubectl get pods

NAME                   READY     STATUS    RESTARTS   AGE
app2-v31-9pbpn         1/1       Running   0          1d
app2-v31-q74wg         1/1       Running   0          1d

kubetail app2

With that command, kubetail is tailing the logs from pod app2-v31-9pbpn and app2-v31-q74wg


wait for kubes to spin up pod then move on...

k8s_pod=some_pod
kubectl get pods -w $k8s_pod | while read LOGLINE
do
   [[ "${LOGLINE}" == *"Running"* ]] && pkill -P $$ kubectl
done

tail logs

for line in $(kubectl get pods | grep $k8s_pod | awk '{print $1}'); do
    kubectl logs -f $line | tee logfile
done

look for success indicator

tail logfile | grep successful! 
RESULT=$?
exit $RESULT