CodeBlanks Logo Icon CodeBlanks

Summary - Job and CronJob

Combine Job and CronJob YAML to write scheduled tasks.

Summary: Job and CronJob

In this group:

  • A Job runs a task to completion; restartPolicy is Never/OnFailure, and backoffLimit is the retry limit.
  • activeDeadlineSeconds manages the time, ttlSecondsAfterFinished the cleanup; kubectl wait --for=condition=complete waits.
  • completions/parallelism give multiple and parallel runs; Indexed mode gives an index per part.
  • A CronJob creates Jobs on a schedule: cron format, timeZone, jobTemplate.
  • concurrencyPolicy manages overlap and history limits manage old Jobs; create job --from=cronjob/ triggers by hand.
kubectl create job goc --from=cronjob/gece-yedek
kubectl wait --for=condition=complete job/goc --timeout=300s

Practice A bounded, self-cleaning Job

Write a Job's spec section (starting with spec:): backoffLimit: 2, activeDeadlineSeconds: 600, ttlSecondsAfterFinished: 3600; restartPolicy: OnFailure in the template; container named rapor, image api:1.0.

Starter code

terminal
# buraya yaz

Sample solution

terminal
spec:
  backoffLimit: 2
  activeDeadlineSeconds: 600
  ttlSecondsAfterFinished: 3600
  template:
    spec:
      restartPolicy: OnFailure
      containers:
        - name: rapor
          image: api:1.0

Expected output

job.batch/rapor created

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What happens if you write restartPolicy: Always in a Job pod template?

  • The Job behaves like a Deployment
  • It is ignored
  • The API server rejects it Correct answer
  • The Job never ends

Explanation: Only Never and OnFailure are valid in Jobs.

2 Give the schedule's time zone.

Question
spec:
  schedule: "0 3 * * *"
  ____: "Europe/Istanbul"
Answer
spec:
  schedule: "0 3 * * *"
  timeZone: "Europe/Istanbul"

Explanation: timeZone takes an IANA time zone name.

3 Match.

Term Meaning
Job Once, to completion
CronJob On a schedule
backoffLimit Retries
suspend Pause temporarily

All lessons in Advanced