LinuxCommandLibrary

kubectl-logs

Fetch container logs in Kubernetes

TLDR

Show logs for a single-container pod

$ kubectl logs [pod_name]
copy

Show logs for a specified container in a pod
$ kubectl logs [[-c|--container]] [container_name] [pod_name]
copy

Show logs for all containers in a pod
$ kubectl logs --all-containers=[true] [pod_name]
copy

Stream pod logs
$ kubectl logs [[-f|--follow]] [pod_name]
copy

Show pod logs newer than a relative time like 10s, 5m, or 1h
$ kubectl logs --since=[relative_time] [pod_name]
copy

Show the 10 most recent logs in a pod
$ kubectl logs --tail=[10] [pod_name]
copy

Show all pod logs for a given deployment
$ kubectl logs deployment/[deployment_name]
copy

SYNOPSIS

kubectl logs [-f] [-p] [--container=CONTAINER] [--since=DURATION] [--tail=LINE_COUNT] POD [CONTAINER]

PARAMETERS

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

-p, --previous
    If true, print the logs for the previous instance of the container in a pod if it exists.

--container=CONTAINER
    Print the logs of this container. Defaults to the single container if only one exists in the pod.

--since=DURATION
    Only return logs newer than a relative duration like 5s, 2m, or 3h. Defaults to all logs. Only one of since-time / since may be specified.

--since-time=TIME
    Only return logs after a specific date (RFC3339). Defaults to all logs. Only one of since-time / since may be specified.

--tail=LINE_COUNT
    Lines of recent log file to display.

--timestamps
    Include timestamps on each line in the log output.

POD
    Name of the pod to get logs from.

CONTAINER
    Optional: Name of the container within the pod to get logs from. If omitted, the command retrieves logs from all containers in the pod.

DESCRIPTION

The `kubectl logs` command is a powerful tool for diagnosing issues and monitoring applications running within a Kubernetes cluster. It allows you to view the logs generated by containers within a pod. This command is crucial for debugging, identifying errors, and understanding the behavior of your applications. It supports retrieving logs from a single container, multiple containers, or even following the logs in real-time. You can also specify time constraints to retrieve logs only from a specific period. Furthermore, options are available to control the format and presentation of the logs to improve readability. Understanding and effectively utilizing `kubectl logs` is essential for any Kubernetes administrator or developer.

CAVEATS

The `kubectl logs` command relies on the container's logging driver configuration. If logs are not being written to standard output or standard error, `kubectl logs` may not capture them. For complex logging setups, consider using dedicated log aggregation solutions.

EXAMPLES

Get logs for a pod:
`kubectl logs my-pod`
Get logs for a specific container in a pod:
`kubectl logs my-pod my-container`
Follow logs for a pod:
`kubectl logs -f my-pod`
Get logs from the previous instance of a container:
`kubectl logs -p my-pod`
Get the last 10 lines of logs:
`kubectl logs --tail=10 my-pod`
Get logs from the last hour:
`kubectl logs --since=1h my-pod`
Get logs and display timestamps:
`kubectl logs --timestamps my-pod`

SEE ALSO

kubectl(1), kubectl-exec(1), journalctl(1)

Copied to clipboard