Kubernetes CronJobs — Part 1: Basics

1) Basic Cron Job Example

nano myCronJob.yamlapiVersion: batch/v1beta1
kind: CronJob
metadata:
name: mycronjob
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: mycron-container
image: alpine
imagePullPolicy: IfNotPresent

command: ['sh', '-c', 'echo Job Pod is Running ; sleep 5']

restartPolicy: OnFailure
terminationGracePeriodSeconds: 0

concurrencyPolicy: Allow
kubectl create -f myCronJob.yaml
cronjob.batch/mycronjob created
  • kubectl get job
  • kubectl get pods
kubectl get cronjobNAME        SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE
mycronjob */1 * * * * False 0 <none> 14s
kubectl get jobNo resources found.
kubectl get job
NAME COMPLETIONS DURATION AGE
mycronjob-1548664260 0/1 4s 4s
kubectl get po
NAME READY STATUS RESTARTS AGE
mycronjob-1548664260-7dhlm 1/1 Running 0 5s
kubectl get jobNAME                   COMPLETIONS   DURATION   AGE
mycronjob-1548664260 1/1 6s 6s
kubectl get poNAME READY STATUS RESTARTS AGE
mycronjob-1548664260-7dhlm 0/1 Completed 0 7s
  • job has completed: COMPLETIONS 1
  • job took 6s … DURATION
  • Pod is Completed
  • Pod is no longer available … READY is 0
kubectl get cronjobNAME        SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE
mycronjob */1 * * * * False 0 54s 87s
kubectl get job
NAME COMPLETIONS DURATION AGE
mycronjob-1548664260 1/1 6s 56s
kubectl get po
mycronjob-1548664260-7dhlm 0/1 Completed 0 56s
kubectl get cronjob
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
mycronjob */1 * * * * False 1 1s 94s
kubectl get jobNAME                   COMPLETIONS   DURATION   AGE
mycronjob-1548664260 1/1 6s 63s
mycronjob-1548664320 0/1 3s 3s
kubectl get po
NAME READY STATUS RESTARTS AGE
mycronjob-1548664260-7dhlm 0/1 Completed 0 63s
mycronjob-1548664320-75n9z 1/1 Running 0 3s
kubectl get jobNAME                   COMPLETIONS   DURATION   AGE
mycronjob-1548664260 1/1 6s 71s
mycronjob-1548664320 1/1 6s 11s
kubectl get poNAME READY STATUS RESTARTS AGE
mycronjob-1548664260-7dhlm 0/1 Completed 0 71s
mycronjob-1548664320-75n9z 0/1 Completed 0 11s
kubectl describe cronjob/mycronjobName:                       mycronjob
Schedule: */1 * * * *
Concurrency Policy: Allow
Pod Template:
Containers:
mycron-container:
Command:
echo Job Pod is Running ; sleep 5
Last Schedule Time: Mon, 28 Jan 2019 10:33:00 +0200
Active Jobs: mycronjob-1548664380
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 2m6s cronjob-controller Created job mycronjob-1548664260
Normal SawCompletedJob 116s cronjob-controller Saw completed job: mycronjob-1548664260
Normal SuccessfulCreate 66s cronjob-controller Created job mycronjob-1548664320
Normal SawCompletedJob 56s cronjob-controller Saw completed job: mycronjob-1548664320
Normal SuccessfulCreate 6s cronjob-controller Created job mycronjob-1548664380
kubectl get job
NAME COMPLETIONS DURATION AGE
mycronjob-1548664560 1/1 7s 2m53s
mycronjob-1548664620 1/1 6s 112s
mycronjob-1548664680 1/1 7s 52s
kubectl get pods
NAME READY STATUS RESTARTS AGE
mycronjob-1548664560-nq4zm 0/1 Completed 0 2m53s
mycronjob-1548664620-5srx2 0/1 Completed 0 112s
mycronjob-1548664680-mxpdx 0/1 Completed 0 52s
kubectl get job
NAME COMPLETIONS DURATION AGE
mycronjob-1548664560 1/1 7s 3m3s
mycronjob-1548664620 1/1 6s 2m2s
mycronjob-1548664680 1/1 7s 62s
mycronjob-1548664740 0/1 2s 2s
kubectl get pods
NAME READY STATUS RESTARTS AGE
mycronjob-1548664560-nq4zm 0/1 Completed 0 3m3s
mycronjob-1548664620-5srx2 0/1 Completed 0 2m2s
mycronjob-1548664680-mxpdx 0/1 Completed 0 62s
mycronjob-1548664740-cqrlt 0/1 ContainerCreating 0 2s
kubectl get cronjobNAME        SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE
mycronjob */1 * * * * False 1 12s 7m45s
  • number of jobs successfully completed
  • number of jobs UNsuccessfully completed
