Study guide

3.4 Batch Workloads: Jobs & CronJobs

Certified Kubernetes Administrator CKA

Track your progress as you read

Finished sections get a check mark.

1 Chapter 1: CKA Exam Environment & Cluster Architecture

1.1 CKA Exam Mechanics, Scoring & Remote Terminal Environment 1.2 Kubernetes Control Plane & Worker Node Components 1.3 etcd Architecture, Clustering & Raft Consensus 1.4 Initializing Clusters with Kubeadm 1.5 Highly Available (HA) Control Plane Topologies 1.6 Cluster Lifecycle & Step-by-Step Upgrades 1.7 etcd Snapshot Backup and Restore Procedures

2 Chapter 2: Cluster Extensions, Packaging & RBAC

2.1 Kubernetes TLS Certificates & PKI Infrastructure 2.2 Managing Kubeconfig Files, Users, & Contexts 2.3 Installing Cluster Components with Helm & Kustomize 2.4 Role-Based Access Control (RBAC): Roles & RoleBindings 2.5 ClusterRoles & ClusterRoleBindings 2.6 Custom Resource Definitions (CRDs) & Operators 2.7 Pod Security Standards (PSS) & Admission Controllers

3 Chapter 3: Workloads, Lifecycle & Pod Scheduling

3.1 Pod Lifecycle, Multi-Container Patterns & Init Containers 3.2 Deployments: Rolling Updates, Rollouts & Rollbacks 3.3 DaemonSets & StatefulSets Architecture 3.4 Batch Workloads: Jobs & CronJobs 3.5 Managing ConfigMaps & Sensitive Data with Secrets 3.6 Resource Requests, Limits & Namespace ResourceQuotas 3.7 NodeSelectors, Node Affinity & Anti-Affinity 3.8 Node Taints & Pod Tolerations 3.9 Autoscaling: Horizontal Pod Autoscaler (HPA) & Custom Metrics

4 Chapter 4: Services, Networking & Ingress Architecture

4.1 Kubernetes IP-per-Pod Model & CNI Plugins (Flannel, Calico, Cilium) 4.2 Services Deep Dive: ClusterIP, NodePort, LoadBalancer & EndpointSlices 4.3 Kube-Proxy Service Dataplane: iptables, nftables, IPVS & eBPF 4.4 CoreDNS Configuration, Resolution & Custom Forwarding 4.5 NetworkPolicies: Ingress, Egress & Default-Deny Isolation 4.6 Ingress Architecture, Controllers & Path-Based Routing 4.7 Gateway API: GatewayClass, Gateway, HTTPRoute & Traffic Splitting 4.8 External Traffic Ingress: NodePort, Cloud LoadBalancers & MetalLB

5 Chapter 5: Storage Architecture & Persistent Volumes

5.1 Ephemeral Volumes, EmptyDir, HostPath & Config Volumes 5.2 PersistentVolumes (PV) & PersistentVolumeClaims (PVC) Lifecycle 5.3 StorageClasses, Dynamic Provisioning & VolumeBindingModes 5.4 Storage Access Modes, Reclaim Policies & Volume Expansion 5.5 Container Storage Interface (CSI) Architecture & Drivers 5.6 StatefulSet VolumeClaimTemplates & Persistent Storage Workflows

6 Chapter 6: Advanced Troubleshooting, Monitoring & Maintenance

6.1 Systematic Kubernetes Troubleshooting Methodology 6.2 Node Failure, Kubelet Diagnostics & Systemd Services 6.3 Control Plane Failures, Static Pods & Component Logs 6.4 etcd Troubleshooting, Quorum Loss & Database Defragmentation 6.5 Pod Startup Failures, CrashLoopBackOff & OOMKilled Diagnostics 6.6 Workload Scheduling Failures & Resource Constraint Resolution 6.7 Container & Node Logging Architecture, Journalctl & Fluentd 6.8 Metrics Server, Resource Utilization & Kubectl Top 6.9 Service, Endpoint & Ingress Connectivity Troubleshooting 6.10 Storage Failures: PVC Pending, Mount Failures & Multi-Attach Errors 6.11 NetworkPolicy Isolation & CoreDNS Resolution Diagnostics

