0

Im very new to kubernetes ,here i tired a cronjob yaml in which the pods are created at every 1 minute.

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            args:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure

but the pods are created only after 1 minute.is it possible to run job immediately and after that every 1 minute ?

june alex
  • 152
  • 7
  • CronJob is backed by Job, you can just run a Job. See https://stackoverflow.com/a/50041304 and https://kubernetes.io/docs/concepts/workloads/controllers/job/ – d4nyll Sep 02 '20 at 19:23
  • @d4nyll .im using kubectl create command .but it always started after specified minute – june alex Sep 03 '20 at 04:17
  • If it's not clear, you need to create the CronJob _as well as_ a Job derived from that CronJob. – d4nyll Sep 03 '20 at 06:26

1 Answers1

1

As already stated in the comments CronJob is backed by Job. What you can do is literally launch Cronjob and Job resources using the same spec at the same time. You can do that conveniently using helm chart or kustomize.

Alternatively you can place both manifests in the same file or two files in the same directory and then use:

kubectl apply -f <file/dir>

With this workaround initial Job is started and then after some time Cronjob. The downside of this solution is that first Job is standalone and it is not included in the Cronjob's history. Another possible side effect is that the first Job and first CronJob can run in parallel if the Job cannot finish its tasks fast enough. concurrencyPolicy does not take that Job into consideration.

From the documentation:

A cron job creates a job object about once per execution time of its schedule. We say "about" because there are certain circumstances where two jobs might be created, or no job might be created. We attempt to make these rare, but do not completely prevent them.

So if you want to keep the task execution more strict, perhaps it may be better to use Bash wrapper script with sleep 1 between task executions or design an app that forks sub processes after specified interval, create a container image and run it as a Deployment.

thomas
  • 3,828
  • 2
  • 16