Cloud Native 8 min read

Master Kubernetes Jobs and CronJobs: A Step‑by‑Step Guide

This article explains what Kubernetes Jobs and CronJobs are, outlines common use cases, and provides detailed, code‑rich instructions for creating, configuring, and managing them—including parallel execution, random naming, and key parameters—so you can automate batch and ad‑hoc tasks in a cloud‑native environment.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Kubernetes Jobs and CronJobs: A Step‑by‑Step Guide

What are Kubernetes Jobs?

Kubernetes Jobs are designed for short‑lived or batch workloads that run to completion, unlike Deployments, ReplicaSets, or DaemonSets which maintain continuously running pods.

Use Cases

Batch processing : run a task on a schedule or once, such as reading files from storage and processing them.

Ops / ad‑hoc tasks : execute scripts for database cleanup, cluster backup, or other maintenance activities.

Creating a Job

In this example we use an Ubuntu container that runs a shell script with a numeric argument determining how many times the script echoes a message.

Step 1: Create a custom Docker image and a job.yaml manifest with the argument set to 100:

apiVersion: batch/v1
kind: Job
metadata:
  name: kubernetes-job-example
  labels:
    jobgroup: jobexample
spec:
  template:
    metadata:
      name: kubejob
      labels:
        jobgroup: jobexample
    spec:
      containers:
      - name: c
        image: devopscube/kubernetes-job-demo:latest
        args: ["100"]
      restartPolicy: OnFailure

Step 2: Deploy the job with kubectl apply -f job.yaml.

Step 3: Check the job status using kubectl get jobs.

Step 4: List the pods with kubectl get po.

Step 5: View the job logs, replacing the pod name with the one shown in the output: kubectl logs kubernetes-job-example-xxxxx -f You should see the script output repeated the specified number of times.

Running Multiple Pods in Parallel

To run a job across several pods concurrently, add completions and parallelism to the manifest:

completions: 6
parallelism: 2

Example manifest:

apiVersion: batch/v1
kind: Job
metadata:
  name: kubernetes-parallel-job
  labels:
    jobgroup: jobexample
spec:
  completions: 5
  parallelism: 2
  template:
    metadata:
      name: kubernetes-parallel-job
      labels:
        jobgroup: jobexample
    spec:
      containers:
      - name: c
        image: devopscube/kubernetes-job-demo:latest
        args: ["100"]
      restartPolicy: OnFailure

Generating Random Job Names

To avoid name collisions when creating multiple jobs, add the generateName field to metadata:

apiVersion: batch/v1
kind: Job
metadata:
  generateName: kube-job-
  labels:
    jobgroup: jobexample

Each execution will prepend kube-job- with a random suffix.

Creating a CronJob

CronJobs run Jobs on a schedule. Define a cron expression in the schedule field. schedule: "0,15,30,45 * * * *" Example CronJob manifest:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: kubernetes-cron-job
spec:
  schedule: "0,15,30,45 * * * *"
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            app: cron-batch-job
        spec:
          restartPolicy: OnFailure
          containers:
          - name: kube-cron-job
            image: devopscube/kubernetes-job-demo:latest
            args: ["100"]

Deploy with kubectl create -f cron-job.yaml, list with kubectl get cronjobs, and view logs via the associated pods.

To run a CronJob manually, create a Job from the CronJob template:

kubectl create job --from=cronjob/kubernetes-cron-job manual-cron-job

Key Job Parameters

failedJobHistoryLimit & successfulJobsHistoryLimit : control how many failed or successful job records are retained.

backoffLimit : maximum retry attempts for a failed pod.

activeDeadlineSeconds : hard limit on how long a job may run (e.g., 60 seconds for a one‑minute CronJob).

By following these steps and configurations you can create and manage Kubernetes Jobs and CronJobs to automate batch processing and ad‑hoc operational tasks.

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.

KubernetesDevOpsYAMLCronJobJobs
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.