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!

Top comments (0)