Terminus
Getting Kubernetes Logs With kubectl

Getting 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>

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]):

$ 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>-n <namespace>

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:

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

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:

$ kubectl logs  <podname>-n<pod 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>-n<namespace>-p 

[#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:

$ 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-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:

$ kubectl get deployment -A

And get the logs of a specific deployment using the [.inline-code]kubectl log[.inline-code] command as follows:

 $ kubectl logs deployment <deployment>

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:

$ kubectl get jobs

And get the logs of a specific job using the [.inline-code]kubectl logs[.inline-code] command:

$ kubectl logs job <job>

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:

$ kubectl get services

Then, to get the logs of a specific service use the [.inline-code]kubectl logs[.inline-code] command:

$ kubectl logs services <service>

Where:

  • [.inline-code]service[.inline-code] is the name of the service obtained from the output of [.inline-code]kubectl get[.inline-code] command.