Try Sailor Cloud - 25% off!

Claim Now
Back to all posts

Kubernetes Jobs and CronJobs: Batch Processing Guide

Kubernetes Jobs and CronJobs: Batch Processing Guide

Not all workloads run continuously. Jobs handle one-time tasks, while CronJobs handle scheduled tasks.

Jobs

Creating a Job

apiVersion: batch/v1
kind: Job
metadata:
  name: backup-job
spec:
  completions: 1
  parallelism: 1
  backoffLimit: 4
  template:
    spec:
      containers:
      - name: backup
        image: backup-tool
        command: ["backup.sh"]
      restartPolicy: Never
# Create job imperatively
kubectl create job backup --image=busybox -- /bin/sh -c "echo done"

Job Parameters

ParameterDescription
completionsNumber of successful completions needed
parallelismNumber of pods running in parallel
backoffLimitNumber of retries before marking failed
activeDeadlineSecondsMaximum job runtime

Multiple Completions

spec:
  completions: 5    # Run 5 times
  parallelism: 2    # 2 pods at a time

CronJobs

Creating a CronJob

apiVersion: batch/v1
kind: CronJob
metadata:
  name: daily-report
spec:
  schedule: "0 2 * * *"  # 2 AM daily
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: report
            image: report-generator
          restartPolicy: OnFailure
# Create cronjob imperatively
kubectl create cronjob hourly-backup --image=backup --schedule="0 * * * *" -- /bin/sh -c "backup.sh"

Cron Schedule Format

┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of month (1 - 31)
│ │ │ ┌───────────── month (1 - 12)
│ │ │ │ ┌───────────── day of week (0 - 6)
│ │ │ │ │
* * * * *

Common Schedules

ScheduleExpression
Every minute* * * * *
Every hour0 * * * *
Daily at midnight0 0 * * *
Weekly Sunday0 0 * * 0
Monthly0 0 1 * *

Managing Jobs

# List jobs
kubectl get jobs

# Check job pods
kubectl get pods --selector=job-name=my-job

# View job logs
kubectl logs job/my-job

# Delete completed jobs
kubectl delete job my-job

Practice Jobs and CronJobs

Jobs appear frequently in CKAD exams. Practice at Sailor.sh.

Start Free Practice