Script Valley
Kubernetes: From Containers to Clusters
Scaling, Scheduling, and Resource ManagementLesson 5.5

Kubernetes Jobs and CronJobs: running batch and scheduled workloads

Job resource, completions, parallelism, restartPolicy OnFailure vs Never, Job cleanup, CronJob resource, cron schedule syntax, concurrencyPolicy, successfulJobsHistoryLimit, failedJobsHistoryLimit

Jobs Run Tasks to Completion

Kubernetes Job and CronJob lifecycle diagram

Deployments run services indefinitely. Jobs run a task until it completes successfully. CronJobs schedule Jobs on a cron-style schedule. Use these for database migrations, report generation, cache warming, and data processing pipelines.

Job Manifest

apiVersion: batch/v1
kind: Job
metadata:
  name: db-migrate
spec:
  completions: 1
  parallelism: 1
  backoffLimit: 3       # retry up to 3 times on failure
  template:
    spec:
      restartPolicy: OnFailure
      containers:
      - name: migrate
        image: my-app:2.0
        command: ["python", "manage.py", "migrate"]

CronJob

apiVersion: batch/v1
kind: CronJob
metadata:
  name: cleanup-job
spec:
  schedule: "0 2 * * *"    # every day at 2am
  concurrencyPolicy: Forbid  # skip if previous run still active
  successfulJobsHistoryLimit: 3
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: OnFailure
          containers:
          - name: cleanup
            image: my-app:2.0
            command: ["python", "cleanup.py"]

Jobs are not deleted automatically. Set ttlSecondsAfterFinished to auto-clean completed Jobs and prevent cluttering your cluster.