kubectl delete -f myCronJob.yaml 

cronjob.batch "mycronjob" deleted

2) concurrencyPolicy: Allow

nano myCronJob.yamlapiVersion: batch/v1beta1
kind: CronJob
metadata:
name: mycronjob
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: mycron-container
image: alpine
imagePullPolicy: IfNotPresent

command: ['sh', '-c', 'echo Job Pod is Running ; sleep 90']

restartPolicy: OnFailure
terminationGracePeriodSeconds: 0

concurrencyPolicy: Allow
kubectl create -f myCronJob.yaml

cronjob.batch/mycronjob created
kubectl get cronjob
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
mycronjob */1 * * * * False 0 <none> 15s
kubectl get job
mycronjob-1548665220 0/1 4s 4s
kubectl get pods
NAME READY STATUS RESTARTS AGE
mycronjob-1548665220-8pdnw 1/1 Running 0 4s
kubectl get job
NAME COMPLETIONS DURATION AGE
mycronjob-1548665220 0/1 63s 63s
mycronjob-1548665280 0/1 3s 3s
kubectl get pods
NAME READY STATUS RESTARTS AGE
mycronjob-1548665220-8pdnw 1/1 Running 0 64s
mycronjob-1548665280-6cnjm 1/1 Running 0 4s
kubectl get job
NAME COMPLETIONS DURATION AGE
mycronjob-1548665220 0/1 76s 76s
mycronjob-1548665280 0/1 16s 16s
kubectl get pods
NAME READY STATUS RESTARTS AGE
mycronjob-1548665220-8pdnw 1/1 Running 0 76s
mycronjob-1548665280-6cnjm 1/1 Running 0 16s
kubectl get job
NAME COMPLETIONS DURATION AGE
mycronjob-1548665220 1/1 92s 94s
mycronjob-1548665280 0/1 34s 34s
kubectl get pods
NAME READY STATUS RESTARTS AGE
mycronjob-1548665220-8pdnw 0/1 Completed 0 94s
mycronjob-1548665280-6cnjm 1/1 Running 0 34s
kubectl get job
NAME COMPLETIONS DURATION AGE
mycronjob-1548665280 1/1 92s 4m2s
mycronjob-1548665340 1/1 91s 3m1s
mycronjob-1548665400 1/1 92s 2m1s
mycronjob-1548665460 0/1 61s 61s
mycronjob-1548665520 0/1 1s 1s
kubectl get pods
NAME READY STATUS RESTARTS AGE
mycronjob-1548665280-6cnjm 0/1 Completed 0 4m2s
mycronjob-1548665340-wm2zk 0/1 Completed 0 3m1s
mycronjob-1548665400-nnfgk 0/1 Completed 0 2m1s
mycronjob-1548665460-crn6x 1/1 Running 0 61s
mycronjob-1548665520-qt7zd 0/1 ContainerCreating 0 1s
kubectl describe cronjob/mycronjobEvents:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 5m44s cronjob-controller Created job mycronjob-1548665220
Normal SuccessfulCreate 4m44s cronjob-controller Created job mycronjob-1548665280
Normal SawCompletedJob 4m3s cronjob-controller Saw completed job: mycronjob-1548665220
Normal SuccessfulCreate 3m43s cronjob-controller Created job mycronjob-1548665340
Normal SawCompletedJob 3m3s cronjob-controller Saw completed job: mycronjob-1548665280
Normal SuccessfulCreate 2m43s cronjob-controller Created job mycronjob-1548665400
Normal SawCompletedJob 2m3s cronjob-controller Saw completed job: mycronjob-1548665340
Normal SuccessfulCreate 103s cronjob-controller Created job mycronjob-1548665460
Normal SawCompletedJob 63s cronjob-controller Saw completed job: mycronjob-1548665400

