Cloud Native 9 min read

Mastering Kubernetes Jobs: Serial & Parallel Execution with Real‑World Examples

This guide explains how Kubernetes Job controllers run one‑off tasks, covering serial and parallel execution modes, required manifest fields, restart policies, monitoring commands, and cleanup options, complete with YAML examples and command‑line snippets.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Mastering Kubernetes Jobs: Serial & Parallel Execution with Real‑World Examples

Job Controller Overview

The Job controller runs Pods that perform a one‑time task; when the container exits successfully the Pod reaches a Completed state, while failures trigger a restart based on the configured restart policy. If a Pod is terminated unexpectedly due to node failure, it is rescheduled.

Job Execution Modes

Jobs can run either serially (single work queue) or in parallel (multiple work queues). In the serial mode only one Pod is active at a time, while the parallel mode can launch several Pods simultaneously, each handling a portion of the total work.

Creating a Job Object

The spec of a Job only requires a template field; a selector is generated automatically, making it similar to a Deployment except for this omission.

1. Define the Job manifest – the example below uses the busybox image and sleeps for 120 seconds before exiting.

cat busybox-job.yaml</code>
<code>apiVersion: batch/v1</code>
<code>kind: Job</code>
<code>metadata:</code>
<code>  name: busybox-job</code>
<code>spec:</code>
<code>  template:</code>
<code>    spec:</code>
<code>      containers:</code>
<code>      - name: busybox</code>
<code>        image: busybox:latest</code>
<code>        command: ["/bin/sh", "-c", "sleep 120s"]</code>
<code>      restartPolicy: Never

2. Apply the Job kubectl apply -f busybox-job.yaml 3. Check Job and Pod status

kubectl get job -o wide</code>
<code>kubectl get pods -l job-name=busybox-job -o wide

After 120 seconds the Pod reaches the Completed state.

The default spec.restartPolicy for Pods is Always , which is unsuitable for Jobs; use Never or OnFailure instead.

Serial Job Example

Set parallelism: 1 and completions: 5 to run five tasks one after another.

cat busybox-serial-job.yaml</code>
<code>apiVersion: batch/v1</code>
<code>kind: Job</code>
<code>metadata:</code>
<code>  name: busybox-job</code>
<code>spec:</code>
<code>  parallelism: 1</code>
<code>  completions: 5</code>
<code>  template:</code>
<code>    spec:</code>
<code>      containers:</code>
<code>      - name: busybox</code>
<code>        image: busybox:latest</code>
<code>        command: ["/bin/sh", "-c", "sleep 20s"]</code>
<code>      restartPolicy: OnFailure
kubectl apply -f busybox-serial-job.yaml

Monitor the Job with kubectl get job -w or watch the Pods.

Parallel Job Example

Set parallelism and completions to the same value (e.g., 5) to launch five Pods concurrently.

cat busybox-parallel-job.yaml</code>
<code>apiVersion: batch/v1</code>
<code>kind: Job</code>
<code>metadata:</code>
<code>  name: busybox-job</code>
<code>spec:</code>
<code>  parallelism: 5</code>
<code>  completions: 5</code>
<code>  template:</code>
<code>    spec:</code>
<code>      containers:</code>
<code>      - name: busybox</code>
<code>        image: busybox:latest</code>
<code>        command: ["/bin/sh", "-c", "sleep 20s"]</code>
<code>      restartPolicy: OnFailure
kubectl apply -f busybox-parallel-job.yaml

All Pods start at the same time; their creation timestamps are identical.

Deleting a Job

Completed Pods free resources automatically, but you can delete them manually if needed. To prevent endless restarts, configure backoffLimit (max retry attempts) and activeDeadlineSeconds (overall timeout).

cat busybox-failure-job.yaml</code>
<code>apiVersion: batch/v1</code>
<code>kind: Job</code>
<code>metadata:</code>
<code>  name: busybox-job</code>
<code>spec:</code>
<code>  backoffLimit: 5</code>
<code>  activeDeadlineSeconds: 100</code>
<code>  parallelism: 1</code>
<code>  completions: 5</code>
<code>  template:</code>
<code>    spec:</code>
<code>      containers:</code>
<code>      - name: busybox</code>
<code>        image: busybox:latest</code>
<code>        command: ["/bin/sh", "-c", "sleep 30s"]</code>
<code>      restartPolicy: OnFailure

Apply the manifest and monitor; the Job will fail after five retries or after 100 seconds, whichever occurs first.

☆ END ☆

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

KubernetesBatchJobParallel
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.