1001Ferramentas
Dev

Kubernetes CronJob Generator

Generate a Kubernetes CronJob in YAML with the schedule expression and the container to run. Schedule recurring tasks in your cluster and apply with kubectl.

YAML

Schedule a cluster task without nesting mistakes

You want a backup running at two in the morning inside the cluster, and the answer is a CronJob. The obstacle is the shape: a spec inside a jobTemplate inside a spec inside a template inside another spec, five levels before you reach the container. It is the Kubernetes object where indentation goes wrong most often, and where people give up and fall back to a crontab on some random box.

The output uses apiVersion batch/v1, the name you typed, the schedule in quotes and a single container called job running the image you gave it, with restartPolicy OnFailure. The quotes around the schedule are not decoration: without them, some expressions turn into something else in the YAML parser. There is no field for command or args, so the image has to do the right work in its own ENTRYPOINT.

The detail that catches almost everyone: the schedule runs in the controller time zone, usually UTC, unless you declare spec.timeZone. In Sao Paulo, a job set for 2am fires at 11pm the previous day. If the image is generic like alpine, add command and args after copying. And for a task that must never overlap the previous run, add concurrencyPolicy Forbid. The YAML is generated in the browser.

Frequently asked questions

Does the schedule use my local time zone?
Not by default. A CronJob follows the kube-controller-manager time zone, which is almost always UTC. Since Kubernetes 1.27 you can set spec.timeZone to something like America/Sao_Paulo.
How do I pass a command to the container?
Add command and args inside the container block, at the same level as image. The page does not generate those fields, so it is a manual edit after copying.
Why do old Jobs pile up?
That is the default behaviour: three successful runs and one failed run are kept. Tune successfulJobsHistoryLimit and failedJobsHistoryLimit in the CronJob spec.

Related Tools