3.4 Batch Workloads: Jobs & CronJobs

Key Takeaways Explain This Topic AI

Jobs manage non-continuous batch tasks, running Pods until a specified number of successful completions (spec.completions) is achieved.

Job restartPolicy must be explicitly set to OnFailure or Never, and backoffLimit governs exponential retry attempts before marking the Job Failed.

Indexed Jobs (completionMode: Indexed) inject a unique JOB_COMPLETION_INDEX environment variable (0 to completions-1) into each Pod for distributed data partitioning.

CronJobs schedule periodic Jobs using standard 5-field cron expressions, managed by concurrencyPolicy (Allow, Forbid, Replace).

startingDeadlineSeconds limits how late the controller may start a missed CronJob occurrence, while history limits and ttlSecondsAfterFinished control cleanup of completed Jobs.

Last updated: August 2026

Batch Workloads: Jobs & CronJobs

While Services and Deployments run long-lived, continuous processes, batch workloads run to completion and exit. Kubernetes provides Jobs for finite batch operations (ETL pipelines, database schema migrations, video transcoding) and CronJobs for recurring, time-based scheduled tasks (nightly backups, report generation, cache warming).

1. Kubernetes Jobs Architecture

A Job creates one or more Pods and ensures that a specified number of them successfully terminate with exit code 0 . +-----------------------------------------------------------------------------------------+

  • | JOB EXECUTION MODES |
  • | |
  • | 1. NON-INDEXED JOB (Work Queue) |
  • | completions: 3, parallelism: 2 |
  • | [Pod 1 (Worker)] ---> Exit 0 |
  • | [Pod 2 (Worker)] ---> Exit 0 |
  • | [Pod 3 (Worker)] ---> Exit 0 (Job Complete: 3/3 Succeeded) |
  • | |
  • | 2. INDEXED JOB (Static Partitioning) |
  • | completions: 4, parallelism: 2, completionMode: Indexed |
  • | [Pod Index 0: JOB_COMPLETION_INDEX=0] (Processes chunk 0-250MB) |
  • | [Pod Index 1: JOB_COMPLETION_INDEX=1] (Processes chunk 250-500MB) |
  • | [Pod Index 2: JOB_COMPLETION_INDEX=2] (Processes chunk 500-750MB) |
  • | [Pod Index 3: JOB_COMPLETION_INDEX=3] (Processes chunk 750MB-1GB) |
  • +-----------------------------------------------------------------------------------------+
  • Core Job Specification Fields apiVersion: batch/v1
  • kind: Job

metadata:

name: db-backup-job

spec:

  • completions: 3
  • parallelism: 2
  • backoffLimit: 4
  • activeDeadlineSeconds: 1200
  • ttlSecondsAfterFinished: 300

template:

spec:

restartPolicy: OnFailure

containers:

  • name: backup
  • image: postgres:16-alpine
  • command: ["pg_dump", "-h", "db-svc", "-U", "admin", "-f", "/backup/data.sql"]
  • Detailed Field Mechanics

completions : Total number of Pods that must exit with 0 for the Job to be marked complete. Defaults to 1 .

parallelism : Maximum number of Pods allowed to run concurrently at any given moment. Defaults to 1 .

backoffLimit : Number of retries before marking the Job as Failed . Defaults to 6 . Pod retries use exponential backoff (10s, 20s, 40s, and so on), capped at 6 minutes between retries.

activeDeadlineSeconds : Hard maximum duration for the Job. If the Job does not finish within this time, all active Pods are terminated and the Job status is set to DeadlineExceeded .

ttlSecondsAfterFinished : Automatically deletes the completed or failed Job and its associated Pods after the specified number of seconds, preventing etcd clutter.

restartPolicy : Must be either OnFailure or Never . It cannot be Always (which is reserved for continuous services).

OnFailure : The container restarts inside the same Pod (retaining the node allocation).

Never : The container is not restarted; kubelet fails the Pod, and the Job controller creates a brand new Pod on a potentially different node.

Indexed Jobs ( completionMode: Indexed )

