CodeBlanks Logo Icon CodeBlanks

What is a Job?

Learn that a Job runs tasks to completion.

What is a Job?

A Job produces pods that run a task to the end and finish successfully: database migration, batch data processing, generating reports. Unlike a Deployment, a pod isn't restarted when it finishes and the Job is marked "complete". If a pod fails, the Job retries it.

$ kubectl create job pi --image=perl:5.40 -- perl -Mbignum=bpi -wle "print bpi(50)"
$ kubectl get jobs
NAME   STATUS     COMPLETIONS   DURATION   AGE
pi     Complete   1/1           8s         20s
$ kubectl logs job/pi
3.1415926535897932384626433832795028841971693993751

Watching a Job

To wait for a Job to finish, kubectl wait is used; in CI pipelines it prevents deploying the application before the migration is done. Finished Jobs and their pods stay; ttlSecondsAfterFinished deletes them automatically after a given time:

kubectl wait --for=condition=complete job/goc --timeout=120s
kubectl logs job/goc
kubectl delete job goc

Practice A one-off task

Write three commands: create a Job named selam with the busybox:1.36 image running the echo merhaba command; wait for the Job to complete with a 60-second timeout; show the Job's logs.

Starter code

terminal
# buraya yaz

Sample solution

terminal
kubectl create job selam --image=busybox:1.36 -- echo merhaba
kubectl wait --for=condition=complete job/selam --timeout=60s
kubectl logs job/selam

Expected output

job.batch/selam created
job.batch/selam condition met
merhaba

Questions and answers

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

1 Why run a database migration as a Job instead of a Deployment?

  • A Job is faster
  • A Deployment can't connect to a database
  • A Job is free
  • The migration must run once and finish; a Deployment restarts a finished pod Correct answer

Explanation: A Deployment is for continuously running processes.

2 Wait until the Job completes.

Question
kubectl wait --for=condition=____ job/goc --timeout=120s
Answer
kubectl wait --for=condition=complete job/goc --timeout=120s

Explanation: A Job's success condition is Complete.

3 Match.

Term Meaning
Job Run to completion
Deployment Run continuously
COMPLETIONS 1/1 Finished successfully
kubectl wait Wait for a condition

All lessons in Advanced