DEV Community

Mesrar
Mesrar

Posted on

Kubernetes Jobs: Running Tasks in Kubernetes

Creating a Job

Initiate a job that runs a specific command in a container:

kubectl create job busybox --image=busybox -- /bin/sh -c "echo hello; sleep 30; echo world"
Enter fullscreen mode Exit fullscreen mode

Managing Jobs
Check the status and details of all jobs in the current namespace:

kubectl get jobs

Enter fullscreen mode Exit fullscreen mode

Viewing Job Logs
Retrieve logs from a specific pod created by a job:

kubectl logs <pod-name>

Enter fullscreen mode Exit fullscreen mode

Deleting a Job
Remove a job and its associated pods:

kubectl delete job <job-name>

Enter fullscreen mode Exit fullscreen mode

Understanding Job Properties
Jobs have several key properties to consider:

completions
: Number of pod completions expected.
backoffLimit: Maximum number of retries before considering a job as failed.

parallelism
: Number of pods to run in parallel.
activeDeadlineSeconds: Maximum time for the job to run.
restartPolicy: Pod restart policy, usually set to "OnFailure" or "Never".

Important Notes

In a job, the default value for the restart property is "Never."
A pod created by a job must have its restartPolicy set to "OnFailure" or "Never."

CronJobs: Scheduled Jobs

Managing CronJobs
List all CronJobs in the current namespace:

kubectl get cronjobs

Enter fullscreen mode Exit fullscreen mode

Creating a CronJob
Establish a CronJob that runs on a specified schedule:

kubectl create cronjob busybox --image=busybox --schedule="*/1 * * * *" -- /bin/sh -c "date; echo Hello from Kubernetes cluster"
Enter fullscreen mode Exit fullscreen mode

Specifying CronJob Properties
In a CronJob, there are three spec sections to note - one for the CronJob itself, one for the Job, and one for the Pod. Important properties include:

spec -> successfulJobHistoryLimit: Number of successful jobs to retain in history.
spec -> failedJobHistoryLimit: Number of failed jobs to retain in history.
Explore the capabilities of Kubernetes Jobs and CronJobs, enhancing your control over task execution and scheduling within your cluster.

Happy Kuberneting!

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more