0
0
Kubernetesdevops~7 mins

Jobs and CronJobs for batch processing in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to run tasks that do a job once or on a schedule, like sending reports or cleaning up files. Kubernetes Jobs and CronJobs help you run these tasks reliably without keeping them running all the time.
When you want to run a task once and make sure it finishes successfully, like a database backup.
When you need to run a task repeatedly on a schedule, like sending daily emails.
When you want to run batch jobs that process data and then stop.
When you want Kubernetes to retry a task if it fails until it succeeds.
When you want to automate maintenance tasks without manual intervention.
Config File - job-cronjob.yaml
job-cronjob.yaml
apiVersion: batch/v1
kind: Job
metadata:
  name: example-job
spec:
  template:
    spec:
      containers:
      - name: example
        image: busybox
        command: ["/bin/sh", "-c", "echo Hello from Job; sleep 5"]
      restartPolicy: Never
  backoffLimit: 3
---
apiVersion: batch/v1
kind: CronJob
metadata:
  name: example-cronjob
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: example
            image: busybox
            command: ["/bin/sh", "-c", "date; echo Hello from CronJob"]
          restartPolicy: Never

This file defines two Kubernetes resources:

  • Job: Runs a simple task once that prints a message and sleeps for 5 seconds. It will retry up to 3 times if it fails.
  • CronJob: Runs a task every minute that prints the current date and a message. It uses a cron schedule format.
  • restartPolicy: Never means containers won't restart automatically on failure; Kubernetes manages retries.
Commands
This command creates the Job and CronJob resources in Kubernetes from the configuration file.
Terminal
kubectl apply -f job-cronjob.yaml
Expected OutputExpected
job.batch/example-job created cronjob.batch/example-cronjob created
This command lists all Jobs to check if the example job has started or completed.
Terminal
kubectl get jobs
Expected OutputExpected
NAME COMPLETIONS DURATION AGE example-job 1/1 5s 10s
This command lists all CronJobs to verify the example CronJob is scheduled correctly.
Terminal
kubectl get cronjobs
Expected OutputExpected
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE example-cronjob */1 * * * * False 0 <none> 10s
This command watches pods to see the Job and CronJob create pods when they run their tasks.
Terminal
kubectl get pods --watch
Expected OutputExpected
NAME READY STATUS RESTARTS AGE example-job-abcde 0/1 Completed 0 15s example-cronjob-xyz12 0/1 Completed 0 1s
--watch - Keeps the command running to show live updates of pod status
Key Concept

Jobs run tasks once and ensure completion; CronJobs run tasks on a schedule using cron syntax.

Common Mistakes
Setting restartPolicy to Always in Job or CronJob pods
This causes pods to restart endlessly, which is not suitable for batch jobs that should finish after running once.
Always set restartPolicy to Never or OnFailure for Jobs and CronJobs.
Not specifying backoffLimit in Job spec
Without backoffLimit, Kubernetes retries failed jobs indefinitely, which can cause resource waste.
Set backoffLimit to a reasonable number like 3 to limit retries.
Using wrong cron schedule format in CronJob
Incorrect cron syntax causes the CronJob not to run as expected or fail to create.
Use standard cron format with five fields: minute hour day month weekday.
Summary
Use kubectl apply to create Jobs and CronJobs from YAML files.
Check Jobs with kubectl get jobs and CronJobs with kubectl get cronjobs.
Watch pods to see batch tasks run and complete using kubectl get pods --watch.