Normal SuccessfulDelete 63s cronjob-controller Deleted job mycronjob-1548665220 ..........

Normal SuccessfulCreate 43s cronjob-controller Created job mycronjob-1548665520
Normal SawCompletedJob 3s cronjob-controller Saw completed job: mycronjob-1548665460

Normal SuccessfulDelete 3s cronjob-controller Deleted job mycronjob-1548665280 ...........
kubectl get pods
NAME READY STATUS RESTARTS AGE
mycronjob-1548665400-nnfgk 0/1 Completed 0 4m21s
mycronjob-1548665460-crn6x 0/1 Completed 0 3m21s
mycronjob-1548665520-qt7zd 0/1 Completed 0 2m21s
mycronjob-1548665580-qzpqq 1/1 Running 0 81s
mycronjob-1548665640-68dhw 1/1 Running 0 21s
kubectl delete -f myCronJob.yaml 

cronjob.batch "mycronjob" deleted

3) concurrencyPolicy: Forbid

nano myCronJob.yamlapiVersion: batch/v1beta1
kind: CronJob
metadata:
name: mycronjob
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: mycron-container
image: alpine
imagePullPolicy: IfNotPresent

command: ['sh', '-c', 'echo Job Pod is Running ; sleep 90']

restartPolicy: OnFailure
terminationGracePeriodSeconds: 0

concurrencyPolicy: Forbid
kubectl create -f myCronJob.yaml 

cronjob.batch/mycronjob created
kubectl get job
NAME COMPLETIONS DURATION AGE
mycronjob-1548666360 0/1 57s 57s
kubectl get pods
NAME READY STATUS RESTARTS AGE
mycronjob-1548666360-gg26f 1/1 Running 0 57s
kubectl get job
NAME COMPLETIONS DURATION AGE
mycronjob-1548666360 0/1 78s 78s
NAME READY STATUS RESTARTS AGE
mycronjob-1548666360-gg26f 1/1 Running 0 78s
kubectl get job
NAME COMPLETIONS DURATION AGE
mycronjob-1548666360 1/1 92s 95s
kubectl get pods
NAME READY STATUS RESTARTS AGE
mycronjob-1548666360-gg26f 0/1 Completed 0 95s
NAME                   COMPLETIONS   DURATION   AGE
mycronjob-1548666360 1/1 92s 103s
mycronjob-1548666420 0/1 3s 3s
kubectl get pods
NAME READY STATUS RESTARTS AGE
mycronjob-1548666360-gg26f 0/1 Completed 0 103s
mycronjob-1548666420-t92nc 1/1 Running 0 3s
kubectl get job
NAME COMPLETIONS DURATION AGE
mycronjob-1548666360 1/1 92s 3m16s
mycronjob-1548666420 1/1 93s 96s
kubectl get pods
NAME READY STATUS RESTARTS AGE
mycronjob-1548666360-gg26f 0/1 Completed 0 3m16s
mycronjob-1548666420-t92nc 0/1 Completed 0 96s
kubectl get job
NAME COMPLETIONS DURATION AGE
mycronjob-1548666360 1/1 92s 3m21s
mycronjob-1548666420 1/1 93s 101s
mycronjob-1548666540 0/1 0s
kubectl get pods
NAME READY STATUS RESTARTS AGE
mycronjob-1548666360-gg26f 0/1 Completed 0 3m21s
mycronjob-1548666420-t92nc 0/1 Completed 0 101s
mycronjob-1548666540-zds5w 0/1 ContainerCreating 0 0s
kubectl get job
NAME COMPLETIONS DURATION AGE
mycronjob-1548666360 1/1 92s 3m29s
mycronjob-1548666420 1/1 93s 109s
mycronjob-1548666540 0/1 8s 8s
kubectl get pods
NAME READY STATUS RESTARTS AGE
mycronjob-1548666360-gg26f 0/1 Completed 0 3m29s
mycronjob-1548666420-t92nc 0/1 Completed 0 109s
mycronjob-1548666540-zds5w 1/1 Running 0 8s
kubectl describe cronjob/mycronjobEvents:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 3m34s cronjob-controller Created job mycronjob-1548666360

