Kubernetes Jobs: Running Tasks in Kubernetes
Mesrar
Posted on February 1, 2024
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"
Managing Jobs
Check the status and details of all jobs in the current namespace:
kubectl get jobs
Viewing Job Logs
Retrieve logs from a specific pod created by a job:
kubectl logs <pod-name>
Deleting a Job
Remove a job and its associated pods:
kubectl delete job <job-name>
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
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"
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!
Posted on February 1, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.