CKAD — Jobs & CronJobs Lab

2 minute read

Lab 1: Parallel Processing Job

Scenario: Create a Job that processes 6 items with 2 Pods running in parallel.

Step 1: Create the Job

apiVersion: batch/v1
kind: Job
metadata:
  name: data-processor
spec:
  completions: 6
  parallelism: 2
  template:
    spec:
      containers:
      - name: processor
        image: busybox:1.36
        command: ["/bin/sh", "-c"]
        args: ["echo Processing batch item && sleep 3 && echo Done"]
      restartPolicy: Never

Step 2: Deploy and Watch

kubectl apply -f data-processor.yaml

# Watch the Pods — you should see 2 Pods running at a time
kubectl get pods -l job-name=data-processor -w

# Check completions
kubectl get job data-processor
# Expected: COMPLETIONS 6/6

What’s Happening

  1. Kubernetes creates 2 Pods simultaneously (parallelism: 2)
  2. As each Pod completes, a new one replaces it
  3. This continues until 6 Pods have succeeded (completions: 6)
  4. The Job is marked Complete

Lab 2: CronJob with History Limits

Scenario: Create a CronJob that runs every minute, keeps 2 successful and 1 failed Job in history, and uses Forbid concurrency.

Step 1: Create the CronJob

kubectl create cronjob minute-check --image=busybox:1.36 \
  --schedule="* * * * *" \
  -- /bin/sh -c "echo 'Health check at $(date)'"

Step 2: Configure Settings

kubectl patch cronjob minute-check -p '{
  "spec": {
    "concurrencyPolicy": "Forbid",
    "successfulJobsHistoryLimit": 2,
    "failedJobsHistoryLimit": 1
  }
}'

Step 3: Verify

# Wait a few minutes and check
kubectl get jobs
# You should see at most 2 successful Jobs

kubectl get cronjob minute-check
# Verify SUSPEND is False and ACTIVE shows recent runs

Cleanup

kubectl delete cronjob minute-check
kubectl delete job data-processor

Practice Questions

Q1: Create a Job named math-job that runs the command expr 3 + 5 using the busybox:1.36 image.

Show Answer ```bash kubectl create job math-job --image=busybox:1.36 -- expr 3 + 5 ``` Check the result: ```bash kubectl logs job/math-job # Expected: 8 ```

Q2: A Job has backoffLimit: 4 and restartPolicy: Never. The container keeps failing. How many Pods will be created before the Job gives up?

Show Answer **5 Pods** will be created — 1 original attempt plus 4 retries (backoffLimit). After the 5th Pod fails, the Job is marked as Failed. With `restartPolicy: Never`, each retry creates a new Pod. With `OnFailure`, the container restarts within the same Pod.

Q3: Create a CronJob named log-cleaner that runs every day at 3:00 AM, with concurrencyPolicy: Replace.

Show Answer ```bash kubectl create cronjob log-cleaner --image=busybox:1.36 \ --schedule="0 3 * * *" \ -- /bin/sh -c "echo 'Cleaning old logs'" kubectl patch cronjob log-cleaner -p '{"spec":{"concurrencyPolicy":"Replace"}}' ``` Or as YAML: ```yaml apiVersion: batch/v1 kind: CronJob metadata: name: log-cleaner spec: schedule: "0 3 * * *" concurrencyPolicy: Replace jobTemplate: spec: template: spec: containers: - name: cleaner image: busybox:1.36 command: ["/bin/sh", "-c", "echo 'Cleaning old logs'"] restartPolicy: OnFailure ```

Q4: How do you manually trigger a CronJob named weekly-report to run immediately?

Show Answer ```bash kubectl create job weekly-report-manual --from=cronjob/weekly-report ``` This creates a one-time Job from the CronJob's template. It does not affect the CronJob's schedule.

Q5: What is the difference between completions and parallelism in a Job spec?

Show Answer - **`completions`** — The total number of successful Pod completions required for the Job to be considered done. Default is 1. - **`parallelism`** — The maximum number of Pods that can run at the same time. Default is 1. Example: `completions: 10, parallelism: 3` means the Job needs 10 successful Pods total, and will run up to 3 at a time.

← Previous: Multi-Container Pods Next: Deployments & Rolling Updates →

Updated: