Get Kubernetes Logs With kubectl

Ekene Ejike
Ekene Ejike
Updated: August 13, 2024Published: February 9, 2024

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 kubectl logs command as follows:

Bash
$ kubectl logs <pod_name>

Easily retrieve this command using Warp’s AI Command Suggestions

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

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

Getting the logs of a pod in a namespace

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

Bash
$ kubectl get pods -A

Then use the kubectl logs command with the -n flag (short for --namespace) as follows:

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

Getting the logs of a pod 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 kubectl logs command with the -f flag (short for --follow) as follows:

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

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

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

Getting the logs of a container within a pod

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 kubectl logs command with the -c flag (short for --container) as follows:

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

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

Getting the logs from all the containers within a pod

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

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

Getting the logs of a crashed container

By default, the kubectl logs 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 kubectl logs command with the -p flag (short for --previous):

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

Getting the logs of a specified resource

To get the logs of a specified resource, such as a Deployment, a Service, a Job, etc, you can use the kubectl logs command as follows:

Bash
$ kubectl logs <resource_type> <resource_name>

For example:

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

Related articles


Execute Commands in Pods With kubectl

Learn how to execute standalone commands and start interactive shell sessions in Pods using the `kubectl exec` command.

Get Kubernetes Secrets With kubectl

Learn how to list, describe, customize, sort and filter secrets in a Kubernetes cluster by name, type, namespace, label and more using the kubectl command.

Copy Files From Pod in Kubernetes

Learn how to copy files and directories from within a Kubernetes Pod into the local filesystem using the kubectl command.

Scale Deployments in Kubernetes

Learn how to manually and automatically scale a Deployment based on CPU usage in Kubernetes using the kubectl-scale and kubectl-autoscale commands.

Tail Logs In Kubernetes

Learn how to tail and monitor Kubernetes logs efficiently to debug, trace, and troubleshoot errors more easily using the kubectl command.

Forward Ports In Kubernetes

Learn how to forward the ports of Kubernetes resources such as Pods and Services using the kubectl port-forward command.

Get Context In Kubernetes

Learn how to get information about one or more contexts in Kubernetes using the kubectl command.

Delete Kubernetes Namespaces With kubectl

Learn how to delete one or more namespaces and their related resources in a Kubernetes cluster using the kubectl command.

List Kubernetes Namespaces With kubectl

Learn how to list, describe, customize, sort and filter namespaces in a Kubernetes cluster by name, label, and more using the kubectl command.

Delete Kubernetes Deployments With kubectl

Learn how to delete Kubernetes deployments and its associated Pods using the kubectl command.

Deleting a Pod with `kubectl`

Learn how to delete a pod in Kubernetes using graceful shutdowns, labels, field selectors, and namespaces.

Create Kubernetes Namespace With `kubectl`

Learn how to create one or more Kubernetes namespaces with `kubectl`.