Terminus
Get Kubernetes Logs With kubectl

Get Kubernetes Logs With kubectl

In Kubernetes, the easiest way to troubleshoot a pod, deployment, or service in a cluster stuck in an error state such as CrashloopBackoff, ImagePullBackoff, or Pending, is to access and review its logs.

The short answer

In Kubernetes, to get the logs of a specific pod, you can use the [.inline-code]kubectl logs[.inline-code] command as follows:

$ kubectl logs <pod_name>

[#easily-recall-syntax-with-ai] Easily retrieve this command using Warp’s AI Command Suggestions [#easily-recall-syntax-with-ai]

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:

Entering [.inline-code]kubernetes get pod logs[.inline-code] in the AI Command Suggestions will prompt an [.inline-code]kubectl[.inline-code] command that can then quickly be inserted into your shell by doing [.inline-code]CMD+ENTER[.inline-code].

[#get-a-pod-logs-in-a-namespace] Getting the logs of a pod in a namespace [#get-a-pod-logs-in-a-namespace]

To get the logs of a pod in a namespace, you can start by first listing all the pods using the [.inline-code]kubectl get pods[.inline-code] command with the [.inline-code]-A[.inline-code] flag (short for [.inline-code]--all-namespaces[.inline-code]):

$ kubectl get pods -A

Then use the [.inline-code]kubectl logs[.inline-code] command with the [.inline-code]-n[.inline-code] flag (short for [.inline-code]--namespace[.inline-code]) as follows:

$ kubectl logs <pod_name>-n<namespace>

[#get-pod-logs-in-real-time] Getting the logs of a pod in real-time [#get-pod-logs-in-real-time]

To quickly troubleshoot a pod issue, you can continuously stream the logs of that pod into the terminal in real-time using the [.inline-code]kubectl logs[.inline-code] command with the [.inline-code]-f[.inline-code] flag (short for [.inline-code]--follow[.inline-code]) as follows:

$ kubectl logs <pod> -n <namespace> -f

Note that this flag can also be combined with the [.inline-code]-c[.inline-code] flag to stream the logs of a specific container within the pod:

$ kubectl logs <pod> -n <namespace> -c <container> -f

[#get-a-single-container-logs] Getting the logs of a container within a pod [#get-a-single-container-logs]

In some cases, pods may contain more than one container. To get the logs of a specific container within a pod, you can use the [.inline-code]kubectl logs[.inline-code] command with the [.inline-code]-c[.inline-code] flag (short for [.inline-code]--container[.inline-code]) as follows:

$ kubectl logs <pod_name> -n <namespace> -c <container_name>

Note that the [.inline-code]-c[.inline-code] flag is not necessary when there is only one container running within a pod.

[#get-all-containers-logs] Getting the logs from all the containers within a pod [#get-all-containers-logs]

Alternatively, to get the logs from all the containers running within a pod at once, you can use the [.inline-code]--all-containers[.inline-code] flag as follows:

$ kubectl logs<pod_name>-n<namespace> --all-containers=true

[#get-crashed-container-logs] Getting the logs of a crashed container [#get-crashed-container-logs]

By default, the [.inline-code]kubectl logs[.inline-code] command only retrieves the logs of a running container within a pod. When a container crashes, you can still troubleshoot or get the logs from a previous running instance of that pod using the [.inline-code]kubectl logs[.inline-code] command with the [.inline-code]-p[.inline-code] flag (short for [.inline-code]--previous[.inline-code]):

$ kubectl logs <pod_name>-n<namespace> -p 

[#get-logs-of-other-resources] Getting the logs of a specified resource [#get-logs-of-other-resources]

To get the logs of a specified resource, such as a Deployment, a Service, a Job, etc, you can use the [.inline-code]kubectl logs[.inline-code] command as follows:

$ kubectl logs <resource_type> <resource_name>

For example:

$ kubectl logs deployment <deployment_name>
$ kubectl logs job <job_name>
$ kubectl logs services <service_name>