How to run jobs and cronjobs in Kubernetes

Sheikh Vazid
Tensult Blogs
Published in
3 min readNov 5, 2019

Kubernetes Jobs are created to run pods for a short period of time which are running for completion as opposed to different objects in Kubernetes like Replicasets, Replication Controllers, Demonesets, which is run continuously.

In Kubernetes, we have two types of Jobs

  • Run to completion
  • Schedulers

Prerequisites

  1. Kubernetes Cluster

Run to completion

It ensures to create one or more pods. It can also parallelly run one or more pods at the same time. It also refers as Jobs in Kubernetes

Let’s start to create Jobs and understand different methods to use it

1. Create Job.yaml file

apiVersion: batch/v1
kind: Job
metadata:
name: simple-job
# namespace: staging
spec:
template:
spec:
containers:
- name: busybox
image: busybox
command: ["echo", "Kubernetes Job"]
restartPolicy: Never

2. Let’s deploy Job using this command

kubectl create -f job.yaml

3. To check the job status

kubetl get jobs

4. Check for pod initiate by Job

kubectl get pods

Create multiple pod and parallelism on Job

Here is an example to run multiple pods with parallelism

apiVersion: batch/v1
kind: Job
metadata:
name: parallel-job
generateName: kube-job-
# namespace: prod
spec:
completions: 5
parallelism: 2
schedule: "*/30 * * * *"
ttlSecondsAfterFinished: 1
template:
spec:
containers:
- name: busybox
image: busybox
command: ["echo", "Kubernetes Job"]
restartPolicy: Never

Here we can also use TTL Controler to cleanup completed Jobs

ttlSecondsAfterFinished: 1

We can also schedule a Job using this command before starting templates

schedule: "*/30 * * * *"

It schedules the job to run every 30 minutes.

When running multiple Jobs with the same name it will generate an error stating that a job with the same name exists. To overcome this issue, we can use genrateName field in the metadata section This will generate pods with prefix Kube-job- with numbers

generateName: kube-job-

CronJob

Running Cronjob on Kubernetes is as simple as we are running crontab on Linux.

It runs a job periodically on a given schedule, written in Cron format.

Note: Time is based on the timezone of the master where the job is initiated.

Here in this example, we will create Cronjob that will run a Linux image at the scheduled time.

We will also understand the best ways to schedule the cronjob in Kubernetes.

Creating a YAML for cronjob

apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: simple-cron
# namespace: prod
spec:
schedule: "*/1 * * * *" # runing cronjob for every 1 minute
concurrencyPolicy: Forbid
successfulJobsHistoryLimit: 1
failedJobsHistoryLimit: 1
jobTemplate:
spec:
template:
spec:
containers:
- name: helloworld
image: busybox
command: ["echo", "Kubernetes Job"]
restartPolicy: OnFailure

This YAML will schedule a pod ever one minute

schedule: schedule time of its jobs to be created and executed

concurrencyPolicy:Forbid The Cron job does not allow concurrent runs; if it is time for a new job run and the previous job run hasn't finished yet, the Cron job skips the new job run. So it can be used to keep our cluster clean environment.

successfulJobsHistoryLimit: 1
failedJobsHistoryLimit: 1

It specifies how many completed and failed jobs should be kept in a cluster.

Here we kept limit 1 for both fields to keep our environment clean.

Reference

Here you can Git pull YAML file from Github Repositoryhttps://github.com/tensult/kubernetes/tree/master/JobsRunning Automated Tasks with a CronJob
Edit This Page You can use CronJobs to run jobs on a time-based schedule. These automated jobs run like Cron tasks on a...kubernetes.io
Kubernetes in AWS EKS using terraform and deploy sample applications
Hi folks, I would like to share my first experience with Kubernetes.

Originally published at https://blogs.tensult.com on November 5, 2019.

--

--

Sheikh Vazid
Tensult Blogs

I’m a Data engineer | learning, writing about data engineering