CronJobs were first released by Bell labs in 1975. Used to invoke periodic jobs on a given day, week, or month at a specific hour & specific minute. CronJob kind has made its way into the kubenetes API and are packed full of capabilities not immediately noticeable by one's imagination. Here is a workflow I developed that functions well and also provides a quick way to debug by creating Jobs from CronJobs.
Lets begin defining a new CronJob. Design, write, push, and schedule an image to be ran in your k8 cluster using Docker Hub, Docker, and Python. We'll follow up design of the newly scheduled job, by checking logs and cleaning up our cluster. Removing unwanted Jobs, Pods, or other objects we've already checked.
for idx in range(1, 5):
RUN apt-get update && apt-get install python3-dev python3 -y
COPY ./sleeping-beetle.py ./sleeping-beetle.py
ENTRYPOINT ["python3", "sleeping-beetle.py"]
With sleeping-beetle.py and Dockerfile written, lets build a Docker Image and push it to Docker Hub.
$ docker build . -t sleeping-beetle
$ docker tag sleeping-beetle jbcurtin/sleeping-beetle
$ docker push jbcurtin/sleeping-beetle
The push refers to repository [docker.io/jbcurtin/sleeping-beetle]
94f1b0a94f4e: Layer already exists
519a13e31af7: Pushing [========================> ] 86.27MB/177MB
114ca5b7280f: Layer already exists
Kubernetes kind: CronJob controller implementations are somewhat unusual in the way scheduling is performed. The inputs we use are most certainly cron specific, but the behaviour of kind: CronJob can be excessive. Without properly setting startingDeadlineSeconds and concurrencyPolicy, the controllor may end up scheduling more than one job at a time. Kubernetes CronJob has more details as to why this is. I'm often more pragmatic when implementing and documenting architecture solutions, I'll embed some default values that have worked well for me.
With Sleeping Beetle now ready. Load it into a k8-cluster by creating a kind: CronJob and then kubectl apply.
schedule: '* */1 * * *'
- name: sleeping-beetle
Apply sleeping-beetle.yaml to the cluster
$ kubectl apply -f sleeping-beetle.yaml
Setup is complete, we can start creating objects with kubectl. Its about 14:44 in the afternoon and we don't want to wait for there oclock to come around. Rather than fidgeting with schedule: attribute, we can create a Job from cronjob.batch/sleeping-beetle-cronjob
$ kubectl create job --from=cronjob/sleeping-beetle-cronjob sleeping-beetle-1
Check the logs
$ kubectl logs job/sleeping-beetle-1
Looks good. Job complete. Clean up our cluster and logout.
$ kubectl delete jobs/sleeping-beetle-1
$ kubectl delete pod --field-selector=status.phase==Succeeded