Skip to content

Room 15

JOBS & CRONJOBS


The Task Scheduler

  • Some work doesn’t need to run forever - Jobs handle one-off tasks, CronJobs handle recurring ones.
  • A CronJob has been computing passwords on a schedule. Find its last output.

📜 Mission Briefing

  1. Apply job.yaml - a Job that completes after running a computation.
  2. Wait for the Job to complete: kubectl get jobs --watch.
  3. Read the Job’s pod logs - it outputs a partial password.
  4. Apply cronjob.yaml - a CronJob that runs every minute.
  5. Wait for one execution to complete, then read the CronJob pod’s logs.
  6. Combine both outputs: cronjob + 99 → cronjob99.

Key Commands
Command Purpose
kubectl get jobs List Jobs
kubectl describe job NAME Job details
kubectl get cronjobs List CronJobs
kubectl logs job/NAME Logs from Job’s pod
kubectl create job --from=cronjob/NAME test-run Manual trigger

How Jobs & CronJobs Work
# Job: Runs to completion (one-off task)
apiVersion: batch/v1
kind: Job
metadata:
  name: pi-calculator
spec:
  completions: 1          # Number of successful completions needed
  parallelism: 1          # Max parallel pods
  backoffLimit: 4          # Max retries on failure
  activeDeadlineSeconds: 100  # Timeout
  template:
    spec:
      restartPolicy: Never  # Jobs must be Never or OnFailure
      containers:
        - name: pi
          image: perl
          command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
---
# CronJob: Runs Jobs on a schedule
apiVersion: batch/v1
kind: CronJob
metadata:
  name: backup
spec:
  schedule: "*/5 * * * *"     # Every 5 minutes (cron syntax)
  concurrencyPolicy: Forbid   # Allow, Forbid, Replace
  successfulJobsHistoryLimit: 3
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: OnFailure
          containers:
            - name: backup
              image: busybox
              command: ['sh', '-c', 'echo "Backup done at $(date)"']
# Cron schedule format: minute hour day-of-month month day-of-week
# */5 * * * *    = every 5 minutes
# 0 * * * *      = every hour
# 0 0 * * *      = daily at midnight
# 0 0 * * 0      = weekly on Sunday

# Create a Job imperatively
kubectl create job my-job --image=busybox -- echo "Hello"

# Trigger CronJob manually
kubectl create job --from=cronjob/backup manual-backup

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

Hints

Hint 1

Apply and watch the Job:

kubectl apply -f job.yaml
kubectl get jobs --watch
kubectl logs job/password-job

Hint 2

Apply the CronJob and wait for execution:

kubectl apply -f cronjob.yaml
kubectl get cronjobs
# Wait 1 minute, then:
kubectl get pods -l job-name --sort-by=.metadata.creationTimestamp

Hint 3

The password is cronjob99.


🔓 Unlock Room 16

openssl enc -aes-256-cbc -d -a -pbkdf2 \
        -in ../room_16/README             \
        -out ../room_16/README.txt        \
        -pass pass:PASSWORD
cat ../room_16/README.txt