kubectl 1.37 reference · Debugging

kubectl logs Command (kubectl)

Print a container's logs from a pod, deployment or job; follow them, limit lines, or read the previous crashed container.

kubectl 1.37

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

kubectl logs POD|TYPE/NAME [-c CONTAINER] [--previous] [-f] [--tail N] [--since 10m]

Parameters

ParameterDescription
-c CONTAINER Container in a multi-container pod.
--previous Logs of the last terminated container (after a crash).
-f Follow.
--tail N / --since 10m Limit the output.
-l SELECTOR Logs from all pods matching labels.

Examples

Last lines of a pod
kubectl create namespace rc-ref-logs > /dev/null
trap 'kubectl delete namespace rc-ref-logs --wait=false > /dev/null' EXIT
kubectl -n rc-ref-logs run job1 --restart=Never --image=busybox:1.37 -- sh -c 'for i in 1 2 3; do echo step $i; done' > /dev/null
kubectl -n rc-ref-logs wait --for=jsonpath='{.status.phase}'=Succeeded pod/job1 --timeout=120s > /dev/null
kubectl -n rc-ref-logs logs job1 --tail 2

Output

step 2
step 3

Run this on your computer — see “Get Started” for setup.

Logs by deployment
kubectl create namespace rc-ref-logs2 > /dev/null
trap 'kubectl delete namespace rc-ref-logs2 --wait=false > /dev/null' EXIT
kubectl -n rc-ref-logs2 create deployment api --image=busybox:1.37 -- sh -c 'echo api started; sleep 3600' > /dev/null
kubectl -n rc-ref-logs2 rollout status deployment/api --timeout=120s > /dev/null; sleep 1
kubectl -n rc-ref-logs2 logs deployment/api

Output

api started

Run this on your computer — see “Get Started” for setup.

Was this page helpful?