Normal SawCompletedJob 114s cronjob-controller Saw completed job: mycronjob-1548666360
Normal SuccessfulCreate 114s cronjob-controller Created job mycronjob-1548666420

Normal SawCompletedJob 13s cronjob-controller Saw completed job: mycronjob-1548666420
Normal SuccessfulCreate 13s cronjob-controller Created job mycronjob-1548666540
kubectl get cronjob
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
mycronjob */1 * * * * False 1 65s 12m
kubectl delete -f myCronJob.yaml 

cronjob.batch "mycronjob" deleted

4) concurrencyPolicy: Replace

nano myCronJob.yaml

apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: mycronjob
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: mycron-container
image: alpine
imagePullPolicy: IfNotPresent

command: ['sh', '-c', 'echo Job Pod is Running ; sleep 90']

restartPolicy: OnFailure
terminationGracePeriodSeconds: 0

concurrencyPolicy: Replace
kubectl create -f myCronJob.yaml

cronjob.batch/mycronjob created
kubectl get job
NAME COMPLETIONS DURATION AGE
mycronjob-1548667320 0/1 58s 58s
kubectl get pods
NAME READY STATUS RESTARTS AGE
mycronjob-1548667320-68bjn 1/1 Running 0 58s
kubectl get job
NAME COMPLETIONS DURATION AGE
mycronjob-1548667380 0/1 6s 6s
kubectl get pods
NAME READY STATUS RESTARTS AGE
mycronjob-1548667380-l7vvx 1/1 Running 0 6s
kubectl describe cronjob/mycronjob
Concurrency Policy: Replace
Pod Template:
Containers:
mycron-container:
Command:
echo Job Pod is Running ; sleep 90
Events:
---- ------ ---- ---- -------
Normal SuccessfulCreate 77s cronjob-controller Created job mycronjob-1548667320
Normal SuccessfulDelete 17s cronjob-controller Deleted job mycronjob-1548667320
kubectl get job
NAME COMPLETIONS DURATION AGE
mycronjob-1548667380 0/1 52s 52s
kubectl get pods
NAME READY STATUS RESTARTS AGE
mycronjob-1548667380-l7vvx 1/1 Running 0 53s
... 8 seconds later ... NEW job ...kubectl get job
NAME COMPLETIONS DURATION AGE
mycronjob-1548667440 0/1 1s 1s
kubectl get pods
NAME READY STATUS RESTARTS AGE
mycronjob-1548667440-t95z5 1/1 Running 0 1s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 2m17s cronjob-controller Created job mycronjob-1548667320
Normal SuccessfulDelete 77s cronjob-controller Deleted job mycronjob-1548667320

Normal SuccessfulCreate 77s cronjob-controller Created job mycronjob-1548667380
Normal SuccessfulDelete 16s cronjob-controller Deleted job mycronjob-1548667380

Normal SuccessfulCreate 16s cronjob-controller Created job mycronjob-1548667440
kubectl delete -f myCronJob.yaml 

cronjob.batch "mycronjob" deleted

Original Source

--

--

--

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Docker 101

SOFAStack: Building a Financial-level Cloud-Native Architecture

Query Tuning PostgreSQL

How to Manage Systemd Services and Units on ECS with the Systemctl Command

Java Interface

Bring PostGIS to PostgreSQL on Alibaba Cloud (Part 1)

Red Hat — Driving Business Innovation with Open Source

Continuous Integration in ABAP

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

More from Medium

Kubernetes, for software developers (part 2)

Resource allocation in Kubernetes

Problem DataBase PostgreSQL Hight Availability Postgres HA Issues

Kubernetes and its architecture (part 1)