Pages

Monday, 3 August 2026

Kubernetes Job object

 

A Kubernetes Job is a controller object designed to run a batch task to completion.

Unlike Deployments or ReplicaSets (which keep applications running indefinitely) or CronJobs (which trigger tasks on a schedule), a Job creates one or more Pods, executes the workload, and ensures they terminate cleanly. Once the specified number of Pods complete successfully, the Job itself is marked as complete and stops.

Standard Job Manifest


apiVersion: batch/v1
kind: Job
metadata:
  name: data-migration-job
spec:
  backoffLimit: 4             # Number of retries before marking the job failed
  completions: 1              # Number of successful pod completions required
  parallelism: 1              # How many pods run concurrently
  ttlSecondsAfterFinished: 600 # Clean up job & pods 10 minutes after completion
  template:
    spec:
      containers:
      - name: migration-task
        image: python:3.11-slim
        command: ["python", "-c", "print('Running database migration...'); import time; time.sleep(10); print('Done!')"]
      restartPolicy: OnFailure # Required: OnFailure or Never (Always is invalid)


Core Execution Patterns


Kubernetes Jobs support three primary workload execution models:

  • 1. Non-Parallel Jobs
    • Behavior: Starts a single Pod and waits for it to complete successfully.
    • Use Case: One-off database schema migrations, report generation, or administrative scripts.
  • 2. Parallel Jobs with Fixed Completions
    • Behavior: Runs multiple Pods in parallel until a total number of successful completions (spec.completions) is reached.
    • Use Case: Batch processing where $N$ independent tasks need to be completed.
  • 3. Parallel Jobs with Work Queue
    • Behavior: Pods coordinate via an external message queue (e.g., RabbitMQ, Redis, SQS). Each Pod pulls work until the queue is empty, then exits.
    • Use Case: High-throughput task processing, media transcoding, or distributed data transformation.


Key Configuration Fields


Field
  • Default
  • Description

restartPolicy
  • Required
  • Must be OnFailure (restarts container inside same Pod) or Never (spawns a new Pod on failure).

backoffLimit
  • 6
  • Maximum number of retries before marking the Job as failed.

completions
  • 1
  • Total number of successful Pod terminations needed for Job completion.

parallelism
  • 1
  • Max number of Pods allowed to run concurrently at any given moment.

activeDeadlineSeconds
  • Unlimited
  • Max time allowed for the entire Job (including retries) before terminating all running Pods.

completionMode
  • NonIndexed
  • Set to Indexed to assign each Pod a unique completion index ($0$ to $\text{completions}-1$) via environment variables.

ttlSecondsAfterFinished
  • Disabled
  • Automatically deletes the Job and its underlying Pods after $N$ seconds of finishing.

Essential kubectl Commands


Operation                                 Command
==================         ===========
Create a Job imperatively         kubectl create job my-job --image=busybox -- echo "Hello World"
Get Job status                            kubectl get jobs
Inspect job details                     kubectl describe job my-job
List Pods associated with Job   kubectl get pods --selector=batch.kubernetes.io/job-name=my-job
View logs of Job Pods              kubectl logs job/my-job
Delete Job and its Pods            kubectl delete job my-job


Jobs vs Deployments vs CronJobs        



                    ┌─────────────────────────┐
                    │      Workload Type      │
                    └────────────┬────────────┘
                                 │
           ┌─────────────────────┴─────────────────────┐
           ▼                                           ▼
   Long-Running Services                     Batch Tasks / One-Off
(Deployments, StatefulSets)                      (Jobs & CronJobs)
           │                                           │
  Maintains target Pod                       Executes task, then
  count indefinitely.                        terminates cleanly.
                                                       │
                                      ┌────────────────┴────────────────┐
                                      ▼                                 ▼
                                Single Run                         Scheduled Run
                                  (Job)                              (CronJob)


No comments:

Post a Comment