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.
Try this,
tail logs from pods
kubectl --tail <"no of lines"> logs <"pod_name">
Example :
kubectl --tail 100 logs app_pod
kubctl logs -f=true [pod-name] -c [container-name]
If you just have a single container over the pod, container name is not necessary else use the container name with -c option. -f i.e. follow is false by default. If you do not set it to true you will get a snapshot of your container logs.
kubectl logs -f <pod-id>
You can use the -f
flag:
-f, --follow=false: Specify if the logs should be streamed.
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.
k8s_pod=some_pod
kubectl get pods -w $k8s_pod | while read LOGLINE
do
[[ "${LOGLINE}" == *"Running"* ]] && pkill -P $ kubectl
done
for line in $(kubectl get pods | grep $k8s_pod | awk '{print $1}'); do
kubectl logs -f $line | tee logfile
done
tail logfile | grep successful!
RESULT=$?
exit $RESULT
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.
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