Kubernetes Jobs & CronJobs – Automate One-Time & Scheduled Tasks

Listen to this Post

Featured Image
Kubernetes Jobs and CronJobs are essential for automating one-time and scheduled tasks in a cluster. Here’s how they work:

🧭 CronJob Controller Layer:

  • CronJob Defined: A CronJob is scheduled with a time pattern like " ".
  • CronJob Controller: Reads the schedule and policies (e.g., concurrencyPolicy: Forbid).
  • Creates Job: When the schedule triggers, the CronJob Controller creates a Job resource.

🧰 Job Controller Layer:

  • Job Configuration: The Job has settings like completions: 1, parallelism: 1, and backoffLimit: 6.
  • Job Controller: Creates Pods based on the Job template and tracks their completions.

Pod Layer:

  • Pod Creation: Job Controller spawns a Pod to run the job.
  • Run Containers: Pod runs containers as per spec (restartPolicy: Never).
  • Success Path: If the Pod finishes successfully, it’s marked as Succeeded.
  • Failure Path: On failure, the Pod is retried based on backoffLimit.
  • Retry Logic: If retries exhaust, the Pod is marked Failed, and the Job may retry or end.

Full Kubernetes Jobs & CronJobs Explained

You Should Know:

1. Creating a Kubernetes Job

apiVersion: batch/v1 
kind: Job 
metadata: 
name: example-job 
spec: 
template: 
spec: 
containers: 
- name: busybox 
image: busybox 
command: ["echo", "Hello, Kubernetes Job!"] 
restartPolicy: Never 
backoffLimit: 4 

Apply it with:

kubectl apply -f job.yaml 

2. Creating a Kubernetes CronJob

apiVersion: batch/v1beta1 
kind: CronJob 
metadata: 
name: example-cronjob 
spec: 
schedule: "/1    " 
jobTemplate: 
spec: 
template: 
spec: 
containers: 
- name: busybox 
image: busybox 
command: ["echo", "Hello, Kubernetes CronJob!"] 
restartPolicy: OnFailure 

Apply it with:

kubectl apply -f cronjob.yaml 

3. Checking Job & CronJob Status

kubectl get jobs 
kubectl get cronjobs 
kubectl describe job <job-name> 
kubectl describe cronjob <cronjob-name> 

4. Viewing Pod Logs

kubectl logs <pod-name> 

5. Deleting Jobs & CronJobs

kubectl delete job <job-name> 
kubectl delete cronjob <cronjob-name> 

6. Debugging Failed Jobs

kubectl get pods --show-all 
kubectl describe pod <failed-pod-name> 

7. Setting Concurrency Policies

  • Allow: Multiple instances can run.
  • Forbid: Skip new runs if previous is still running.
  • Replace: Replace the existing job if a new run is due.

8. Manual CronJob Trigger

kubectl create job --from=cronjob/<cronjob-name> <manual-job-name> 

What Undercode Say:

Kubernetes Jobs and CronJobs are powerful for automating batch processes, cleanup tasks, and scheduled operations. Mastering them ensures efficient cluster management. Use `kubectl` commands to monitor and debug jobs effectively. Always set proper `backoffLimit` and `restartPolicy` to handle failures gracefully.

Expected Output:

$ kubectl get jobs 
NAME COMPLETIONS DURATION AGE 
example-job 1/1 3s 2m

$ kubectl get cronjobs 
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE 
example-cronjob /1     False 0 25s 5m 

Further Reading:

References:

Reported By: Sandip Das – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 Telegram