kubectl logs - continuously
Solution 1
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
Solution 2
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.
Solution 3
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.
Solution 4
Try this,
tail logs from pods
kubectl --tail <"no of lines"> logs <"pod_name">
Example :
kubectl --tail 100 logs app_pod
Solution 5
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
Related videos on Youtube
npr
Updated on May 07, 2022Comments
-
npr about 2 years
kubectl logs <pod-id>
gets latest logs from my deployment - I am working on a bug and interested to know the logs at runtime - How can I get continuous stream of logs ?
edit: corrected question at the end.
-
ddtraveller about 6 years'successful!' may need to change depending on your pod.
-
ddtraveller about 6 yearsI took those first two parts from several other stackoverflow posts so I can't take credit for those entirely but the combo I hope will serve others well...
-
Alexander Mills almost 5 yearswhat about logs for service or anything other than the pods?
-
Alexander Mills almost 5 yearscan there be more than one container in a pod?
-
pferrel over 4 yearsthis works for a short time then the logs stop. I have to ctrl-c to get out of kubectl, then restart them. This shows more logs after but stops again. Anyone know why the logs stop in random spots when they are obviously still being generated by the pod?
-
grokpot over 4 years@AlexanderMills yes, this is the "sidecar" pattern
-
duyn9uyen over 2 years@pferrel, did you ever figure this out? I'm having the same issue.
-
Yu-Ju Hong over 2 yearsCould be related to log rotation: github.com/kubernetes/kubernetes/issues/59902
-
gchandra over 2 years@duyn9uyen I think it's because logs stop coming in from the server. even without restarting kubectl, logs start coming in automatically
-
shadow0359 almost 2 yearsI'm facing similar issues. I have tried kubectl attach (mentioned in answer below). but occasionally the logs stop there as well.