Kubernetes
kubectl
job logs
troubleshooting
DevOps

How to view logs of failed jobs with kubectl?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Kubernetes, understanding how to diagnose and debug issues with your workloads is crucial. Among the most common tasks is viewing logs for failed jobs. Whether it's a CrashLoopBackOff error or an unexpected termination, inspecting logs is often the first step in troubleshooting. In this article, we will explore how to view logs of failed jobs using kubectl.

Prerequisites

  • A Kubernetes cluster with administrative access.
  • kubectl CLI configured to interact with your Kubernetes cluster.

Step-by-Step Guide

Understanding Kubernetes Jobs

Kubernetes jobs are designed to run tasks with a defined completion cycle. Unlike Deployments or StatefulSets, Jobs are utilized for tasks that are expected to terminate, like batch processing. Failed jobs are those that do not reach completion successfully.

Identifying Failed Jobs

To locate failed jobs within your cluster, use the following command:

bash
kubectl get jobs

This command outputs a list of jobs, including their completion status. Look for jobs with 0/1 under the COMPLETIONS column and a status of FAILED.

Accessing Pod Logs

Once you've identified a failed job, you need to find the pod associated with it because logs are stored at the pod level.

  1. List pods for the job:
    After identifying the failed job, list its pods using:
bash
   kubectl get pods --selector=job-name=<job-name>

Replace <job-name> with the name of your job.

  1. View logs of the pod:
    Once you have the pod name, access its logs using:
bash
   kubectl logs <pod-name>

Replace <pod-name> with the name of your pod. This outputs the standard output and standard error logs of the container running in that pod.

Handling Multi-Container Pods

If your pod contains multiple containers, you need to specify the container name to retrieve logs:

bash
kubectl logs <pod-name> -c <container-name>

Advanced Techniques

Using Labels for Efficient Log Retrieval

Jobs often have labels that can be used for efficient querying. For example:

bash
kubectl get pods -l job-name=<job-name> --field-selector=status.phase=Failed

This command retrieves only the pods that have failed, streamlining the log retrieval process.

Viewing Previous Container Logs

In some cases, the current pod logs may not provide complete details due to restarts. To view logs from a previous instance of the container, use:

bash
kubectl logs <pod-name> -c <container-name> --previous

Monitoring Pod Events

Sometimes, logs may not show all reasons for a job failure. Viewing events related to the pod provides additional context:

bash
kubectl describe pod <pod-name>

The describe command lists events that might be associated with the pod, such as failed image pulls or node scheduling errors.

Summary Table

ActionCommand Example
List all jobskubectl get jobs
Identify pods for a jobkubectl get pods --selector=job-name=<job-name>
View logs of a single container podkubectl logs <pod-name>
Logs from a specific containerkubectl logs <pod-name> -c <container-name>
Logs from a previous container runkubectl logs <pod-name> -c <container-name> --previous
View failed pods specificallykubectl get pods --selector=job-name=<job-name> --field-selector=status.phase=Failed
Examine pod eventskubectl describe pod <pod-name>

Conclusion

Troubleshooting failed jobs in Kubernetes largely depends on efficiently accessing and interpreting pod logs. By mastering the commands and techniques discussed in this article, you will significantly improve your ability to diagnose and rectify issues occurring within your workloads. As always, integrating these diagnostic techniques into your workflows ensures a higher level of operational reliability in your Kubernetes environments.


Course illustration
Course illustration

All Rights Reserved.