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:
$ 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):
$ kubectl get pods -A
Then use the kubectl logs command with the -n flag (short for --namespace) as follows:
$ 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:
$ 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:
$ 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:
$ 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:
$ 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):
$ 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:
$ kubectl logs <resource_type> <resource_name>
For example:
$ kubectl logs deployment <deployment_name>
$ kubectl logs job <job_name>
$ kubectl logs services <service_name>
Written by
Ekene Ejike
Filed Under
Related Articles
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.
Forward Ports In Kubernetes
Learn how to forward the ports of Kubernetes resources such as Pods and Services using the kubectl port-forward command.
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.
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.
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.
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.
How To List Events With kubectl
Learn how to list and filter events in Kubernetes cluster by namespace, pod name and more using the kubectl command.
Kubernetes vs Docker: The Backbone of Modern Backend Technologies
Lean the fundamentals of the Kubernetes and Docker technologies and how they interplay with each other.
Set Context With kubectl
Learn how to create, modify, switch, and delete a context in Kubernetes using the kubectl config command.
List Pods With kubectl
Learn how to list and filter Kubernetes Pods by name, namespaces, labels, manifests, and more using the kubectl command.