3
votes

I need to create a Cron jobs at K8S.
some jobs runs every minute , some of them every 5 minutes (24/7)

This jobs need to run rest call (get) to some components and to check the availability and if something is wrong (post) a rest call to other system

To handle this task I currently see two approaches

  1. Create k8s cronjob - which is native k8s CRD and use shell script for it, https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/

PROS is using K8S native way to handle such issue

CONS - very hard to debug, logs debug etc

  1. Create k8s job/cronjob - which execute run a Golang program that will do use following library as cronjob

PROS - debug , logging etc , https://github.com/robfig/cron

CONS - create some abstraction …

Any suggestion, recommendation which is better approach to use If I need a full control on those jobs

1
CONS - very hard to debug, logs debug etc - how a plain k8s pod with a cron timer inside helps to debug in this case? It will be the same container, just a container per job run.Max Lobur
@MaxLobur - im talking about local debug, go code vs shell scriptNSS
If you're running a K8S cronjob it creates a pod with every invocation of the job, so it'll be easy to debug. Is there a reason why you aren't running these cronjobs inside the container? You can use a lifecycle hook, so you don't have to change your Dockerfile.cookiedough
so debugging should be in pros of k8s cron job. You write the code which does task exactly once and exits - easy to debug. If you implement cron timer yourself, you will need to separate that every time you debug.Max Lobur
@cookiedough - I want to create a k8s cronjob/job which execute a go program that running some API calls. this is not a good approach ?I dont want to create a pod for each invocation ....NSS

1 Answers

3
votes

I'd approach this problem in several steps.

First, write the program that does the REST calls, checks the result, maybe posts the alert, and exits. You can write this program in whatever language or toolkit you feel like. If you're comfortable in Go, great; I might pick Python myself; it'd be possible as a shell script, but probably more awkward than many alternatives. Build this program completely independently of Kubernetes. Test it as much as you need to convince yourself that it does what you want.

Once you've made the REST polling program work, and only then, build it into a Docker image; push it to a registry; and create a CronJob Kubernetes resource that runs it on a schedule.

Given how you've described the task, I wouldn't write a specialized program just to replicate Kubernetes' built-in scheduled-task runner. You could; I'd develop it the same way as above but use a Deployment instead of a CronJob; but probably the CronJob path is both simpler and more reliable.