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.
kubectlCLI 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:
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.
- List pods for the job:After identifying the failed job, list its pods using:
Replace <job-name> with the name of your job.
- View logs of the pod:Once you have the pod name, access its logs using:
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:
Advanced Techniques
Using Labels for Efficient Log Retrieval
Jobs often have labels that can be used for efficient querying. For example:
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:
Monitoring Pod Events
Sometimes, logs may not show all reasons for a job failure. Viewing events related to the pod provides additional context:
The describe command lists events that might be associated with the pod, such as failed image pulls or node scheduling errors.
Summary Table
| Action | Command Example |
| List all jobs | kubectl get jobs |
| Identify pods for a job | kubectl get pods --selector=job-name=<job-name> |
| View logs of a single container pod | kubectl logs <pod-name> |
| Logs from a specific container | kubectl logs <pod-name> -c <container-name> |
| Logs from a previous container run | kubectl logs <pod-name> -c <container-name> --previous |
| View failed pods specifically | kubectl get pods --selector=job-name=<job-name> --field-selector=status.phase=Failed |
| Examine pod events | kubectl 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.

