Kubernetes Job & Cronjob
Types of Jobs.
- Run to completion(Jobs).
- Scheduled(Cronjobs)
Jobs :
- The main function of a job is to create one or more pods and tracks about the success of pods.
- They ensure that the specified number of pods is completed successfully. When a specified number of a successful run of pods is completed, then the job is considered complete.
- This is primarily used for performing a batch processing so as soon as we submit this job manifest file to API server pod will kick in and execute a task and when the task is completed it shut down by itself.
- Running pod temporarily till the task is completed.
5. when the job is completed pods go to the shutdown state.
Now we see how to use jobs in Kubernetes.
- Create a job.yaml file.
apiVersion: batch/v1
kind: Job
metadata:
name: helloworld
spec:
template:
spec:
containers:
- name: busybox
image: busybox
command: ["echo", "Hello harshal!!!"]
restartPolicy: Never
here we just create a busybox pod and echo “hello harshal”
2. Create a job.
kubectl create -f <job.yaml>
3. The job has been scheduled and you can see the job is here.
you see the job completions 1/1 means job is completed.
4. We can also see the job logs.
kubectl describe job <jobname>
Output: it says when the job actually started and when it completed and how long it took.
5. When the job is completed not gonna delete itself so we need to clean up.
kubectl delete job <jobname>
Now we see different types of job parameters.
- Completions.
- parallelism.
- backofflimit.
- activeDeadlineSecond
Completions
- completions: Basically the repetition of a process, default it’s 1.
e.x: if we set completion as value 2, the jobs runes 2times.
2. In this pod runes one after another, when 1st pod completed then 2nd pod is started. it’s also called sequential processing.
3. See 2 pods are running and completions are also set to 2/2.
parallelism
- In completions, we see jobs runners sequential one after another.
- Using the parallelism we can set the limit that how many jobs can run in parallel
e.x: if u set completion: 2, and parallelism:2 both the pods are running at the same time.
backoffLimit
- If the container is failing to load the image or completing the job, then still it creates more pods one after another until it succeeds.
- In this situation, backoffLimit is used.
- We set the backoffLimit for the job , that not create more pods once it’s failed.
E.X: backoffLimit:2, once pods failed 2 times it won’t create more pods.
4. After backoffLimit reaches, we got the below message when you describe job
kubectl describe job <jobname>
activeDeadlineSecond
- In we can set a threshold to say, if I know job taking 10 sec to come up, so I set an activeDeadlineSecond as a 10second.
- If I set activeDeadlineSecond: 10s, then if pods taking more time then the 10second then pods are automatically terminated.
3. See in the image pods are terminating automatically after 10s.
Scheduled(Cronjobs)
A CronJob
the object allows you to schedule Job
execution rather than starting them manually.
Now we see how to use a cronjob.
- Create a Cronjob.yaml file.
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: helloworld-cron
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: busybox
image: busybox
command: ["echo", "Hello harshal!!!"]
restartPolicy: Never
In the schedule I set the value is “* * * * *” means every 1 min job is triggered and the new pod is created.
2. Create a cronjob
kubectl create -f <cronjob.yaml>
3. After every 1 min, a new pod is created as we set off the scheduler.
output: see when the 60sec age of the 1 pod is completed 2nd pod is created.
CronJob Parameter.
- SuccessfulJobHistoryLimit
- failedJobHistoryLimit
- suspending cronjobs
- concurrency policy
SuccessfulJobHistoryLimit & failedJobHistoryLimit
- By default, the job holds the last 3 successful jobs and last 1failed jobs.
- We changed the YAML file and set the successful job limit and failed job limits by ourself.
suspending cronjobs
- As we see cronjob is continuously making new pods, at the specific scheduled time.
- by default suspend: false, and if we want not to create new pods so we basically set the suspend: true. then no new pod is created after applying these changes.
Output: you see 2 pods are created and when we apply the suspend: true, you won’t see any new jobs is created.
concurrency policy
Concurrency means if the jobs miss its schedule for some reason it’s gonna create a another job.
basically concurrency means more than 1 jobs running at the same time.
It has three possible values — Forbird, Allow, and Replace.
Forbid: if you don’t want concurrent executions of your job.
Allow: will let multiple job instances run concurrently.
Replace: as the concurrency policy, the currently running job will be stopped and a new job will be spawned
Thank you, if you have any doubt reach out to me
LinkedIn : linkedin.com/in/harshal-kathar-b2a19b118