Helm chart for Kubernetes Cronjob

Nikhil Surendran
3 min readOct 21, 2020

--

One CronJob object is like one line of a crontab (cron table) file. It runs a job periodically on a given schedule, written in Cron format. Cron jobs are useful for creating periodic and recurring tasks, like running backups or sending emails. Cron jobs can also schedule individual tasks for a specific time, such as if you want to schedule a job for a low activity period.

You can download the chart folder from repo: https://github.com/cloudops-guru/helm-cronjob

Standard Helm template to create a Kubernetes CronJob

Here the helm chart to write your first cronjob.yaml using helm : In this example we are configuring single cronjob that executes a script “/opt/myapp/myscript.sh” on every day at 10:30. The script require some the secrets and keys which is already saved to kube secret “myapp-secret” and we are injecting these values using env variables in container.

CronJob.yaml

apiVersion: batch/v1beta1
kind: CronJob
metadata:
namespace: {{ .Values.env }}
name: {{ .Chart.Name }}
labels:
draft: {{ default "draft-app" .Values.draft }}
chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}"
spec:
schedule: {{ .Values.schedule }}
successfulJobsHistoryLimit: 0
jobTemplate:
spec:
template:
metadata:
labels:
draft: {{ default "draft-app" .Values.draft }}
app: {{ .Chart.Name }}
spec:
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
args:
- sh
- -c
- |
sleep 2
"/opt/myapp/myscript.sh"
sleep 30
env:
- name: APP_SECRET
valueFrom:
secretKeyRef:
name: {{ .Values.appsecret }}
key: app_secret
- name: APP_CLIENT_ID
valueFrom:
secretKeyRef:
name: {{ .Values.appsecret }}
key: app_client_id
ports:
- containerPort: {{ .Values.service.internalPort }}
restartPolicy: {{ .Values.restartPolicy }}
imagePullSecrets:
- name: artifactory

Values.yaml

env: dev
appsecret: myapp-secret
schedule: '"30 10 * * *"'
restartPolicy: OnFailure
image:
repository: XYZ
tag: 1.0.2
pullPolicy: IfNotPresent
service:
internalPort: 8028

Iterate multiple Jobs in helm chart CronJob

We can use the ‘range’ instruction for iterative cronjob resource, using the values.yaml you could be able to pass multiple values for all different jobs. In the range instruction, you need to call all Values $.Values.val instead of .Values.val . You need to have and — and end to close the loop.

CronJob.yaml

{{- range $job, $val := .Values.cronjob.crons }}
apiVersion: batch/v1beta1
kind: CronJob
metadata:
namespace: {{ .Values.env }}
name: {{ .name }}
labels:
draft: {{ default "draft-app" $.Values.draft }}
chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}"
spec:
schedule: {{ .schedule | quote }}
successfulJobsHistoryLimit: 0
jobTemplate:
spec:
template:
metadata:
labels:
draft: {{ default "draft-app" $.Values.draft }}
app: {{ .name }}
spec:
containers:
- name: {{ .name }}
image: "{{ $.Values.image.repository }}:{{ $.Values.image.tag }}"
imagePullPolicy: {{ $.Values.image.pullPolicy }}
args:
- sh
- -c
- |
sleep 2
"./bin/console --env={{ $.Values.env }} {{ .command}}"
sleep 30
ports:
- containerPort: {{ $.Values.service.internalPort }}
restartPolicy: {{ $.Values.restartPolicy }}
imagePullSecrets:
- name: artifactory
---
{{- end}}

Values.yaml

env: dev  
restartPolicy: OnFailure
image:
repository: XYZ
tag: 1.0.2
pullPolicy: IfNotPresent
service:
internalPort: 8028
crons:
"0":
name: cog-first-cron
command: /opt/myapp/myscript1.sh
schedule: "10 4 * * *"
"1":
name: cog-second-cron
command: /opt/myapp/myscript2.sh
schedule: "" 30 5 * * *"
"2":
name: cog-third-cron
command: /opt/myapp/myscript3.sh
schedule: "0/5 * * * *"

https://cloudops-guru.in/2020/09/17/helm-chart-define-multiple-cronjob/

--

--

Nikhil Surendran
Nikhil Surendran

Written by Nikhil Surendran

Cloud and DevOps Architect | A software engineer at heart, always exploring innovative solutions to drive performance and resilience in complex environments.

Responses (3)