Skip to content

Kubernetes Jobs, CronJobs & Batch Processing

DodaTech 3 min read

In this tutorial, you'll learn about Kubernetes Jobs, CronJobs & Batch Processing. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

Kubernetes Jobs manage batch workloads by running pods to completion, with CronJobs providing time-based scheduling and patterns for parallel and work queue processing.

What You'll Learn

This tutorial covers creating Jobs and CronJobs, parallel job patterns, work queues with indexed jobs, handling job failures with backoff limits, and batch data processing.

Why It Matters

Batch processing handles critical tasks like data exports, image processing, report generation, and database maintenance. Kubernetes Jobs provide reliable execution with automatic retries and parallel processing.

Real-World Use

Spotify uses Kubernetes Jobs for nightly data processing pipelines that analyze listening patterns. Google Cloud Batch uses Kubernetes Jobs to run scientific computing and Machine Learning training jobs.

Creating a Basic Job

A Job runs one or more pods until a specified number successfully complete.

apiVersion: batch/v1
kind: Job
metadata:
  name: data-export
spec:
  completions: 1
  parallelism: 1
  backoffLimit: 4
  template:
    spec:
      containers:
      - name: exporter
        image: myapp/exporter:1.0
        command: ["python", "export.py", "--date", "2024-06-21"]
      restartPolicy: Never
# Create the job
kubectl apply -f job.yaml

# Watch job status
kubectl get jobs -w

# View pod logs
kubectl logs job/data-export

Parallel Job Patterns

Fixed Completion Count

Runs a fixed number of pods in parallel.

apiVersion: batch/v1
kind: Job
metadata:
  name: parallel-process
spec:
  completions: 10
  parallelism: 3
  template:
    spec:
      containers:
      - name: worker
        image: myapp/worker:1.0
      restartPolicy: Never

This creates 10 total pods, running up to 3 at a time.

Work Queue Pattern

Pods pull work from a queue and run until the queue is empty.

apiVersion: batch/v1
kind: Job
metadata:
  name: queue-processor
spec:
  completions: 1
  parallelism: 5
  template:
    spec:
      containers:
      - name: worker
        image: myapp/queue-worker:1.0
        env:
        - name: QUEUE_NAME
          value: "processing-queue"
      restartPolicy: Never

Set completions to 1 for this pattern so the job succeeds when all pods exit successfully. The workers exit when the queue is empty.

Indexed Jobs

Indexed jobs assign a unique index to each pod.

apiVersion: batch/v1
kind: Job
metadata:
  name: indexed-processor
spec:
  completions: 5
  parallelism: 2
  completionMode: Indexed
  template:
    spec:
      containers:
      - name: worker
        image: myapp/worker:1.0
        command:
        - "python"
        - "process_chunk.py"
        - "$(JOB_COMPLETION_INDEX)"
      restartPolicy: Never

Each pod receives a JOB_COMPLETION_INDEX environment variable from 0 to 4, allowing each pod to process a specific data shard.

CronJobs

CronJobs schedule Jobs on a time-based cron schedule.

apiVersion: batch/v1
kind: CronJob
metadata:
  name: nightly-report
spec:
  schedule: "0 3 * * *"
  concurrencyPolicy: Forbid
  startingDeadlineSeconds: 300
  successfulJobsHistoryLimit: 3
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: reporter
            image: myapp/reporter:1.0
            command: ["python", "generate_report.py"]
          restartPolicy: Never
# Create the cronjob
kubectl apply -f cronjob.yaml

# List cronjobs
kubectl get cronjobs

# View triggered jobs
kubectl get jobs --watch

# Suspend a cronjob
kubectl patch cronjob nightly-report -p '{"spec":{"suspend":true}}'

Handling Job Failures

spec:
  backoffLimit: 6
  activeDeadlineSeconds: 3600
  ttlSecondsAfterFinished: 86400

backoffLimit controls retries. activeDeadlineSeconds sets a maximum execution time. ttlSecondsAfterFinished cleans up completed pods.

Practice Questions

  1. What is the difference between completions and parallelism? completions is the total number of successful pod runs. parallelism is how many pods run simultaneously.

  2. How do indexed jobs distribute work among pods? Each pod gets a JOB_COMPLETION_INDEX environment variable that identifies which data shard to process.

  3. What does concurrencyPolicy: Forbid do in a CronJob? It prevents a new job from starting if the previous one is still running.

  4. How do you clean up completed Job pods automatically? Set ttlSecondsAfterFinished in the Job spec to delete pods after a specified time.

  5. What happens when a Job reaches its backoffLimit? The Job is marked as Failed and no more pods are created.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro