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:
Where:
- [.inline-code]pod[.inline-code] is the name of the pod you want to get the logs of.
[#easily-recall-syntax-with-ai]Easily retrieve this command using Warp’s AI Command Search[#easily-recall-syntax-with-ai]
If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Search feature:
Entering [.inline-code]kubernetes get pod logs[.inline-code] in the AI Command Search 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-pod-logs-in-a-namespace]Getting the logs of a pod in a namespace[#get-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]):
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:
Where:
- [.inline-code]pod[.inline-code] is the name of the pod you want to get the logs of.
- [.inline-code]namespace[.inline-code] is the name of the sub-cluster that contains this pod.
[#get-container-logs-in-a-pod]Getting the logs of a container within a pod[#get-container-logs-in-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 [.inline-code]kubectl logs[.inline-code] command with the [.inline-code]-c[.inline-code] flag (short for [.inline-code]--container[.inline-code]) as follows:
Where:
- [.inline-code]pod[.inline-code] is the name of the pod you want to get the logs of.
- [.inline-code]container[.inline-code] is the name of the container running within that pod.
Note that the [.inline-code]-c[.inline-code] flag is not necessary when there is only one container running within a pod.
[#get-all-container-logs-in-a-pod]Getting the logs from all the containers within a pod[#get-all-container-logs-in-a-pod]
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:
[#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]):.
[#get-logs-in-real-time]Getting the logs of a pod in real-time[#get-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:
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:
[#get-deployment-logs]Getting the logs of a deployment [#get-deployment-logs]
There are times when a deployment is done in a kubernetes cluster, and the pods do not get started after some time.
To get the list of all running deployments, you can use the following [.inline-code]kubectl get[.inline-code] command with the [.inline-code]-A[.inline-code] flag:
And get the logs of a specific deployment using the [.inline-code]kubectl log[.inline-code] command as follows:
Where:
- [.inline-code]deployment[.inline-code] is the name of the problematic deployment obtained using the [.inline-code]kubectl get[.inline-code] command.
[#get-job-logs]Getting the logs of a job[#get-job-logs]
In Kubernetes, a Job is a resource that is used to create and manage a single, finite task that runs to completion.
To get all the jobs in a Kubernetes cluster, you can use the following [.inline-code]kubectl get[.inline-code] command:
And get the logs of a specific job using the [.inline-code]kubectl logs[.inline-code] command:
Where:
- [.inline-code]job[.inline-code] is the name of the job obtained using the [.inline-code]kubectl get[.inline-code] command.
[#get-service-logs]Getting the logs of a service[#get-service-logs]
In Kubernetes, services are in charge of connecting an applications front-end to the backend for separate deployments.
To troubleshoot a service, use the following [.inline-code]kubectl get[.inline-code] command to fetch all services deployed in your kubernetes cluster:
Then, to get the logs of a specific service use the [.inline-code]kubectl logs[.inline-code] command:
Where:
- [.inline-code]service[.inline-code] is the name of the service obtained from the output of [.inline-code]kubectl get[.inline-code] command.