Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Running Applications  ›  Capstone Project

Jobs

Jobs 12 minA Job runs pods until a set number succeed and then stops, and a CronJob creates Jobs on a cron schedule.

Jobs

Every night at 2 a.m. the notes team copies the database to backup storage. That task should run, finish and stop. A Deployment would restart it forever. A Job is built for work that ends.

The rule

A Job runs pods until enough of them succeed, then stops.

A Job creates pods and watches them. When a pod exits with success, the Job counts it. When the count reaches completions (default 1), the Job is complete. If a pod fails, the Job retries, up to backoffLimit times (default 6).

apiVersion: batch/v1
kind: Job
metadata:
  name: notes-backup
  namespace: notes
spec:
  backoffLimit: 3
  template:
    spec:
      restartPolicy: Never
      containers:
        - name: backup
          image: ghcr.io/example/notes-backup:1.0

Two rules:

  • restartPolicy must be Never or OnFailure. The pod default, Always, would restart a finished task forever, so Jobs reject it.
  • parallelism sets how many pods run at the same time — useful for splitting big work.

To run it on a timetable, wrap it in a CronJob:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: notes-backup
  namespace: notes
spec:
  schedule: "0 2 * * *"
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: OnFailure
          containers:
            - name: backup
              image: ghcr.io/example/notes-backup:1.0

The five schedule fields are minute, hour, day of month, month, day of week. 0 2 * * * means 02:00 every day.

The shortcut

Ask: "Should this ever finish?" No → Deployment. Yes, once → Job. Yes, again and again on a clock → CronJob.

Placing three notes workloadsStep 1 of 3
  1. notes-web

    Serves users all day and should never finish: Deployment.

The trap is a backup script inside a Deployment: when it exits, Kubernetes treats that as a crash and starts it again, looping backups forever.

Drills

Answer fast — trust the rule.

quick drill

Which restartPolicy is not allowed in a Job's pod template?

quick drill

A Job has completions: 5 and parallelism: 2. How many pods run at the same time at most?

quick drill

Which schedule runs at 02:30 every day?

quick drill

A one-off database migration must run before release 1.3. Which object fits?

Remember

  • A Job runs pods until enough succeed, then stops; a CronJob starts Jobs on a schedule.
  • Ask "should this ever finish?": no → Deployment, once → Job, on a clock → CronJob.
  • Trap: finite work in a Deployment gets restarted forever.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainChoose the workload

Choose Deployment, StatefulSet, Job or CronJob for each notes workload and justify it: the web server, the Postgres database, a one-time data migration, and a backup at 02:00 every day (give the schedule).