What Is a Kubernetes Job?

beginner|jobsdevopssrebackend developerCKACKAD
TL;DR

A Kubernetes Job creates one or more Pods and ensures they run to successful completion. Unlike Deployments that keep Pods running indefinitely, Jobs are designed for finite tasks like database migrations, batch processing, or data exports.

Detailed Answer

A Job is a Kubernetes workload controller designed for tasks that should run to completion and then stop. While Deployments keep Pods running forever, Jobs ensure a specified number of Pods successfully terminate.

Basic Job Example

apiVersion: batch/v1
kind: Job
metadata:
  name: db-migration
spec:
  template:
    spec:
      containers:
        - name: migrate
          image: myapp/migrate:v3.2
          command: ["python", "manage.py", "migrate"]
          env:
            - name: DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: db-credentials
                  key: url
          resources:
            requests:
              cpu: "250m"
              memory: "256Mi"
            limits:
              cpu: "1"
              memory: "512Mi"
      restartPolicy: Never
  backoffLimit: 3

Key requirements:

  • restartPolicy must be Never or OnFailure (not Always)
  • backoffLimit controls how many times the Job retries on failure

Job Lifecycle

  1. Job is created — the controller creates a Pod
  2. Pod runs — executes the workload
  3. Pod succeeds (exit code 0) — Job is marked Complete
  4. Pod fails (non-zero exit code) — controller retries up to backoffLimit
  5. All retries exhausted — Job is marked Failed
# Check Job status
kubectl get jobs
# NAME           COMPLETIONS   DURATION   AGE
# db-migration   1/1           45s        2m

# View the Pod created by the Job
kubectl get pods -l job-name=db-migration
# NAME                 READY   STATUS      RESTARTS   AGE
# db-migration-x7k2q   0/1     Completed   0          2m

# View logs from the completed Pod
kubectl logs db-migration-x7k2q

restartPolicy Behavior

The restartPolicy controls what happens when a container fails:

| Policy | Behavior | Use Case | |---|---|---| | Never | Job controller creates a new Pod | When you need a fresh environment on retry | | OnFailure | kubelet restarts the container in the same Pod | When restarting in-place is safe and faster |

With restartPolicy: Never, each retry creates a new Pod, so you can inspect failed Pods for debugging. With OnFailure, the container restarts in the same Pod, which is faster but loses the previous container's filesystem.

Common Use Cases

| Task | Description | |---|---| | Database migrations | Run schema changes before deploying a new version | | Data processing | Process a batch of files, records, or messages | | Report generation | Generate periodic reports and export to storage | | Backups | Create database or volume snapshots | | ML training | Train a model to completion | | Integration tests | Run a test suite against a staging environment |

Cleanup

Completed Jobs and their Pods remain in the cluster by default. Clean them up with:

# Delete a specific Job and its Pods
kubectl delete job db-migration

# Delete all completed Jobs
kubectl delete jobs --field-selector status.successful=1

Or use automatic TTL-based cleanup (covered in the job-ttl-after-finished question).

Jobs vs CronJobs

A Job runs once when created. A CronJob creates Jobs on a schedule, similar to cron in Linux. If you need periodic batch tasks, use a CronJob that creates Jobs at specified intervals.

Why Interviewers Ask This

Interviewers ask this to check if you understand how Kubernetes handles batch and one-off workloads, which are common in real-world applications.

Common Follow-Up Questions

What happens when a Job Pod fails?
The Job controller creates a new Pod to retry. The number of retries is controlled by spec.backoffLimit (default 6). After exceeding the limit, the Job is marked as Failed.
How is a Job different from a Deployment?
Deployments maintain a desired replica count indefinitely. Jobs run Pods to completion and stop. Deployments use restartPolicy: Always; Jobs require restartPolicy: Never or OnFailure.
Are Job Pods cleaned up automatically?
By default, completed Job Pods remain in Completed state. You can use ttlSecondsAfterFinished to auto-delete them, or manually clean up with kubectl delete job.

Key Takeaways

  • Jobs are for finite workloads that run to completion, not long-running services.
  • Jobs support retry logic via backoffLimit and can run multiple completions in parallel.
  • Completed Pods remain for log inspection unless TTL cleanup is configured.

Related Questions

You Might Also Like