When completionMode: Indexed is set, Kubernetes injects an environment variable JOB_COMPLETION_INDEX (ranging from 0 to completions - 1 ) into each Pod. This allows worker Pods to know their exact shard/partition without coordinating via an external message broker.

2. Kubernetes CronJobs Architecture

A CronJob creates Jobs on a repeating schedule using standard 5-field cron syntax. +-----------------------------------------------------------------------------------------+

  • | CRON SCHEDULE EXPRESSION |
  • | |
  • | ┌───────────── minute (0 - 59) |
  • | │ ┌───────────── hour (0 - 23) |
  • | │ │ ┌───────────── day of month (1 - 31) |
  • | │ │ │ ┌───────────── month (1 - 12) |
  • | │ │ │ │ ┌───────────── day of week (0 - 6) (Sunday to Saturday) |
  • | │ │ │ │ │ |
  • | * * * * * |
  • | |
  • | Example: "0 2 * * 1-5" -> Run at 02:00 AM every weekday (Monday through Friday) |

+-----------------------------------------------------------------------------------------+ apiVersion: batch/v1

kind: CronJob

metadata:

name: nightly-report

spec:

  • schedule: "0 1 * * *"
  • timeZone: "America/New_York"
  • concurrencyPolicy: Forbid
  • startingDeadlineSeconds: 200
  • successfulJobsHistoryLimit: 3
  • failedJobsHistoryLimit: 1
  • suspend: false

jobTemplate:

spec:

template:

spec:

restartPolicy: OnFailure

containers:

  • name: reporter
  • image: python:3.11-slim
  • command: ["python", "generate_reports.py"]
  • 3. Concurrency Policies in CronJobs

The concurrencyPolicy dictates how the CronJob controller handles a new schedule trigger if the previous Job execution has not yet completed. +-----------------------------------------------------------------------------------------+

  • | CRONJOB CONCURRENCY POLICIES |
  • | |
  • | 1. Allow (Default): |
  • | Job 1 (01:00) ----------------------------------> Still running at 02:00 |
  • | Job 2 (02:00) ----------------------------------> Runs concurrently with Job 1 |
  • | |
  • | 2. Forbid: |
  • | Job 1 (01:00) ----------------------------------> Still running at 02:00 |
  • | Job 2 (02:00) (SKIPPED / BLOCKED to prevent resource contention or race conditions)|
  • | |
  • | 3. Replace: |
  • | Job 1 (01:00) -------------------------> [TERMINATED / CANCELLED at 02:00] |
  • | Job 2 (02:00) -------------------------> Starts fresh execution |
  • +-----------------------------------------------------------------------------------------+

Concurrency Policy Behavior when Previous Job is Running Ideal Use Case

Allow (Default) Allows concurrent jobs to run simultaneously. Independent log analysis, idempotent event processing.

Forbid Skips the new job run entirely until the previous execution finishes. Database backups, ledger reconciliations, sequential ETL.

Replace Cancels the currently running job and starts a new one in its place. Fresh cache re-population, real-time snapshot generators.

  • 4. Operational Troubleshooting and Edge Cases
  • startingDeadlineSeconds

If a CronJob misses its scheduled execution time (e.g., control plane outage, cluster downtime, or concurrencyPolicy: Forbid ), startingDeadlineSeconds defines the maximum allowable delay (in seconds) to start the missed Job. If the delay exceeds this limit, the execution is recorded as missed.

Caution

If more than 100 execution windows are missed since the last scheduled time and no startingDeadlineSeconds is configured, the CronJob controller stops scheduling future Jobs entirely and logs an error in its controller event stream.

  • Imperative Management Commands # Trigger an ad-hoc Job execution immediately from a CronJob template
  • kubectl create job --from=cronjob/nightly-report manual-run-001
  • # Pause a CronJob without deleting it
  • kubectl patch cronjob nightly-report -p '{"spec":{"suspend":true}}'
  • # Resume a suspended CronJob
  • kubectl patch cronjob nightly-report -p '{"spec":{"suspend":false}}'
  • # Check Job logs
  • kubectl logs job/db-backup-job

Loading diagram... CronJob Execution Scheduling & Concurrency Resolution Flow