Kubernetes retrieves container logs from a container’s standard output and standard error streams. The applications running in the container must output their logs to a place read by the container logging engine (commonly STDOUT).
You can retrieve container logs with the
kubectl logs pod_name command. If the pod is a multi-container pod then the container must be specified with the
-c option with the container name
kubectl logs pod_name -c container_name
In this example, we create a pod called
logging-pod that outputs the system date and time every second.
$ kubectl run logging-pod --image=busybox:latest --command -- /bin/sh -c 'while true; do date; sleep 1; done' pod/logging-pod created $
Then the container logs are retrieved with
$ kubectl logs logging-pod Wed Apr 20 16:31:45 UTC 2022 Wed Apr 20 16:31:46 UTC 2022 $
Here are other useful container logging options:
--previous– retrieves container logs from the previous instantiation of a container, this is helpful for containers that are crash looping
-f– streams container logs
--since– prints logs since a specific time period e.g. –since 15m
--timestamps– includes timestamps
Learn more about container logging.